LeetCode 题解工作台

提莫攻击

在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄。他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。 当提莫攻击艾希,艾希的中毒状态正好持续 duration 秒。 正式地讲,提莫在 t 发起攻击意味着艾希在时间区间 [t, t + duration - 1] (含 t 和 t + du…

category

2

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·模拟

bolt

答案摘要

我们先考虑最后一次攻击,此次攻击一定可以使得艾希处于中毒状态,所以总中毒时间至少为 `duration`。 接下来,我们考虑前 次攻击,每一次攻击的中毒持续时间为 $min(duration, timeSeries[i] - timeSeries[i-1])$,其中 从 1 开始。我们将这些中毒持续时间累加起来,即为总中毒时间。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·模拟 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄。他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。

当提莫攻击艾希,艾希的中毒状态正好持续 duration 秒。

正式地讲,提莫在 t 发起攻击意味着艾希在时间区间 [t, t + duration - 1](含 tt + duration - 1)处于中毒状态。如果提莫在中毒影响结束 再次攻击,中毒状态计时器将会 重置 ,在新的攻击之后,中毒影响将会在 duration 秒后结束。

给你一个 非递减 的整数数组 timeSeries ,其中 timeSeries[i] 表示提莫在 timeSeries[i] 秒时对艾希发起攻击,以及一个表示中毒持续时间的整数 duration

返回艾希处于中毒状态的 秒数。

 

示例 1:

输入:timeSeries = [1,4], duration = 2
输出:4
解释:提莫攻击对艾希的影响如下:
- 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
- 第 4 秒,提莫再次攻击艾希,艾希中毒状态又持续 2 秒,即第 4 秒和第 5 秒。
艾希在第 1、2、4、5 秒处于中毒状态,所以总中毒秒数是 4 。

示例 2:

输入:timeSeries = [1,2], duration = 2
输出:3
解释:提莫攻击对艾希的影响如下:
- 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
- 第 2 秒,提莫再次攻击艾希,并重置中毒计时器,艾希中毒状态需要持续 2 秒,即第 2 秒和第 3 秒。
艾希在第 1、2、3 秒处于中毒状态,所以总中毒秒数是 3 。

 

提示:

  • 1 <= timeSeries.length <= 104
  • 0 <= timeSeries[i], duration <= 107
  • timeSeries非递减 顺序排列
lightbulb

解题思路

方法一:一次遍历

我们先考虑最后一次攻击,此次攻击一定可以使得艾希处于中毒状态,所以总中毒时间至少为 duration

接下来,我们考虑前 n1n-1 次攻击,每一次攻击的中毒持续时间为 min(duration,timeSeries[i]timeSeries[i1])min(duration, timeSeries[i] - timeSeries[i-1]),其中 ii 从 1 开始。我们将这些中毒持续时间累加起来,即为总中毒时间。

时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。其中 nn 为数组 timeSeries 的长度。

1
2
3
4
5
6
7
class Solution:
    def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
        ans = duration
        for a, b in pairwise(timeSeries):
            ans += min(duration, b - a)
        return ans
speed

复杂度分析

指标
时间complexity is O(N) since each attack is processed once. Space complexity is O(1) as only a few variables track the total poisoned time and previous poison end.
空间\mathcal{O}(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Focus on handling overlapping poison durations correctly.

  • question_mark

    Check if candidates use linear iteration instead of nested loops.

  • question_mark

    See if the candidate accounts for edge cases when attacks occur back-to-back.

warning

常见陷阱

外企场景
  • error

    Double-counting seconds when poison durations overlap.

  • error

    Ignoring the inclusive nature of poison intervals.

  • error

    Using extra space unnecessarily instead of O(1) accumulation.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow unsorted timeSeries array and require sorting before processing.

  • arrow_right_alt

    Change poison duration dynamically per attack, requiring interval comparison.

  • arrow_right_alt

    Compute maximum continuous poisoned interval instead of total poisoned seconds.

help

常见问题

外企场景

提莫攻击题解:数组·模拟 | LeetCode #495 简单