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
相关推荐
银空飞羽11 分钟前
职型求职工具实测:把简历分析、岗位匹配和定向优化串成一条求职链路
人工智能·经验分享·面试·职场和发展·跳槽·求职招聘·创业创新
进化矩阵21 分钟前
别把指标当目的:当数字开始反噬你
大数据·人工智能·职场和发展·创业创新
Jared_devin1 小时前
单头、多头 Self-Attention可视化
人工智能·pytorch·深度学习·算法·机器学习·pycharm
wuyk5551 小时前
18.Kruskal 算法:用最短的边连成一张网
开发语言·算法·图论
pen-ai2 小时前
【优化方法】最小二乘:从误差平方到线性与非线性拟合
人工智能·算法·机器学习
Q26433650232 小时前
【有源码】基于机器学习的电信网络诈骗话术语义识别与可视化分析系统 XGBoost算法+Hadoop+Spark
大数据·hadoop·算法·机器学习·spark·毕业设计·课程设计
不会就选b3 小时前
算法日常・每日刷题--贪心<11>
算法·leetcode·职场和发展
千里码aicood3 小时前
面向电商冷启动与数据稀疏性的协同过滤算法优化研究
大数据·算法·协同过滤
xxxiugou1233 小时前
双指针解题秘籍:从入门到精通
c语言·数据结构·c++·算法
能源革命3 小时前
TimesFM(Time Series Foundation Model)介绍
算法·能源