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]. You can make some value x if you can choose some of your n coins such that their values sum up to x.
Return the maximum number of consecutive integer values that you can make with your coins starting from and including 0.
Note that you may have multiple coins of the same value.
Example 1:
Input: coins = [1,3]
Output: 2
Explanation: You can make the following values:
- 0: take []
- 1: take [1]
You can make 2 consecutive integer values starting from 0.
Example 2:
Input: coins = [1,1,1,4]
Output: 8
Explanation: You can make the following values:
- 0: take []
- 1: take [1]
- 2: take [1,1]
- 3: take [1,1,1]
- 4: take [4]
- 5: take [4,1]
- 6: take [4,1,1]
- 7: take [4,1,1,1]
You can make 8 consecutive integer values starting from 0.
Example 3:
Input: nums = [1,4,10,3,1]
Output: 20
Constraints:
coins.length == n
- 1 <= n <= 4 * 104
- 1 <= coins[i] <= 4 * 104
res 表示下一个要合成的数, 假设当前的 coin 的值为 c, 如果 c <= res, 则我们可以组成[c + 0, c + 1, c + 2, …, c + res - 1], 所以我们下一个要合成的数就变成了 c + res。如果 c > res 那我们至少不能合成当前的 res, 因为 c + 0 > res, 所以能组合出的最大的连续数字数量就是当前的 res
impl Solution {
pub fn get_maximum_consecutive(mut coins: Vec<i32>) -> i32 {
coins.sort();
let mut res = 1;
for &c in &coins {
if c <= res {
res += c;
continue;
}
break;
}
res
}
}
使用硬币组合形成的最长连续整数序列

给定一个整数数组coins,表示你拥有的硬币类型。返回你可以用这些硬币形成的最长连续整数序列的长度,从0开始。你可以多次使用同一面额的硬币。例如,对于coins=[1,3],可以形成[0,1,2,3],所以返回2。"
121987262,11617041,计算机组成原理模拟试题,"['计算机硬件', '计算机组成', '试题', '考试复习']
568

被折叠的 条评论
为什么被折叠?



