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
相关推荐
aaaameliaaa5 小时前
字符函数和字符串函数
c语言·笔记·算法
城管不管6 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
月疯6 小时前
二分法算法(水平等分图形面积)
算法
豆瓣鸡7 小时前
算法日记 - Day3
java·开发语言·算法
白白白小纯7 小时前
算法篇—反转链表
c语言·数据结构·算法·leetcode
Achou.Wang7 小时前
深入理解go语言-第5章 并发编程——Go的灵魂
大数据·算法·golang
The Chosen One9857 小时前
高进度算法模板速记(待完善)
java·前端·算法
圣保罗的大教堂10 小时前
leetcode 3517. 最小回文排列 I 中等
leetcode
土豆.exe10 小时前
Fastjson2 2.0.53 哈希碰撞 RCE:从原理到三种打法
算法·哈希算法
黄河123长江10 小时前
有限Abel群的结构()
算法