LeetCode 495. Teemo Attacking 题目翻译+AC代码

英文原题:
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.

中文题目:
在LLP的世界里,有一个名叫Teemo的英雄,他的攻击可以让他的敌人Ashe陷入中毒的状态。现在,我们将给出Teemo对Ashe发起攻击的时间点的上升序列,以及每次攻击后Ashe中毒状态持续的时间,你需要输出Ashe处于中毒状态的时间总和。

假设Teemo的攻击发起在每个时间点的最开端,并且在发起攻击时便立刻使Ashe陷入中毒状态。

例1:

输入:[1.4],2
输出:4
解释:在第1个时间单位,TeemoAshe发起进攻,并使Ashe立刻处于中毒状态。Ashe的这种中毒状态将持续2个时间单位,直到第2个时间单位结束,中毒状态结束。
在第4个时间单位,Teemo再次对Ashe发起进攻,使Ashe再次陷入持续2个时间单位的中毒状态中。
所以,你最终输出的结果是4。

例2:

输入:[1,2],2
输出:3
解释:在第1个时间单位,TeemoAshe发起攻击并使Ashe立刻处于中毒状态。Ashe的这种中毒状态将持续2个时间单位,直到第2个时间单位结束,中毒状态结束。
然而,在第2个时间单位一开始,Teemo便对已经处在中毒状态的Ashe再次发起了攻击。
中毒状态不会叠加,不过第二次攻击对Ashe带来的中毒作用仍然会在第2个时间单位开始,Ashe的中毒状态将会在第3个时间单位结束时结束。
所以,你最终输出结果是3。

注:
1.假设时间点序列的长度不超过10000。
2.假设时间点序列中的时间点和中毒状态持续时间都是非负整数,而且不超过10000。

解题思路:
只需要注意一点,中毒时间重叠时,不能重复计数(也就是第二个输入样例中出现的情况)。

AC代码:

class Solution {
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
        int cnt = 0;
        if (timeSeries.size() == 0)// 一个特判
            return cnt;
        for (int i = 0; i < (int)timeSeries.size()-1; i++) {
            for (int k = 0; k < duration; k++) {
                if ((timeSeries[i] + k) != timeSeries[i + 1]) {
                    cnt++;
                }
                else {
                    break;
                }
            }
        }
        return cnt + duration;
        }
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值