Skip to content

Commit a0ae9e6

Browse files
committed
Time: 4 ms (67.05%), Space: 13.7 MB (82.46%) - LeetHub
1 parent 83e6609 commit a0ae9e6

File tree

1 file changed

+21
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)