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
相关推荐
.柒宇.13 小时前
力扣hot100----15.三数之和(java版)
java·数据结构·算法·leetcode
杰克尼14 小时前
二分查找为什么总是写错
java·数据结构·算法
程序员阿鹏17 小时前
56.合并区间
java·数据结构·算法·leetcode
程序员三藏18 小时前
软件测试之环境搭建及测试流程
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
古译汉书18 小时前
Stm32江科大入门教程--各章节详细笔记---查阅传送门
数据结构·stm32·单片机·嵌入式硬件·算法
Jasmine_llq20 小时前
《P2656 采蘑菇》
算法·强连通分量·广度优先搜索(bfs)·tarjan 算法·图的缩点操作·有向无环图(dag)·最长路径
芥子沫20 小时前
《人工智能基础》[算法篇3]:决策树
人工智能·算法·决策树
mit6.82420 小时前
dfs|位运算
算法