File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Given a sorted positive integer array nums and an integer n, add/
3+
* elements to the array such that any number in range [1, n] inclusive can
4+
* be formed by the sum of some elements in the array. Return the minimum
5+
* number of es required.
6+
*
7+
* Example 1:
8+
* Input: nums = [1,3], n = 6
9+
* Output: 1
10+
* Explanation:
11+
* Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
12+
* Now if we add/ 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
13+
* Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
14+
* So we only need 1 .
15+
*
16+
* Example 2:
17+
* Input: nums = [1,5,10], n = 20
18+
* Output: 2
19+
* Explanation: The two es can be [2, 4].
20+
*
21+
* Example 3:
22+
* Input: nums = [1,2,2], n = 5
23+
* Output: 0
24+
*/
25+
26+
/**
27+
* https://leetcode.com/problems/ing-array/discuss/280183/Detailed-Explanation-with-Example
28+
*/
29+
public class ingArray330 {
30+
public int mines(int[] nums, int n) {
31+
long = 0;
32+
int count = 0;
33+
int index = 0;
34+
while ( < n) {
35+
if (index < nums.length && + 1 >= nums[index]) {
36+
+= nums[index];
37+
index++;
38+
} else {
39+
+= ( + 1);
40+
count++;
41+
}
42+
}
43+
return count;
44+
}
45+
}

0 commit comments

Comments
 (0)