Leetcode 1798: Maximum Number of Consecutive Values You Can Make

grid47
grid47
Exploring patterns and algorithms
May 11, 2024 4 min read

You are given an integer array ‘coins’, where each element represents the value of a coin you own. You can use these coins to create sums of values starting from 0. Your task is to find the maximum number of consecutive integer values starting from 0 that you can make with your coins.
Problem
Approach
Steps
Complexity
Input: You are given an integer array 'coins' where each coin is represented by an integer value. You must calculate the maximum consecutive sum of values that can be made using these coins.
Example: coins = [2, 3]
Constraints:
• 1 <= n <= 4 * 10^4
• 1 <= coins[i] <= 4 * 10^4
Output: Return the maximum number of consecutive integer values starting from 0 that you can make using the given coins.
Example: For coins = [2, 3], the output will be 4.
Constraints:
Goal: The goal is to calculate the maximum consecutive integers starting from 0 that can be made by summing various combinations of coin values.
Steps:
• 1. Sort the array of coins.
• 2. Initialize a variable 'res' to 1, representing the initial sum 0.
• 3. Loop through the sorted array of coins. For each coin, check if it can extend the consecutive sums. If so, add it to 'res'.
Goal: The problem has constraints regarding the array size and the coin values.
Steps:
• coins.length == n
• 1 <= n <= 4 * 10^4
• 1 <= coins[i] <= 4 * 10^4
Assumptions:
• You can have multiple coins of the same value.
Input: coins = [2, 3]
Explanation: The coins are sorted to [2, 3]. The values that can be made starting from 0 are 0, 2, 3, and 5, giving us 4 consecutive sums.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus