采集果实

标签: 数组

难度: Easy

欢迎各位勇者来到力扣新手村,本次训练内容为「采集果实」。 在新手村中,各位勇者需要采集一些果实来制作药剂。`time[i]` 表示勇者每次采集 `1~limit` 颗第 `i` 种类型的果实需要的时间(即每次最多可以采集 `limit` 颗果实)。 当前勇者需要完成「采集若干批果实」的任务, `fruits[j] = [type, num]` 表示第 `j` 批需要采集 `num` 颗 `type` 类型的果实。采集规则如下: - 按 `fruits` 给定的顺序**依次**采集每一批次 - 采集完当前批次的果实才能开始采集下一批次 - 勇者完成当前批次的采集后将**清空背包**(即多余的果实将清空) 请计算并返回勇者完成采集任务最少需要的时间。 **示例 1:** >输入:`time = [2,3,2], fruits = [[0,2],[1,4],[2,1]], limit = 3` > >输出:`10` > >解释: >由于单次最多采集 3 颗 >第 0 批需要采集 2 颗第 0 类型果实,需要采集 1 次,耗时为 2\*1=2 >第 1 批需要采集 4 颗第 1 类型果实,需要采集 2 次,耗时为 3\*2=6 >第 2 批需要采集 1 颗第 2 类型果实,需要采集 1 次,耗时为 2\*1=2 >返回总耗时 2+6+2=10 **示例 2:** >输入:`time = [1], fruits = [[0,3],[0,5]], limit = 2` > >输出:`5` > >解释: >由于单次最多采集 2 颗 >第 0 批需要采集 3 颗第 0 类型果实,需要采集 2 次,耗时为 1\*2=2 >第 1 批需要采集 5 颗第 0 类型果实,需要采集 3 次,耗时为 1\*3=3 >需按照顺序依次采集,返回 2+3=5 **提示:** - `1 <= time.length <= 100` - `1 <= time[i] <= 100` - `1 <= fruits.length <= 10^3` - `0 <= fruits[i][0] < time.length` - `1 <= fruits[i][1] < 10^3` - `1 <= limit <= 100`

Submission

运行时间: 34 ms

内存: 16.4 MB

class Solution:
    def getMinimumTime(self, time: List[int], fruits: List[List[int]], limit: int) -> int:
        ans = 0
        for fruit, number in fruits:
            ans += ceil(number / limit) * time[fruit]
        return ans

Explain

The solution iterates through each batch of fruits specified in the 'fruits' list. For each batch, it calculates the number of times the hero needs to collect fruits of that type to meet the required quantity. This is done by dividing the number of fruits needed ('number') by the maximum number of fruits that can be collected in one go ('limit'), and taking the ceiling of the result to ensure that we account for any remaining fruits that don't make up a full 'limit'. The time taken for each batch is then calculated by multiplying the number of times the hero needs to collect fruits by the time it takes to collect one batch of fruits of that type ('time[fruit]'). The total time taken ('ans') is the sum of the times taken for all batches.

时间复杂度: O(n)

空间复杂度: O(1)

class Solution:
    def getMinimumTime(self, time: List[int], fruits: List[List[int]], limit: int) -> int:
        ans = 0  # Initialize the total time taken to 0
        for fruit, number in fruits:  # Iterate through each batch of fruits
            ans += ceil(number / limit) * time[fruit]  # Calculate and add the time taken for the current batch
        return ans  # Return the total time taken

Explore

当`num`(需要采集的果实数量)不是`limit`(每次可以采集的最大数量)的整数倍时,会有一些多余的果实数量无法恰好组成一个完整的`limit`批次。在这种情况下,使用向上取整的方式(ceil函数),保证即使是超出`limit`的部分也被计算为一个完整的采集周期。例如,如果`num`为10,`limit`为3,计算得到的采集次数为`ceil(10/3) = 4`次。这确保了所有果实都被采集完毕,包括最后一次可能不满`limit`的采集。

使用向上取整的方式(ceil函数)是为了确保所有的果实都能被采集完毕。例如,如果一个批次需要采集的果实数除以每次采集的限制数(limit),结果不是一个整数,表示最后一次采集不会达到完整的限制数,但这些果实仍然需要被采集。向上取整能保证即使最后一次采集果实数少于`limit`,也会被算作一次完整的采集过程,从而保障所有果实都被收集。

在算法中,`time`数组是一个整数列表,每个元素代表对应类型果实的采集时间。`fruits`数组是一个二维列表,其中每个子列表包含两个元素:果实的类型(fruit)和需要采集的数量(number)。`fruit`的值作为索引直接用于从`time`数组中获取对应类型果实的采集时间。例如,如果`fruits`中某个元素为`[1, 5]`,则表示类型1的果实需要采集5个,其采集时间由`time[1]`提供。