Question
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.
思路
题目相当简单, 只要判断
当前时间点time[i]+持续时间-1 是否大于下一时间点time[i+1]
大于: 中毒时间为 time[i+1]-time[i];
小于 :中毒时间 为 duration.代码如下:
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
int ret=0;
auto len=timeSeries.size();
for(size_t i=0; i<len; ++i){
size_t j=i+1;
if(j<len){
if(timeSeries[i]+duration<=timeSeries[j])
ret+=duration;
else
ret+=(timeSeries[j]-timeSeries[i]);
}
else
ret+=duration; //最后一个时间点,持续时间为duration
}
return ret;
}
};