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

Commita4f05f8

Browse files
refactor 410
1 parent6baff82 commita4f05f8

File tree

2 files changed

+64
-68
lines changed

2 files changed

+64
-68
lines changed

‎src/main/java/com/fishercoder/solutions/_410.java

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -29,66 +29,65 @@
2929
*/
3030
publicclass_410 {
3131

32-
/**credit: https://discuss.leetcode.com/topic/61324/clear-explanation-8ms-binary-search-java
33-
*
34-
* The answer is between maximum value of input array numbers and sum of those numbers.
35-
Use binary search to approach the correct answer.
36-
We have l = max number of array;
37-
r = sum of all numbers in the array;
38-
Every time we do mid = (l + r) / 2;
32+
publicstaticclassSolution1 {
33+
/**
34+
* credit: https://discuss.leetcode.com/topic/61324/clear-explanation-8ms-binary-search-java
35+
*
36+
* The answer is between maximum value of input array numbers and sum of those numbers. Use
37+
* binary search to approach the correct answer. We have l = max number of array; r = sum of all
38+
* numbers in the array; Every time we do mid = (l + r) / 2;
39+
*
40+
* Use greedy to narrow down left and right boundaries in binary search. 3.1 Cut the array from
41+
* left. 3.2 Try our best to make sure that the sum of numbers between each two cuts (inclusive)
42+
* is large enough but still less than mid. 3.3 We'll end up with two results: either we can
43+
* divide the array into more than m subarrays or we cannot. If we can, it means that the mid
44+
* value we pick is too small because we've already tried our best to make sure each part holds
45+
* as many non-negative numbers as we can but we still have numbers left. So, it is impossible
46+
* to cut the array into m parts and make sure each parts is no larger than mid. We should
47+
* increase m. This leads to l = mid + 1; If we can't, it is either we successfully divide the
48+
* array into m parts and the sum of each part is less than mid, or we used up all numbers
49+
* before we reach m. Both of them mean that we should lower mid because we need to find the
50+
* minimum one. This leads to r = mid - 1;
51+
*/
3952

40-
Use greedy to narrow down left and right boundaries in binary search.
41-
3.1 Cut the array from left.
42-
3.2 Try our best to make sure that the sum of numbers between each two cuts (inclusive) is large enough but still less than mid.
43-
3.3 We'll end up with two results: either we can divide the array into more than m subarrays or we cannot.
44-
If we can, it means that the mid value we pick is too small because we've already
45-
tried our best to make sure each part holds as many non-negative numbers as we can
46-
but we still have numbers left.
47-
So, it is impossible to cut the array into m parts and make sure each parts is no larger than mid.
48-
We should increase m. This leads to l = mid + 1;
49-
If we can't, it is either we successfully divide the array into m parts and
50-
the sum of each part is less than mid,
51-
or we used up all numbers before we reach m.
52-
Both of them mean that we should lower mid because we need to find the minimum one. This leads to r = mid - 1;*/
53-
54-
publicintsplitArray(int[]nums,intm) {
55-
intmax =0;
56-
longsum =0;
57-
for (intnum :nums) {
58-
max =Math.max(num,max);
59-
sum +=num;
60-
}
61-
if (m ==1) {
62-
return (int)sum;
63-
}
64-
//binary search
65-
longl =max;
66-
longr =sum;
67-
while (l <=r) {
68-
longmid = (l +r) /2;
69-
if (valid(mid,nums,m)) {
70-
r =mid -1;
71-
}else {
72-
l =mid +1;
53+
publicintsplitArray(int[]nums,intm) {
54+
intmax =0;
55+
longsum =0;
56+
for (intnum :nums) {
57+
max =Math.max(num,max);
58+
sum +=num;
59+
}
60+
if (m ==1) {
61+
return (int)sum;
7362
}
63+
//binary search
64+
longl =max;
65+
longr =sum;
66+
while (l <=r) {
67+
longmid = (l +r) /2;
68+
if (valid(mid,nums,m)) {
69+
r =mid -1;
70+
}else {
71+
l =mid +1;
72+
}
73+
}
74+
return (int)l;
7475
}
75-
return (int)l;
76-
}
7776

78-
publicbooleanvalid(longtarget,int[]nums,intm) {
79-
intcount =1;
80-
longtotal =0;
81-
for (intnum :nums) {
82-
total +=num;
83-
if (total >target) {
84-
total =num;
85-
count++;
86-
if (count >m) {
87-
returnfalse;
77+
publicbooleanvalid(longtarget,int[]nums,intm) {
78+
intcount =1;
79+
longtotal =0;
80+
for (intnum :nums) {
81+
total +=num;
82+
if (total >target) {
83+
total =num;
84+
count++;
85+
if (count >m) {
86+
returnfalse;
87+
}
8888
}
8989
}
90+
returntrue;
9091
}
91-
returntrue;
9292
}
93-
9493
}

‎src/test/java/com/fishercoder/_410Test.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@
66

77
importstaticorg.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 6/6/17.
11-
*/
129
publicclass_410Test {
13-
privatestatic_410test;
14-
privatestaticint[]nums;
10+
privatestatic_410.Solution1test;
11+
privatestaticint[]nums;
1512

16-
@BeforeClass
17-
publicstaticvoidsetup() {
18-
test =new_410();
19-
}
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
test =new_410.Solution1();
16+
}
2017

21-
@Test
22-
publicvoidtest1() {
23-
nums =newint[]{7,2,5,10,8};
24-
assertEquals(18,test.splitArray(nums,2));
25-
}
18+
@Test
19+
publicvoidtest1() {
20+
nums =newint[]{7,2,5,10,8};
21+
assertEquals(18,test.splitArray(nums,2));
22+
}
2623
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp