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
相关推荐
一只旭宝13 小时前
回调函数(Callback)
linux·服务器·算法
秋名RG13 小时前
算法的划分
算法
Tisfy13 小时前
LeetCode 3619.总价值可以被 K 整除的岛屿数目:广度优先搜索BFS
算法·leetcode·bfs·宽度优先
地平线开发者13 小时前
【大模型】EAGLE 与 EAGLE-3:用“特征预测“加速投机解码
算法
lengxuenong14 小时前
区间覆盖问题 · 简明学习手册1
java·学习·算法
明志数科14 小时前
人类视频与机器人示教的数据配比:分层训练的数据采购与采集设计
人工智能·算法
Ivanqhz14 小时前
MoE 原理、TC/EC 路由、LSH-MoE 与负载均衡浅析
运维·网络·人工智能·深度学习·算法·机器学习·负载均衡
Sarvartha14 小时前
栈的四则运算、栈的递归应用
数据结构·算法
stolentime14 小时前
CF2277E Tree Guessing题解
c++·算法·贪心算法·交互·ad-hoc
杨逢昌工厂6S管理14 小时前
126-杨逢昌6S三环体系:越做越乱的现场熵增机理与基准化治理——一线现场诊断笔记
经验分享·笔记·职场和发展·学习方法