Skip to content

Commit c42f953

Browse files
committed
Runtime 167 ms (Top 9.82%) | Memory 66.0 MB (Top 61.82%)
1 parent 20ea179 commit c42f953

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Solution {
22
public:
33
int getMaximumConsecutive(vector<int>& coins) {
4-
// support variables
5-
int res = 1;
6-
// preparing coins
7-
sort(begin(coins), end(coins));
8-
for (int coin: coins) {
9-
// checking if we could have reached coin without gaps
10-
if (res >= coin) res += coin;
11-
else break;
4+
sort(coins.begin(), coins.end());
5+
int n = coins.size();
6+
int ans = 1;
7+
for(int i = 0; i < n; i++) {
8+
if(coins[i] > ans) return ans;
9+
ans += coins[i];
1210
}
13-
return res;
11+
return ans;
1412
}
15-
};
13+
};

0 commit comments

Comments
 (0)