-
Notifications
You must be signed in to change notification settings - Fork 0
/
A.cpp
42 lines (36 loc) · 832 Bytes
/
A.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
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int coins9(vector<int>& coins) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int coin;
cin >> coin;
coins.push_back(coin);
}
int total_sum = 0;
for (int coin : coins) {
total_sum += coin;
}
int target_sum = total_sum / 2;
sort(coins.begin(), coins.end(), greater<int>());
int my_sum = 0;
int count = 0;
for (int coin : coins) {
my_sum += coin;
count++;
if (my_sum > total_sum - my_sum) {
return count;
}
}
return count; // return the minimum number of coins
}
};
int main() {
Solution solution;
vector<int> coins;
cout << solution.coins9(coins)<< endl;
return 0;
}