Maximum Number of Consecutive Values You Can Make — LeetCode 1798 Python Solution
MediumGreedyArraySorting
- Problem
- #1798
- Pattern
- Greedy
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given an integer array coins of length n which represents the n coins that you own. The value of the ith coin is coins[i].
Example
- Input
- coins = [1,3]
- Output
- 2
- Explanation
- You can make the following values:
Python solution
Python
class Solution:
def getMaximumConsecutive(self, coins: List[int]) -> int:
ans = 1
for v in sorted(coins):
if v > ans:
break
ans += v
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n \times \log n) |
| Space | O(\log n) auxiliary |
Pattern: Greedy
Take the locally best option every time — when you can prove that never costs you later. LeetCode 1798. Maximum Number of Consecutive Values You Can Make is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Greedy.
The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1798. Maximum Number of Consecutive Values You Can Make?
- LeetCode 1798. Maximum Number of Consecutive Values You Can Make is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1798. Maximum Number of Consecutive Values You Can Make?
- The Python solution on this page runs in O(n \times \log n).
- What is the space complexity of LeetCode 1798. Maximum Number of Consecutive Values You Can Make?
- The Python solution on this page uses O(\log n) auxiliary space.
- What topics does LeetCode 1798. Maximum Number of Consecutive Values You Can Make cover?
- LeetCode 1798. Maximum Number of Consecutive Values You Can Make is tagged Greedy, Array and Sorting on LeetCode.