-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminPatchs.cpp
More file actions
42 lines (38 loc) · 788 Bytes
/
Copy pathminPatchs.cpp
File metadata and controls
42 lines (38 loc) · 788 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
// [1, 3], n = 6 => we need fill 2 , so ans is 1
// [1, 5, 10], n = 20 => we need fill [2, 4], so ans = 2
// get min patches that can fill number 1 ~ n
int minPatches(vector<int> nums, int n){
int sum = 0;
int index = 0;
int ans = 0;
if(nums.size() > 0 && nums[0]== 1){
sum = 1;
index = 1;
}
while(sum < n){
while(index < nums.size() && nums[index] <= sum){
sum += nums[index];
index++;
}
if(sum < n){
if(index < nums.size() && (sum+1) == nums[index]){
index++;
}
else{
ans++;
}
sum = 2 * sum + 1;
}
}
return ans;
}
int main(void){
vector<int> nums = {1,3};
cout<<minPatches(nums, 5)<<endl;
vector<int> nums2 = {1,5, 10};
cout<<minPatches(nums2, 20)<<endl;
return 0;
}