美文网首页Leetcode
Leetcode 1785. Minimum Elements

Leetcode 1785. Minimum Elements

作者: SnailTyan | 来源:发表于2021-08-30 13:39 被阅读0次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Minimum Elements to Add to Form a Given Sum

2. Solution

解析:Version 1,根据题意,需要先数组求和,然后再求跟目标的差值,为了添加最少数量的数字,因此每次都应添加极限数值,题目变成了算除法并对余数取整。

  • Version 1
class Solution:
    def minElements(self, nums: List[int], limit: int, goal: int) -> int:
        total = sum(nums)
        diff = goal - total
        count = ceil(abs(diff) / limit)
        return count

Reference

  1. https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/

相关文章

网友评论

    本文标题:Leetcode 1785. Minimum Elements

    本文链接:https://www.haomeiwen.com/subject/mdwuvltx.html