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
相关推荐
勤劳的进取家12 分钟前
数据链路层基础
网络·学习·算法
Advancer-29 分钟前
第二次蓝桥杯总结(上)
java·算法·职场和发展·蓝桥杯
ん贤1 小时前
加密算法(对称、非对称、哈希、签名...)
算法·哈希算法
superior tigre1 小时前
78 子集
算法·leetcode·深度优先·回溯
天威?*1 小时前
bitset的数据结构用法
算法·动态规划
hoiii1872 小时前
粒子滤波跟踪系统 - 蒙特卡洛方法实现
算法
weisian1513 小时前
Java并发编程--47-分布式ID生成器:雪花算法(Snowflake)与时钟回拨问题
java·算法·时钟回拨·雪花算法id
itzixiao3 小时前
L1-066 猫是液体(5分)[java][python]
java·开发语言·python·算法
ytttr8733 小时前
MATLAB SIFT图像配准实现
算法·机器学习·matlab
小饕3 小时前
从 Word2Vec 到多模态:词嵌入技术的演进全景
人工智能·算法·机器学习