-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.cpp
51 lines (45 loc) · 964 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
int minPatches(vector<int>& nums, int n) {
long curr = 0;
int count = 0;
int i = 0;
while (curr < n) {
if (nums.size() == 0 ||
i == nums.size() ||
(i < nums.size() && nums[i] > curr + 1)) {
count++;
curr = curr * 2 + 1;
} else {
curr += nums[i];
i++;
}
}
return count;
}
};
int main() {
Solution sol;
vector<int> nums = {1, 3};
cout << sol.minPatches(nums, 6) << endl;
nums = {1, 2, 2};
cout << sol.minPatches(nums, 5) << endl;
nums = {};
cout << sol.minPatches(nums, 7) << endl;
nums = {1, 2, 31, 33};
cout << sol.minPatches(nums, 2147483647) << endl;
return 0;
}