Leetcode 495. Teemo Attacking

Problem

Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds. More formally, an attack at second t will mean Ashe is poisoned during the inclusive time interval [t, t + duration - 1]. If Teemo attacks again before the poison effect ends, the timer for it is reset, and the poison effect will end duration seconds after the new attack.

You are given a non-decreasing integer array timeSeries, where timeSeries[i] denotes that Teemo attacks Ashe at second timeSeries[i], and an integer duration.

Return the total number of seconds that Ashe is poisoned.

Algorithm

Calculate the sum of the sizes of each adjacent interval. If the sum exceeds the duration, use the duration as the value; otherwise, use the actual sum of the intervals.

Code

python3 复制代码
class Solution:
    def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
        ans, tlen = duration, len(timeSeries)
        for i in range(1, tlen):
            if timeSeries[i] - timeSeries[i-1] > duration:
                ans += duration
            else:
                ans += timeSeries[i] - timeSeries[i-1]
        return ans
相关推荐
没学上了3 分钟前
VLM_一维离散卷积与二维离散卷积(还是复习感觉还行)
算法
黛色正浓15 分钟前
leetCode-热题100-贪心合集(JavaScript)
javascript·算法·leetcode
轻微的风格艾丝凡33 分钟前
嵌入式定时器计时技巧:用有符号数省略溢出判断的底层逻辑与实践
数据库·算法·dsp开发·嵌入式软件
No0d1es39 分钟前
2025年12月 GESP CCF编程能力等级认证C++四级真题
算法·青少年编程·等级考试·gesp·ccf
CodeByV1 小时前
【算法题】快排
算法
一起努力啊~1 小时前
算法刷题--长度最小的子数组
开发语言·数据结构·算法·leetcode
rchmin1 小时前
限流算法:令牌桶与漏桶详解
算法·限流
leoufung1 小时前
LeetCode 221:Maximal Square 动态规划详解
算法·leetcode·动态规划
黑符石1 小时前
【论文研读】Madgwick 姿态滤波算法报告总结
人工智能·算法·机器学习·imu·惯性动捕·madgwick·姿态滤波
源代码•宸1 小时前
Leetcode—39. 组合总和【中等】
经验分享·算法·leetcode·golang·sort·slices