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
相关推荐
ysa0510306 分钟前
【板子】欧拉序
c++·笔记·算法·深度优先·图论
大耳朵糊涂10 分钟前
链表的基础操作(二)
算法
拂拉氏15 分钟前
【知识讲解】 红黑树的删除讲解
数据结构·算法·红黑树
初学者,亦行者16 分钟前
算法设计与分析:动态规划 - TSP问题和0-1背包问题
c++·算法·动态规划
ward RINL31 分钟前
# GPT-5.6-sol vs GPT-5.5 新题实测:非弹性碰撞物理题和稳定路由算法
网络·gpt·算法
灯澜忆梦31 分钟前
【dp_1】爬楼梯 | 斐波那契数 | 第 N 个泰波那契数 | 三步问题
算法·golang
CoderYanger44 分钟前
视频切割脚本(Python版)
linux·开发语言·windows·后端·python·程序人生·职场和发展
星释1 小时前
鸿蒙智能体开发实战:35.鸿蒙壁纸大师 - 调用火山引擎模型生成壁纸
算法·华为·ai·harmonyos·鸿蒙·火山引擎
geovindu1 小时前
go: Floyd-Warshall Algorithms
开发语言·后端·算法·golang