LeetCode 题解工作台

收集巧克力

给你一个长度为 n 、下标从 0 开始的整数数组 nums , nums[i] 表示收集位于下标 i 处的巧克力成本。每个巧克力都对应一个不同的类型,最初,位于下标 i 的巧克力就对应第 i 个类型。 在一步操作中,你可以用成本 x 执行下述行为: 同时修改所有巧克力的类型,将巧克力的类型 i th…

category

2

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·结合·enumeration

bolt

答案摘要

我们考虑枚举操作的次数,定义 表示第 个巧克力进行了 次操作后的最小成本。 对于第 个巧克力:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·结合·enumeration 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个长度为 n、下标从 0 开始的整数数组 numsnums[i] 表示收集位于下标 i 处的巧克力成本。每个巧克力都对应一个不同的类型,最初,位于下标 i 的巧克力就对应第 i 个类型。

在一步操作中,你可以用成本 x 执行下述行为:

  • 同时修改所有巧克力的类型,将巧克力的类型 ith 修改为类型 ((i + 1) mod n)th

假设你可以执行任意次操作,请返回收集所有类型巧克力所需的最小成本。

 

示例 1:

输入:nums = [20,1,15], x = 5
输出:13
解释:最开始,巧克力的类型分别是 [0,1,2] 。我们可以用成本 1 购买第 1 个类型的巧克力。
接着,我们用成本 5 执行一次操作,巧克力的类型变更为 [1,2,0] 。我们可以用成本 1 购买第 2 个类型的巧克力。
然后,我们用成本 5 执行一次操作,巧克力的类型变更为 [2,0,1] 。我们可以用成本 1 购买第 0 个类型的巧克力。
因此,收集所有类型的巧克力需要的总成本是 (1 + 5 + 1 + 5 + 1) = 13 。可以证明这是一种最优方案。

示例 2:

输入:nums = [1,2,3], x = 4
输出:6
解释:我们将会按最初的成本收集全部三个类型的巧克力,而不需执行任何操作。因此,收集所有类型的巧克力需要的总成本是 1 + 2 + 3 = 6 。

 

提示:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 109
  • 1 <= x <= 109
lightbulb

解题思路

方法一:枚举

我们考虑枚举操作的次数,定义 f[i][j]f[i][j] 表示第 ii 个巧克力进行了 jj 次操作后的最小成本。

对于第 ii 个巧克力:

  • 如果 j=0j = 0,即没有进行操作,那么 f[i][j]=nums[i]f[i][j] = nums[i]
  • 如果 0<jn10 \lt j \leq n-1,那么它的最小成本就是下标范围为 [i,..(ij+n)modn][i,.. (i - j + n) \bmod n] 的最小成本,即 f[i][j]=min{nums[i],nums[i1],,nums[(ij+n)modn]}f[i][j] = \min\{nums[i], nums[i - 1], \cdots, nums[(i - j + n) \bmod n]\},或者可以写成 f[i][j]=min{f[i][j1],nums[(ij+n)modn]}f[i][j] = \min\{f[i][j - 1], nums[(i - j + n) \bmod n]\}
  • 如果 jnj \ge n,由于当 j=n1j = n - 1 时,已经覆盖了所有最小成本,如果 jj 继续增大,那么最小成本不会再变化,但是操作次数的增加却会导致最终的成本增加,因此,我们不需要考虑 jnj \ge n 的情况。

综上,我们可以得到状态转移方程:

f[i][j]={nums[i],j=0min(f[i][j1],nums[(ij+n)modn]),0<jn1f[i][j] = \begin{cases} nums[i] ,& j = 0 \\ \min(f[i][j - 1], nums[(i - j + n) \bmod n]) ,& 0 \lt j \leq n - 1 \end{cases}

最后,我们只需要枚举操作的次数 jj,计算出每种操作次数下的最小成本,取最小值即可。即答案为 min0jn1i=0n1f[i][j]+x×j\min\limits_{0 \leq j \leq n - 1} \sum\limits_{i = 0}^{n - 1} f[i][j] + x \times j

时间复杂度 O(n2)O(n^2),空间复杂度 O(n2)O(n^2)。其中 nn 为数组 numsnums 的长度。

1
2
3
4
5
6
7
8
9
10
class Solution:
    def minCost(self, nums: List[int], x: int) -> int:
        n = len(nums)
        f = [[0] * n for _ in range(n)]
        for i, v in enumerate(nums):
            f[i][0] = v
            for j in range(1, n):
                f[i][j] = min(f[i][j - 1], nums[(i - j) % n])
        return min(sum(f[i][j] for i in range(n)) + x * j for j in range(n))
speed

复杂度分析

指标
时间complexity is O(n^2) because for each of n rotations, we may sum costs across n elements. Space complexity is O(n) for storing rotated cost arrays and tracking totals.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Will you consider the rotation cost when deciding which chocolate to pick first?

  • question_mark

    How many rotations will be necessary at maximum before costs repeat?

  • question_mark

    Can you optimize the calculation of cumulative costs across rotations using enumeration?

warning

常见陷阱

外企场景
  • error

    Forgetting to include the rotation cost x in total calculations.

  • error

    Overcounting purchases when a rotation shifts already collected chocolates.

  • error

    Failing to check all possible rotation counts for minimal total cost.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Minimize total cost when multiple chocolates of the same type exist with different prices.

  • arrow_right_alt

    Compute minimum cost if rotations are restricted to k times only.

  • arrow_right_alt

    Find total cost if some chocolates cannot be rotated due to constraints.

help

常见问题

外企场景

收集巧克力题解:数组·结合·enumeration | LeetCode #2735 中等