Skip to content

Commit a4f05f8

Browse files
refactor 410
1 parent 6baff82 commit a4f05f8

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
public class _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+
public static class Solution1 {
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-
public int splitArray(int[] nums, int m) {
55-
int max = 0;
56-
long sum = 0;
57-
for (int num : nums) {
58-
max = Math.max(num, max);
59-
sum += num;
60-
}
61-
if (m == 1) {
62-
return (int) sum;
63-
}
64-
//binary search
65-
long l = max;
66-
long r = sum;
67-
while (l <= r) {
68-
long mid = (l + r) / 2;
69-
if (valid(mid, nums, m)) {
70-
r = mid - 1;
71-
} else {
72-
l = mid + 1;
53+
public int splitArray(int[] nums, int m) {
54+
int max = 0;
55+
long sum = 0;
56+
for (int num : nums) {
57+
max = Math.max(num, max);
58+
sum += num;
59+
}
60+
if (m == 1) {
61+
return (int) sum;
7362
}
63+
//binary search
64+
long l = max;
65+
long r = sum;
66+
while (l <= r) {
67+
long mid = (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-
public boolean valid(long target, int[] nums, int m) {
79-
int count = 1;
80-
long total = 0;
81-
for (int num : nums) {
82-
total += num;
83-
if (total > target) {
84-
total = num;
85-
count++;
86-
if (count > m) {
87-
return false;
77+
public boolean valid(long target, int[] nums, int m) {
78+
int count = 1;
79+
long total = 0;
80+
for (int num : nums) {
81+
total += num;
82+
if (total > target) {
83+
total = num;
84+
count++;
85+
if (count > m) {
86+
return false;
87+
}
8888
}
8989
}
90+
return true;
9091
}
91-
return true;
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
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 6/6/17.
11-
*/
129
public class _410Test {
13-
private static _410 test;
14-
private static int[] nums;
10+
private static _410.Solution1 test;
11+
private static int[] nums;
1512

16-
@BeforeClass
17-
public static void setup() {
18-
test = new _410();
19-
}
13+
@BeforeClass
14+
public static void setup() {
15+
test = new _410.Solution1();
16+
}
2017

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

0 commit comments

Comments
 (0)