Compare commits
10 Commits
5dbc1f093a
...
1176c423e8
Author | SHA1 | Date | |
---|---|---|---|
1176c423e8 | |||
af4efbce2f | |||
1db371407c | |||
5e99323cdc | |||
2df78dec1d | |||
f455d54fe0 | |||
ba57817c74 | |||
cc9b311081 | |||
f459721c1f | |||
b3d15f5983 |
125
1038-231204-pass/main.cpp
Normal file
125
1038-231204-pass/main.cpp
Normal file
@ -0,0 +1,125 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
priority_queue<int, vector<int>, greater<int> > vals;
|
||||
int cnt = 0;
|
||||
void traverse(TreeNode * node){
|
||||
vals.push(node->val);
|
||||
cnt++;
|
||||
if(node->left!=nullptr) traverse(node->left);
|
||||
if(node->right!=nullptr) traverse(node->right);
|
||||
}
|
||||
TreeNode* bstToGst(TreeNode* root) {
|
||||
traverse(root);
|
||||
int val[110];
|
||||
int idx[110];
|
||||
int pref[110],sum=0;
|
||||
for(int i = 0 ; i < cnt ; i++){
|
||||
val[i] = vals.top();
|
||||
vals.pop();
|
||||
if(i==0)
|
||||
pref[i] = val[i];
|
||||
else{
|
||||
pref[i] = pref[i-1] + val[i];
|
||||
}
|
||||
}
|
||||
sum = pref[cnt-1];
|
||||
for(int i = 0 ; i < cnt ; i++){
|
||||
idx[val[i]] = sum - pref[i];
|
||||
}
|
||||
stack<TreeNode*> sta;
|
||||
sta.push(root);
|
||||
while(sta.empty() == false){
|
||||
TreeNode* top_node = sta.top();
|
||||
cout<<"top_node is "<<top_node->val<<endl;
|
||||
sta.pop();
|
||||
if(top_node->left!=nullptr) sta.push(top_node->left);
|
||||
if(top_node->right!=nullptr) sta.push(top_node->right);
|
||||
top_node->val+=idx[top_node->val];
|
||||
}
|
||||
cnt = 0;
|
||||
std::priority_queue<int, std::vector<int>, std::greater<int>> empty;
|
||||
vals.swap(empty);
|
||||
return root;
|
||||
}
|
||||
};
|
||||
|
||||
void create(int * val, int cnt, TreeNode *cur,int idx){
|
||||
int left_idx = (idx + 1) * 2 - 1;
|
||||
int right_idx = (idx + 1) * 2 ;
|
||||
if(left_idx > cnt){
|
||||
cur->left = nullptr;
|
||||
cur->right = nullptr;
|
||||
return;
|
||||
}
|
||||
if(right_idx > cnt){
|
||||
cur->right = nullptr;
|
||||
return;
|
||||
}
|
||||
if(val[left_idx] == -1){
|
||||
cur -> left = nullptr;
|
||||
}else{
|
||||
TreeNode * left_child = new TreeNode(val[left_idx]);
|
||||
cur->left = left_child;
|
||||
create(val,cnt,left_child,left_idx);
|
||||
}
|
||||
if(val[right_idx] == -1){
|
||||
cur -> right = nullptr;
|
||||
}else{
|
||||
TreeNode * right_child = new TreeNode(val[right_idx]);
|
||||
cur->right = right_child;
|
||||
create(val, cnt, right_child, right_idx);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void traverse(TreeNode* node){
|
||||
cout<<node->val<<' ';
|
||||
if(node->left != nullptr){
|
||||
traverse(node->left);
|
||||
}
|
||||
if(node->right != nullptr){
|
||||
traverse(node->right);
|
||||
}
|
||||
return;
|
||||
}
|
||||
TreeNode* createTree(int * val, int cnt){
|
||||
TreeNode * root = new TreeNode(val[0]);
|
||||
cout<<"create a tree"<<endl;
|
||||
create(val,cnt,root, 0);
|
||||
traverse(root);
|
||||
cout<<"\ndone"<<endl;
|
||||
// cout<<endl;
|
||||
return root;
|
||||
}
|
||||
int main(){
|
||||
Solution sol;
|
||||
|
||||
//create example 1
|
||||
const int ex1_length = 15;
|
||||
int ex1_data[ex1_length] = {4,1,6,0,2,5,7,-1,-1,-1,3,-1,-1,-1,8};
|
||||
TreeNode * ex1 = createTree(ex1_data, ex1_length);
|
||||
sol.bstToGst(ex1);
|
||||
traverse(ex1);
|
||||
cout<<endl;
|
||||
|
||||
const int ex2_length = 3;
|
||||
int ex2_data[ex2_length] = {0,-1,1};
|
||||
TreeNode * ex2 = createTree(ex2_data,ex2_length);
|
||||
sol.bstToGst(ex2);
|
||||
traverse(ex2);
|
||||
cout<<endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
47
1094-20231203-pass/main.cpp
Normal file
47
1094-20231203-pass/main.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
class Solution {
|
||||
public:
|
||||
bool carPooling(vector<vector<int>>& trips, int capacity) {
|
||||
int len = trips.size();
|
||||
struct op{
|
||||
bool tp;
|
||||
int people;
|
||||
int station;
|
||||
};
|
||||
vector<op> ops(trips.size()*2+10);
|
||||
for(int i = 0 ; i < trips.size() ;i++){
|
||||
op in = {true,trips[i][0],trips[i][1]};
|
||||
op out = {false,trips[i][0],trips[i][2]};
|
||||
ops[i*2] = in;
|
||||
ops[i*2+1] = out;
|
||||
}
|
||||
sort(ops.begin(), ops.end(), [](const op a, const op& b){
|
||||
return a.station<b.station||(a.station == b.station && a.tp < b.tp);
|
||||
});
|
||||
int peo = 0;
|
||||
for(int i = 0 ; i < ops.size() ; i++){
|
||||
if(!ops[i].tp){
|
||||
peo-=ops[i].people;
|
||||
}
|
||||
if(ops[i].tp){
|
||||
peo+=ops[i].people;
|
||||
if(peo>capacity)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
Solution sol;
|
||||
vector<int> sta1 = {2,1,5};
|
||||
vector<int> sta2 = {3,3,7};
|
||||
vector<vector<int> >ex1 = {sta1,sta2};
|
||||
cout<<sol.carPooling(ex1, 4)<<endl;
|
||||
cout<<sol.carPooling(ex1, 5)<<endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
25
125-240524-pass/main.py
Normal file
25
125-240524-pass/main.py
Normal file
@ -0,0 +1,25 @@
|
||||
def isPalindrome(s: str) -> bool:
|
||||
tmp = s
|
||||
tmp = tmp.lower()
|
||||
def is_char(ch):
|
||||
if (ch >= 'a' and ch <= 'z') or (ch >= '0' and ch <= '9'):
|
||||
return True
|
||||
return False
|
||||
exp = []
|
||||
for lt in tmp:
|
||||
if is_char(lt):
|
||||
exp.append(lt)
|
||||
length = len(exp)
|
||||
for index, ele in enumerate(exp):
|
||||
if(exp[index] != exp[length - index - 1]):
|
||||
return False
|
||||
return True
|
||||
|
||||
s = 'A man, a plan a canal: Panama'
|
||||
print(isPalindrome(s))
|
||||
s = " "
|
||||
print(isPalindrome(s))
|
||||
s = "race a car"
|
||||
print(isPalindrome(s))
|
||||
s = "0P"
|
||||
print(isPalindrome(s))
|
69
1423-231205-pass/main.cpp
Normal file
69
1423-231205-pass/main.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
class Solution {
|
||||
public:
|
||||
int maxScore1(vector<int>& cardPoints, int k) {
|
||||
const int len = 1e5+10;
|
||||
const int siz = cardPoints.size();
|
||||
int h2t_state[len][2]= {}, t2h_state[len][2] = {};
|
||||
h2t_state[0][1] = cardPoints[0];
|
||||
t2h_state[siz-1][1] = cardPoints[siz - 1];
|
||||
for(int i = 2 ; i <= k ; i++){
|
||||
int idx = i%2;
|
||||
for(int j = 0 ; j < i ; j++){
|
||||
// h2t_state[j][idx] = t2h_state[siz-j-1][1-idx] + cardPoints[j];
|
||||
// t2h_state[siz-j-1][idx] = h2t_state[j][1-idx] + cardPoints[siz-j-1];
|
||||
if(j == 0){
|
||||
h2t_state[j][idx] = t2h_state[siz - i + 1][1-idx] + cardPoints[j];
|
||||
t2h_state[siz-j-1][idx] = h2t_state[ i - 2 ][1-idx] + cardPoints[siz-j-1];
|
||||
}else{
|
||||
h2t_state[j][idx] = h2t_state[j-1][1-idx] + cardPoints[j];
|
||||
t2h_state[siz-j-1][idx] = t2h_state[siz-j][1-idx] + cardPoints[siz-j-1];
|
||||
}
|
||||
}
|
||||
// cout<<"i: "<<i<<endl;
|
||||
// for(int j = 0 ; j < siz ; j++){
|
||||
// cout<<h2t_state[j][idx]<<' '<<t2h_state[j][idx]<<' ';
|
||||
// cout<<h2t_state[j][1-idx]<<' '<<t2h_state[j][1-idx]<<endl;
|
||||
// }
|
||||
// cout<<"======"<<endl;
|
||||
}
|
||||
// cout<<"++++++++"<<endl;
|
||||
int rlt = -1;
|
||||
for(int i = 0 ; i < siz ; i++){
|
||||
rlt = max(rlt,h2t_state[i][k%2]);
|
||||
rlt = max(rlt,t2h_state[i][k%2]);
|
||||
// cout<<h2t_state[i][k%2]<<' '<<t2h_state[i][k%2]<<endl;
|
||||
}
|
||||
// cout<<"done"<<endl;
|
||||
return rlt;
|
||||
}
|
||||
int maxScore(vector<int>& cardPoints, int k){
|
||||
int sum = 0;
|
||||
int ans = 0;
|
||||
int sz = cardPoints.size();
|
||||
for(int i = 0 ; i < k ; i++) sum+=cardPoints[i];
|
||||
ans = sum;
|
||||
for(int i = 0 ; i < k ; i++){
|
||||
sum-=cardPoints[k - i - 1];
|
||||
sum+=cardPoints[sz - 1 - i];
|
||||
ans = max(ans, sum);
|
||||
cout<<sum<<endl;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
Solution sol;
|
||||
vector<int> ex1 = {1,2,3,4,5,6,1};
|
||||
int k1 = 3;
|
||||
cout<<sol.maxScore(ex1,k1)<<endl;
|
||||
|
||||
vector<int> ex2 = {2,2,2};
|
||||
int k2 = 2;
|
||||
cout<<sol.maxScore(ex2,k2)<<endl;
|
||||
|
||||
vector<int> ex3 = {9,7,7,9,7,7,9};
|
||||
int k3 = 7;
|
||||
cout<<sol.maxScore(ex3,k3)<<endl;
|
||||
}
|
24
1688-20231205-pass/main.cpp
Normal file
24
1688-20231205-pass/main.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
class Solution{
|
||||
public:
|
||||
int numberOfMatches(int n){
|
||||
int sum = 0;
|
||||
while(n!=1){
|
||||
sum+=n/2;
|
||||
if(n%2) n+=1;
|
||||
n/=2;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
Solution sol;
|
||||
int n;
|
||||
n = 7;
|
||||
cout<<sol.numberOfMatches(n)<<endl;
|
||||
n = 14;
|
||||
cout<<sol.numberOfMatches(n)<<endl;
|
||||
return 0;
|
||||
}
|
31
1716-20231206-pass/main.cpp
Normal file
31
1716-20231206-pass/main.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
class Solution{
|
||||
public:
|
||||
int totalMoney(int n){
|
||||
int monday = 0;
|
||||
int rlt = 0;
|
||||
for(int i = 1 ; i<=n ;i++){
|
||||
if(i%7==1){
|
||||
monday = monday + 1;
|
||||
rlt += monday;
|
||||
cout<<monday<<endl;
|
||||
}else{
|
||||
int amount = i%7;
|
||||
if(!amount) amount = 7;
|
||||
cout<< monday + amount - 1 <<endl;
|
||||
rlt += monday + amount - 1 ;
|
||||
|
||||
}
|
||||
}
|
||||
cout<<endl;
|
||||
return rlt;
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
int n;
|
||||
Solution sol;
|
||||
cout<<sol.totalMoney(4)<<endl;
|
||||
cout<<sol.totalMoney(10)<<endl;
|
||||
cout<<sol.totalMoney(20)<<endl;
|
||||
}
|
22
1903-20231207-pass/main.cpp
Normal file
22
1903-20231207-pass/main.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
class Solution{
|
||||
public:
|
||||
string largestOddNumber(string num){
|
||||
string rlt = "";
|
||||
for(int i = num.size() - 1 ; i >= 0 ; i--){
|
||||
if(num[i]=='1' || num[i] == '3' || num[i] == '5' || num[i] == '7' || num[i] == '9'){
|
||||
rlt = num.substr(0,i+1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return rlt;
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
Solution sol;
|
||||
cout<<sol.largestOddNumber("52")<<endl;
|
||||
cout<<sol.largestOddNumber("2480")<<endl;
|
||||
cout<<sol.largestOddNumber("35247")<<endl;
|
||||
return 0;
|
||||
}
|
55
2477-20231205-pass/main.cpp
Normal file
55
2477-20231205-pass/main.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
class Solution{
|
||||
public:
|
||||
int people_counter(
|
||||
vector<vector<int> > nodes, int k, int prev, long long & ans, int seats
|
||||
){
|
||||
int people = 1;
|
||||
for(int node : nodes[k]){
|
||||
if(node == prev){continue;}
|
||||
people+=people_counter(nodes,node,k, ans, seats);
|
||||
}
|
||||
if(k > 0)
|
||||
ans += (people+seats - 1)/seats;
|
||||
return people;
|
||||
}
|
||||
long long minimumFuelCost(vector<vector<int> >& roads, int seats){
|
||||
vector<vector<int> >nodes(roads.size() + 1);
|
||||
for(vector<int> edge: roads){
|
||||
const int u = edge[0], v = edge[1];
|
||||
nodes[u].push_back(v);
|
||||
nodes[v].push_back(u);
|
||||
}
|
||||
long long ans = 0;
|
||||
people_counter(nodes,0, -1, ans, seats);
|
||||
cout<<ans<<endl;
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
Solution sol;
|
||||
vector<int> rd1 = {3,1};
|
||||
vector<int> rd2 = {3,2};
|
||||
vector<int> rd3 = {1,0};
|
||||
vector<int> rd4 = {0,4};
|
||||
vector<int> rd5 = {0,5};
|
||||
vector<int> rd6 = {4,6};
|
||||
vector<vector<int> >ex1 = {rd1,rd2,rd3,rd4,rd5,rd6};
|
||||
sol.minimumFuelCost(ex1, 2);
|
||||
|
||||
rd1[0]=0; rd1[1] = 1;
|
||||
rd2[0]=0; rd2[1] = 2;
|
||||
rd3[0]=0; rd3[1] = 3;
|
||||
vector<vector<int> >ex2 = {rd1,rd2,rd3};
|
||||
|
||||
sol.minimumFuelCost(ex2, 5);
|
||||
|
||||
vector<vector<int> >ex3 ;
|
||||
sol.minimumFuelCost(ex3,1);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
94
606-20231208-pass/main.cpp
Normal file
94
606-20231208-pass/main.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
};
|
||||
void create(int * val, int cnt, TreeNode *cur,int idx){
|
||||
int left_idx = (idx + 1) * 2 - 1;
|
||||
int right_idx = (idx + 1) * 2 ;
|
||||
if(left_idx >= cnt && right_idx >= cnt) {
|
||||
cur->left = nullptr;
|
||||
cur->right = nullptr;
|
||||
return;
|
||||
}
|
||||
if(val[left_idx] == -1 || left_idx >= cnt){
|
||||
cur -> left = nullptr;
|
||||
}else{
|
||||
TreeNode * left_child = new TreeNode(val[left_idx]);
|
||||
cur->left = left_child;
|
||||
create(val,cnt,left_child,left_idx);
|
||||
}
|
||||
if(val[right_idx] == -1 || right_idx >= cnt){
|
||||
cur -> right = nullptr;
|
||||
}else{
|
||||
TreeNode * right_child = new TreeNode(val[right_idx]);
|
||||
cur->right = right_child;
|
||||
create(val, cnt, right_child, right_idx);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void traverse(TreeNode* node){
|
||||
cout<<node->val<<' ';
|
||||
if(node->left != nullptr){
|
||||
traverse(node->left);
|
||||
}
|
||||
if(node->right != nullptr){
|
||||
traverse(node->right);
|
||||
}
|
||||
return;
|
||||
}
|
||||
TreeNode* createTree(int * val, int cnt){
|
||||
TreeNode * root = new TreeNode(val[0]);
|
||||
cout<<"create a tree"<<endl;
|
||||
create(val,cnt,root, 0);
|
||||
traverse(root);
|
||||
cout<<"\ndone"<<endl;
|
||||
return root;
|
||||
}
|
||||
|
||||
class Solution{
|
||||
public:
|
||||
string traverse(TreeNode * node, string &str){
|
||||
str.push_back((char)('0'+node->val));
|
||||
if(node -> left == nullptr && node -> right == nullptr) return str;
|
||||
if(node -> left == nullptr){
|
||||
str.push_back('(');
|
||||
str.push_back(')');
|
||||
}else{
|
||||
str.push_back('(');
|
||||
traverse(node->left, str);
|
||||
str.push_back(')');
|
||||
}
|
||||
if(node -> right == nullptr){
|
||||
return str;
|
||||
}else{
|
||||
str.push_back('(');
|
||||
traverse(node->right, str);
|
||||
str.push_back(')');
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
string tree2str(TreeNode * root){
|
||||
string rlt = "";
|
||||
return traverse(root,rlt);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
Solution sol;
|
||||
int ex1_val[4] = {1,2,3,4};
|
||||
TreeNode * ex1 = createTree(ex1_val,4);
|
||||
cout<<sol.tree2str(ex1)<<endl;
|
||||
int ex2_val[5] = {1,2,3,-1,4};
|
||||
TreeNode * ex2 = createTree(ex2_val, 5);
|
||||
cout<<sol.tree2str(ex2)<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
42
88-231127-pass/main.py
Normal file
42
88-231127-pass/main.py
Normal file
@ -0,0 +1,42 @@
|
||||
def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
|
||||
"""
|
||||
Do not return anything, modify nums1 in-place instead.
|
||||
"""
|
||||
tmp = []
|
||||
for i in range(m):
|
||||
tmp.append(nums1[i])
|
||||
for num in nums2:
|
||||
tmp.append(num)
|
||||
|
||||
ind1 = 0
|
||||
ind2 = 0
|
||||
rlt = []
|
||||
if(m == 0 or n == 0):
|
||||
rlt += tmp
|
||||
else:
|
||||
for i in range(m + n):
|
||||
if(nums1[ind1]<= nums2[ind2]):
|
||||
rlt.append(nums1[ind1])
|
||||
ind1 += 1
|
||||
if(ind1 == m):
|
||||
rlt += nums2[ind2:]
|
||||
print(i, rlt)
|
||||
break
|
||||
else:
|
||||
rlt.append(nums2[ind2])
|
||||
ind2 += 1
|
||||
if(ind2 == n):
|
||||
rlt += nums1[ind1:]
|
||||
print(i, rlt)
|
||||
break;
|
||||
print(i, rlt)
|
||||
for i in range(len(rlt)):
|
||||
nums1[i] = rlt[i]
|
||||
|
||||
nums1 = [2,0]
|
||||
m = 1
|
||||
nums2 = [1]
|
||||
n = 1
|
||||
print(merge(0, nums1, m, nums2, n))
|
||||
|
||||
|
32
contest-20231203/2951/main.cpp
Normal file
32
contest-20231203/2951/main.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> findPeaks(vector<int>& mountain) {
|
||||
vector<int>rlt;
|
||||
const int len = mountain.size();
|
||||
for(int i = 1 ; i < mountain.size() - 1; i++){
|
||||
if(mountain[i] > mountain[i-1] && mountain[i] > mountain[i+1])
|
||||
rlt.push_back(i);
|
||||
}
|
||||
|
||||
return rlt;
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
Solution sol;
|
||||
|
||||
vector<int> ex1 = {2,4,4};
|
||||
vector<int>rlt1 = sol.findPeaks(ex1);
|
||||
for(int i = 0 ; i <rlt1.size();i++)
|
||||
cout<<rlt1[i]<<' ';
|
||||
cout<<endl;
|
||||
|
||||
vector<int> ex2 = {1,4,3,8,5};
|
||||
vector<int>rlt2 = sol.findPeaks(ex2);
|
||||
for(int i = 0 ; i <rlt2.size();i++)
|
||||
cout<<rlt2[i]<<' ';
|
||||
cout<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
42
contest-20231203/2952/main.cpp
Normal file
42
contest-20231203/2952/main.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include<stdcpp.h>
|
||||
using namespace std;
|
||||
class Solution {
|
||||
public:
|
||||
int minimumAddedCoins(vector<int>& coins, int target) {
|
||||
const int len = coins.size();
|
||||
sort(coins.begin(), coins.end());
|
||||
int express = 0;
|
||||
int idx = 0;
|
||||
int ans = 0;
|
||||
while(express < target){
|
||||
if(idx < len && express+1 >= coins[idx]){
|
||||
express += coins[idx++];
|
||||
}else{
|
||||
ans++;
|
||||
express += express + 1;
|
||||
}
|
||||
}
|
||||
cout<<ans<<endl;
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
Solution sol;
|
||||
|
||||
vector<int> ex1 = {1,4,10};
|
||||
int target1 = 19;
|
||||
sol.minimumAddedCoins(ex1,target1);
|
||||
|
||||
vector<int> ex2 = {1,4,10,5,7,19};
|
||||
int target2 = 19;
|
||||
sol.minimumAddedCoins(ex2,target2);
|
||||
|
||||
vector<int> ex3 = {1,1,1};
|
||||
int target3 = 20;
|
||||
sol.minimumAddedCoins(ex3,target3);
|
||||
|
||||
vector<int> ex4 = {1};
|
||||
int target4 = 100000;
|
||||
sol.minimumAddedCoins(ex4,target4);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user