[LeetCode] Teemo Attacking 提莫攻击

简介:

In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition.

You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.

Example 1:

Input: [1,4], 2
Output: 4
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately. 
This poisoned status will last 2 seconds until the end of time point 2.
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds.
So you finally need to output 4.

Example 2:

Input: [1,2], 2
Output: 3
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned. 
This poisoned status will last 2 seconds until the end of time point 2.
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status.
Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3.
So you finally need to output 3.

Note:

  1. You may assume the length of given time series array won't exceed 10000.
  2. You may assume the numbers in the Teemo's attacking time series and his poisoning time duration per attacking are non-negative integers, which won't exceed 10,000,000.

LeetCode果然花样百出,连提莫都搬上题目了,那个草丛里乱种蘑菇的小提莫,那个“团战可以输提莫必须死”的提莫??可以,服了,坐等女枪女警轮子妈的题目了~好了,不闲扯了,其实这道题蛮简单的,感觉不能算一道medium的题,就直接使用贪心算法,比较相邻两个时间点的时间差,如果小于duration,就加上这个差,如果大于或等于,就加上duration即可,参见代码如下:

class Solution {
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
        if (timeSeries.empty()) return 0;
        int res = 0, n = timeSeries.size();
        for (int i = 1; i < n; ++i) {
            int diff = timeSeries[i] - timeSeries[i - 1];
            res += (diff < duration) ? diff : duration;
        }
        return res + duration;
    }
};

本文转自博客园Grandyang的博客,原文链接:Teemo Attacking 提莫攻击,如需转载请自行联系原博主。

相关文章
|
7月前
leetcode:495. 提莫攻击
leetcode:495. 提莫攻击
38 0
【Leetcode -495.提莫攻击 -496.下一个更大的元素Ⅰ】
【Leetcode -495.提莫攻击 -496.下一个更大的元素Ⅰ】
48 0
leetcode 1222. 可以攻击国王的皇后(每日一题)
leetcode 1222. 可以攻击国王的皇后(每日一题)
78 0
|
算法 C++ Python
每日算法系列【LeetCode 495】提莫攻击
每日算法系列【LeetCode 495】提莫攻击
109 0
|
算法 Java C#
【算法千题案例】⚡️每日LeetCode打卡⚡️——60.提莫攻击
📢前言 🌲原题样例:提莫攻击 🌻C#方法:一次遍历 🌻Java 方法:一次遍历
【算法千题案例】⚡️每日LeetCode打卡⚡️——60.提莫攻击
|
算法 Java
【leetcode刷题】36.提莫攻击——Java版
⭐欢迎订阅《leetcode》专栏,每日一题,每天进步⭐ 我建议加上攻速,攻击力,生命回复速率,护甲,魔抗,血量,计算在攻击频率下提莫几秒弄死艾希 ——leetcode此题热评
166 0
【leetcode刷题】36.提莫攻击——Java版
|
算法
​LeetCode刷题实战495:提莫攻击
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
116 0
|
3月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
4月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
120 2