Skip to content

Commit 0e91ab3

Browse files
authored
Create 330. Patching Array (#505)
2 parents 34dbf3a + 1253129 commit 0e91ab3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

330. Patching Array

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int minPatches(vector<int>& nums, int n) {
4+
int patches = 0;
5+
int index = 0;
6+
long long nextSum = 1;
7+
8+
while (nextSum <= n) {
9+
if (index < nums.size() && nums[index] <= nextSum) {
10+
nextSum += nums[index++];
11+
} else {
12+
nextSum += nextSum;
13+
patches++;
14+
}
15+
}
16+
17+
return patches;
18+
}
19+
};

0 commit comments

Comments
 (0)