LeetCode 1798. Maximum Number of Consecutive Values You Can Make

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int getMaximumConsecutive(vector<int>& coins) {
int r = 0; // start from [0, r].
sort(coins.begin(), coins.end());
for (int coin : coins) {
// to make [0, r] & [coin, coin + r] connected,
// coin should not be greater than 'r + 1'.
if (coin <= r + 1) r += coin;
else break;
}
return r + 1;
}
};