FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-algorithms/src/SplitWithMinimumSum.java at master · anishLearnsToCode/leetcode-algorithms · GitHub
anishLearnsToCode
/
leetcode-algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
17
Star
98
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-algorithms
/
src
/
SplitWithMinimumSum.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
32 lines (29 loc) · 879 Bytes
Breadcrumbs
leetcode-algorithms
/
src
/
SplitWithMinimumSum.java
Copy path
File metadata and controls
32 lines (29 loc) · 879 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// https://leetcode.com/problems/split-with-minimum-sum
// T: O(log(N)log(log(N)))
// S: O(log(log(N)))
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
SplitWithMinimumSum
{
public
int
splitNum
(
int
num
) {
final
List
<
Integer
>
digits
=
getDigits
(
num
);
digits
.
sort
(
Integer
::
compareTo
);
int
a
=
0
,
b
=
0
,
k
=
0
;
for
(
int
digit
:
digits
) {
if
(
k
==
0
) {
a
=
10
*
a
+
digit
;
}
else
{
b
=
10
*
b
+
digit
;
}
k
^=
1
;
}
return
a
+
b
;
}
private
List
<
Integer
>
getDigits
(
int
x
) {
final
List
<
Integer
>
digits
=
new
ArrayList
<>();
final
String
number
=
x
+
""
;
for
(
int
index
=
0
;
index
<
number
.
length
() ;
index
++) {
digits
.
add
(
number
.
charAt
(
index
) -
'0'
);
}
return
digits
;
}
}
Back
|
FazBrowse Home
|
New Git URL