|
30 | 30 | */ |
31 | 31 | public class _330 { |
32 | 32 |
|
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 | + */ |
41 | 52 |
|
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 | + } |
65 | 64 | } |
| 65 | + return patches; |
66 | 66 | } |
67 | | - return patches; |
68 | | - } |
69 | 67 |
|
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 | + } |
80 | 79 | } |
| 80 | + return patches; |
81 | 81 | } |
82 | | - return patches; |
83 | 82 | } |
84 | 83 |
|
85 | 84 | } |
0 commit comments