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
相关推荐
CS创新实验室5 小时前
《计算机网络》深入学:路由算法与路径选择
网络·计算机网络·算法
一条大祥脚5 小时前
ABC357 基环树dp|懒标记线段树
数据结构·算法·图论
tod1135 小时前
力扣高频 SQL 50 题阶段总结(四)
开发语言·数据库·sql·算法·leetcode
naruto_lnq5 小时前
C++中的桥接模式
开发语言·c++·算法
苦藤新鸡5 小时前
50.腐烂的橘子
数据结构·算法
想进个大厂5 小时前
代码随想录day32 动态规划01
算法·动态规划
j445566115 小时前
C++中的职责链模式高级应用
开发语言·c++·算法
uesowys6 小时前
Apache Spark算法开发指导-Decision tree classifier
算法·决策树·spark
池央6 小时前
贪心算法-最大数
算法·贪心算法
iAkuya6 小时前
(leetcode)力扣100 57电话号码的字母组合(回溯)
算法·leetcode·深度优先