Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1ae934b

Browse files
Merge pull requestneetcode-gh#3300 from AmitSaha15/0410-split-array-largest-sum
Create 0410-split-array-largest-sum.java
2 parentsf1b0c10 +1fa033d commit1ae934b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
classSolution {
2+
publicintsplitArray(int[]nums,intk) {
3+
intstart =0;
4+
intend =0;
5+
6+
for (inti =0;i <nums.length;i++) {
7+
start =Math.max(start,nums[i]);
8+
end +=nums[i];
9+
}
10+
11+
while (start <end) {
12+
intmid =start + (end -start) /2;
13+
14+
// calculate how many pieces you can divide this in with this max sum
15+
intsum =0;
16+
intpieces =1;
17+
for(intnum :nums) {
18+
if (sum +num >mid) {
19+
sum =num;
20+
pieces++;
21+
}else {
22+
sum +=num;
23+
}
24+
}
25+
26+
if (pieces >k) {
27+
start =mid +1;
28+
}else {
29+
end =mid;
30+
}
31+
32+
}
33+
returnend;// here start == end
34+
}
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp