Leetcode 495. 提莫攻击

原题链接:Leetcode 495. Teemo Attacking

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.

Example 1:

Input: timeSeries = [1,4], duration = 2
Output: 4
Explanation: Teemo’s attacks on Ashe go as follows:

  • At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
  • At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
    Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.

Example 2:

Input: timeSeries = [1,2], duration = 2
Output: 3
Explanation: Teemo’s attacks on Ashe go as follows:

  • At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
  • At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
    Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.

Constraints:

  • 1 <= timeSeries.length <= 104
  • 0 <= timeSeries[i], duration <= 107
  • timeSeries is sorted in non-decreasing order.

方法一:遍历

思路:

timeSeries数组已经保证了是非递减排序

C++代码:

class Solution {
   
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
   
        int ans = 0, last = -1;  // last为上一次攻击的结束点
        for(int i = 0; i < timeSeries.size(); i++ ){
   
            int cur = timeSeries[i] + duration - 1; // 本次攻击持续时间
            if(last < timeSeries[i])
                ans += duration;
            else
                ans += cur - last;
            last = cur;
        }
        return ans;
    }
};

相关推荐

  1. Leetcode 495. 攻击

    2023-12-29 23:34:02       36 阅读
  2. LeetCode第一天(495.攻击

    2023-12-29 23:34:02       17 阅读
  3. leetcode-攻击

    2023-12-29 23:34:02       23 阅读
  4. 「优选算法刷题」:攻击

    2023-12-29 23:34:02       29 阅读
  5. LeetCode 45

    2023-12-29 23:34:02       46 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 23:34:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 23:34:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 23:34:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 23:34:02       20 阅读

热门阅读

  1. Red Hat系列Docker安装与移除

    2023-12-29 23:34:02       30 阅读
  2. uniapp-H5项目的坑

    2023-12-29 23:34:02       37 阅读
  3. Python爬虫实战演练之爬去VIP电影

    2023-12-29 23:34:02       38 阅读
  4. 大模型系列课程学习

    2023-12-29 23:34:02       26 阅读
  5. MySQL 设置商品乐观锁号示例

    2023-12-29 23:34:02       37 阅读
  6. 力扣:435. 无重叠区间(贪心)

    2023-12-29 23:34:02       36 阅读
  7. Leetcode的AC指南 —— 哈希法:454. 四数相加 II

    2023-12-29 23:34:02       44 阅读