您的位置:首页 > 编程语言 > C语言/C++

leetcode 495. Teemo Attacking

2017-06-25 15:32 453 查看

1.题目

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.

英雄Teemo对敌人 Ashe 使用毒药会使敌人中毒一段时间。

给出一个时间序列timeSeries表示英雄对敌人使用毒药的时间点,duration表示毒药的持续时间。求敌人总共有多长时间保持中毒状态。

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

4-1>2 所以 2+2=4

Example 2:

Input: [1,2], 2

Output: 3

(2-1)+2=3

2.分析

判断两个使用毒药的时间间隔delta

delta>= duration, 持续时间+=duration

delta< duration, 持续时间+=delta

3.代码

class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
if(timeSeries.size()<1)
return 0;
int count = duration;
for (int i = 1; i < timeSeries.size(); i++)
count += timeSeries[i] - timeSeries[i - 1] >= duration ? duration : timeSeries[i] - timeSeries[i - 1];
return count;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode c++