LeetCode 题解工作台

拆炸弹

你有一个炸弹需要拆除,时间紧迫!你的情报员会给你一个长度为 n 的 循环 数组 code 以及一个密钥 k 。 为了获得正确的密码,你需要替换掉每一个数字。所有数字会 同时 被替换。 如果 k > 0 ,将第 i 个数字用 接下来 k 个数字之和替换。 如果 k ,将第 i 个数字用 之前 k 个数…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 滑动窗口(状态滚动更新)

bolt

答案摘要

我们定义一个长度为 的答案数组 ,初始时所有元素都为 。根据题意,若 为 ,直接返回 。 否则,遍历每个位置 :

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 滑动窗口(状态滚动更新) 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

你有一个炸弹需要拆除,时间紧迫!你的情报员会给你一个长度为 n 的 循环 数组 code 以及一个密钥 k 。

为了获得正确的密码,你需要替换掉每一个数字。所有数字会 同时 被替换。

  • 如果 k > 0 ,将第 i 个数字用 接下来 k 个数字之和替换。
  • 如果 k < 0 ,将第 i 个数字用 之前 k 个数字之和替换。
  • 如果 k == 0 ,将第 i 个数字用 0 替换。

由于 code 是循环的, code[n-1] 下一个元素是 code[0] ,且 code[0] 前一个元素是 code[n-1] 。

给你 循环 数组 code 和整数密钥 k ,请你返回解密后的结果来拆除炸弹!

 

示例 1:

输入:code = [5,7,1,4], k = 3
输出:[12,10,16,13]
解释:每个数字都被接下来 3 个数字之和替换。解密后的密码为 [7+1+4, 1+4+5, 4+5+7, 5+7+1]。注意到数组是循环连接的。

示例 2:

输入:code = [1,2,3,4], k = 0
输出:[0,0,0,0]
解释:当 k 为 0 时,所有数字都被 0 替换。

示例 3:

输入:code = [2,4,9,3], k = -2
输出:[12,5,6,13]
解释:解密后的密码为 [3+9, 2+3, 4+2, 9+4] 。注意到数组是循环连接的。如果 k 是负数,那么和为 之前 的数字。

 

提示:

  • n == code.length
  • 1 <= n <= 100
  • 1 <= code[i] <= 100
  • -(n - 1) <= k <= n - 1
lightbulb

解题思路

方法一:模拟

我们定义一个长度为 nn 的答案数组 ansans,初始时所有元素都为 00。根据题意,若 kk00,直接返回 ansans

否则,遍历每个位置 ii

kk 为正数,那么 ii 位置的值为 ii 位置后 kk 个位置的值之和,即:

ans[i]=j=i+1i+kcode[jmodn]ans[i] = \sum_{j=i+1}^{i+k} code[j \bmod n]

kk 为负数,那么 ii 位置的值为 ii 位置前 k|k| 个位置的值之和,即:

ans[i]=j=i+ki1code[(j+n)modn]ans[i] = \sum_{j=i+k}^{i-1} code[(j+n) \bmod n]

时间复杂度 O(n×k)O(n \times |k|),忽略答案的空间消耗,空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        n = len(code)
        ans = [0] * n
        if k == 0:
            return ans
        for i in range(n):
            if k > 0:
                for j in range(i + 1, i + k + 1):
                    ans[i] += code[j % n]
            else:
                for j in range(i + k, i):
                    ans[i] += code[(j + n) % n]
        return ans
speed

复杂度分析

指标
时间O(n)
空间O(n)
psychology

面试官常问的追问

外企场景
  • question_mark

    Evaluate understanding of the sliding window approach, especially with circular arrays.

  • question_mark

    Check if the candidate handles negative k values correctly and adapts the sliding window accordingly.

  • question_mark

    Look for efficient use of space, ensuring the algorithm uses O(n) space and doesn't recompute sums unnecessarily.

warning

常见陷阱

外企场景
  • error

    Overcomplicating the solution by using nested loops or redundant calculations.

  • error

    Not using modulo arithmetic correctly to handle the circular nature of the array.

  • error

    Failing to consider edge cases, like k being 0 or negative.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the problem to return the product of the next k numbers instead of the sum.

  • arrow_right_alt

    Change the problem to use an array of negative values and see if the candidate can adjust the solution.

  • arrow_right_alt

    Allow k to be larger than the length of the array and check if the candidate can handle that with modulo arithmetic.

help

常见问题

外企场景

拆炸弹题解:滑动窗口(状态滚动更新) | LeetCode #1652 简单