LeetCode每日一题(1798. Maximum Number of Consecutive Values You Can Make)

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

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
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值