495. 提莫攻击

链接

https://leetcode.cn/problems/teemo-attacking/description/

思路

这题思路其实很简单,需要2个变量,1个记住总数,1个记住之前遍历过的序列能够访问到的最大值。

如果遍历到某个数时,这个最大值比他大,就证明之前序列中的某个加和被浪费掉了,所以应该从total中减去差值。

代码

class Solution:
    def findPoisonedDuration(self, timeSeries, duration: int) -> int:
        total = 0
        remains = 0
        for time in timeSeries:
            total += duration
            if remains == 0:
                remains = time + duration
            else:
                if remains > time:
                    total -= remains - time
                remains = time + duration
        return total

 

posted @ 2023-09-18 19:17  BJFU-VTH  阅读(5)  评论(0编辑  收藏  举报