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 timeSeriesi denotes that Teemo attacks Ashe at second timeSeriesi, 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
相关推荐
奋发向前wcx3 小时前
CSP-J复赛模拟赛4 王晨旭补题 2026.10.5
算法
念何架构之路5 小时前
zap采样器与性能优化内幕
算法·性能优化·哈希算法
晒太羊的猫6 小时前
hot100动态规划刷题
算法·动态规划
weixin_531094696 小时前
heap_4内存管理
java·开发语言·算法
余额瞒着我当琳6 小时前
红黑树的实现--红黑树的概念、规则、效率、结构、旋转、查找、验证
数据结构·c++·算法
程序员南飞7 小时前
win系统安装minio
java·数据结构·算法
青少儿编程课堂7 小时前
AC自动机多模式串匹配精讲:校园关键词巡检实战解析
c++·python·算法·bfs·信息学竞赛
我命由我123457 小时前
Photoshop - Photoshop 矢量蒙版
学习·职场和发展·产品运营·求职招聘·职场发展·产品经理·photoshop
longlongzihan7 小时前
LeetCode 5. 最长回文子串:从暴力匹配到中心扩展的思维演进
c++·算法·leetcode
waoooqwe7 小时前
问卷样本真实吗
大数据·数据库·算法·数据分析