495. Teemo Attacking

题目

In LOL 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.

看着很复杂,实际上就是给一串时间点,给一个作用时间,计算Teemo的总的作用时间。

思路

判断时间序列上的点加上duration是否超过下个时间点,没有超过则加上duration,超过则加上下个时间点-上个时间点。
最后加上duration,因为最后一个肯定是完整作用时间。

代码

    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
    if (timeSeries.size()==0) return 0;
    if (duration==0) return 0;
    int totalTime=0;
    for(int i=0;i<timeSeries.size()-1;i++)
    {
        if(timeSeries[i]+duration>timeSeries[i+1]) totalTime+=timeSeries[i+1]-timeSeries[i];
        else totalTime+=duration;
    }
    totalTime+=duration;
    return totalTime;
    
    }