Skip to content

Commit 8c9eff8

Browse files
refactor 330
1 parent 4ee87aa commit 8c9eff8

File tree

1 file changed

+2
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+2
-27
lines changed

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

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,17 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 330. Patching Array
8-
*
9-
* Given a sorted positive integer array nums and an integer n,
10-
* add/patch elements to the array such that any number in range [1, n]
11-
* inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required.
12-
13-
Example 1:
14-
nums = [1, 3], n = 6
15-
Return 1.
16-
17-
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
18-
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
19-
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
20-
So we only need 1 patch.
21-
22-
Example 2:
23-
nums = [1, 5, 10], n = 20
24-
Return 2.
25-
The two patches can be [2, 4].
26-
27-
Example 3:
28-
nums = [1, 2, 2], n = 5
29-
Return 0.
30-
*/
316
public class _330 {
327

338
public static class Solution1 {
349
/**
3510
* credit: https://leetcode.com/articles/patching-array/ and https://discuss.leetcode.com/topic/35494/solution-explanation/2
36-
*
11+
* <p>
3712
* Let miss be the smallest sum in [0,n] that we might be missing. Meaning we already know we
3813
* can build all sums in [0,miss). Then if we have a number num <= miss in the given array, we
3914
* can add it to those smaller sums to build all sums in [0,miss+num). If we don't, then we must
4015
* add such a number to the array, and it's best to add miss itself, to maximize the reach.
41-
*
16+
* <p>
4217
* Example: Let's say the input is nums = [1, 2, 4, 13, 43] and n = 100. We need to ensure that
4318
* all sums in the range [1,100] are possible. Using the given numbers 1, 2 and 4, we can
4419
* already build all sums from 0 to 7, i.e., the range [0,8). But we can't build the sum 8, and

0 commit comments

Comments
 (0)