Skip to content

Commit a83b5cd

Browse files
refactor 330
1 parent 6a04845 commit a83b5cd

File tree

2 files changed

+56
-60
lines changed

2 files changed

+56
-60
lines changed

src/main/java/com/fishercoder/solutions/_330.java

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,55 @@
3030
*/
3131
public class _330 {
3232

33-
/**credit: https://leetcode.com/articles/patching-array/ and https://discuss.leetcode.com/topic/35494/solution-explanation/2
34-
*
35-
* Let miss be the smallest sum in [0,n] that we might be missing.
36-
* Meaning we already know we can build all sums in [0,miss).
37-
* Then if we have a number num <= miss in the given array,
38-
* we can add it to those smaller sums to build all sums in [0,miss+num).
39-
* If we don't, then we must add such a number to the array,
40-
* and it's best to add miss itself, to maximize the reach.
33+
public static class Solution1 {
34+
/**
35+
* credit: https://leetcode.com/articles/patching-array/ and https://discuss.leetcode.com/topic/35494/solution-explanation/2
36+
*
37+
* Let miss be the smallest sum in [0,n] that we might be missing. Meaning we already know we
38+
* can build all sums in [0,miss). Then if we have a number num <= miss in the given array, we
39+
* can add it to those smaller sums to build all sums in [0,miss+num). If we don't, then we must
40+
* add such a number to the array, and it's best to add miss itself, to maximize the reach.
41+
*
42+
* Example: Let's say the input is nums = [1, 2, 4, 13, 43] and n = 100. We need to ensure that
43+
* all sums in the range [1,100] are possible. Using the given numbers 1, 2 and 4, we can
44+
* already build all sums from 0 to 7, i.e., the range [0,8). But we can't build the sum 8, and
45+
* the next given number (13) is too large. So we insert 8 into the array. Then we can build all
46+
* sums in [0,16). Do we need to insert 16 into the array? No! We can already build the sum 3,
47+
* and adding the given 13 gives us sum 16. We can also add the 13 to the other sums, extending
48+
* our range to [0,29). And so on. The given 43 is too large to help with sum 29, so we must
49+
* insert 29 into our array. This extends our range to [0,58). But then the 43 becomes useful
50+
* and expands our range to [0,101). At which point we're done.
51+
*/
4152

42-
Example: Let's say the input is nums = [1, 2, 4, 13, 43] and n = 100.
43-
We need to ensure that all sums in the range [1,100] are possible.
44-
Using the given numbers 1, 2 and 4, we can already build all sums from 0 to 7, i.e., the range [0,8).
45-
But we can't build the sum 8, and the next given number (13) is too large.
46-
So we insert 8 into the array. Then we can build all sums in [0,16).
47-
Do we need to insert 16 into the array? No! We can already build the sum 3,
48-
and adding the given 13 gives us sum 16.
49-
We can also add the 13 to the other sums, extending our range to [0,29).
50-
And so on. The given 43 is too large to help with sum 29, so we must insert 29 into our array.
51-
This extends our range to [0,58).
52-
But then the 43 becomes useful and expands our range to [0,101).
53-
At which point we're done.*/
54-
55-
public int minPatches(int[] nums, int n) {
56-
long misses = 1;//use long to avoid integer addition overflow
57-
int patches = 0;
58-
int i = 0;
59-
while (misses <= n) {
60-
if (i < nums.length && nums[i] <= misses) { //miss is covered
61-
misses += nums[i++];
62-
} else { //patch miss to the array
63-
misses += misses;
64-
patches++;//increase the answer
53+
public int minPatches(int[] nums, int n) {
54+
long misses = 1;//use long to avoid integer addition overflow
55+
int patches = 0;
56+
int i = 0;
57+
while (misses <= n) {
58+
if (i < nums.length && nums[i] <= misses) { //miss is covered
59+
misses += nums[i++];
60+
} else { //patch miss to the array
61+
misses += misses;
62+
patches++;//increase the answer
63+
}
6564
}
65+
return patches;
6666
}
67-
return patches;
68-
}
6967

70-
public List<Integer> findPatches(int[] nums, int n) {
71-
long misses = 1;//use long to avoid integer addition overflow
72-
List<Integer> patches = new ArrayList<>();
73-
int i = 0;
74-
while (misses <= n) {
75-
if (i < nums.length && nums[i] <= misses) { //miss is covered
76-
misses += nums[i++];
77-
} else { //patch miss to the array
78-
patches.add((int) misses);//increase the answer
79-
misses += misses;
68+
public List<Integer> findPatches(int[] nums, int n) {
69+
long misses = 1;//use long to avoid integer addition overflow
70+
List<Integer> patches = new ArrayList<>();
71+
int i = 0;
72+
while (misses <= n) {
73+
if (i < nums.length && nums[i] <= misses) { //miss is covered
74+
misses += nums[i++];
75+
} else { //patch miss to the array
76+
patches.add((int) misses);//increase the answer
77+
misses += misses;
78+
}
8079
}
80+
return patches;
8181
}
82-
return patches;
8382
}
8483

8584
}

src/test/java/com/fishercoder/_330Test.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,20 @@
1010

1111
import static org.junit.Assert.assertEquals;
1212

13-
/**
14-
* Created by stevesun on 6/12/17.
15-
*/
1613
public class _330Test {
17-
private static _330 test;
18-
private static int[] nums;
14+
private static _330.Solution1 solution1;
15+
private static int[] nums;
1916

20-
@BeforeClass
21-
public static void setup() {
22-
test = new _330();
23-
}
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _330.Solution1();
20+
}
2421

25-
@Test
26-
public void test1() {
27-
nums = new int[]{1, 2, 4, 13, 43};
28-
List<Integer> expected = new ArrayList(Arrays.asList(8, 29));
29-
assertEquals(expected, test.findPatches(nums, 100));
30-
assertEquals(2, test.minPatches(nums, 100));
31-
}
22+
@Test
23+
public void test1() {
24+
nums = new int[] {1, 2, 4, 13, 43};
25+
List<Integer> expected = new ArrayList(Arrays.asList(8, 29));
26+
assertEquals(expected, solution1.findPatches(nums, 100));
27+
assertEquals(2, solution1.minPatches(nums, 100));
28+
}
3229
}

0 commit comments

Comments
 (0)