Skip to content

1798. Maximum Number of Consecutive Values You Can Make 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(\texttt{sort})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  int getMaximumConsecutive(vector<int>& coins) {
    int ans = 1;  // the next value we want to make

    ranges::sort(coins);

    for (const int coin : coins) {
      if (coin > ans)
        return ans;
      ans += coin;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
  public int getMaximumConsecutive(int[] coins) {
    int ans = 1; // the next value we want to make

    Arrays.sort(coins);

    for (final int coin : coins) {
      if (coin > ans)
        return ans;
      ans += coin;
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
  def getMaximumConsecutive(self, coins: list[int]) -> int:
    ans = 1  # the next value we want to make

    for coin in sorted(coins):
      if coin > ans:
        return ans
      ans += coin

    return ans