Week Training: 495 Teemo Attacking

来源:互联网 发布:湘潭大学网络 编辑:程序博客网 时间:2024/06/05 08:15
Although the difficulty of the problem is "medium", it's a easy problem obscured by a story. The fact of the problem is just to find the total length of effective insertion of a integer to a sequence. We just need to judge if the interval of the sequence is bigger or less than the integer, and decide what to add. In addition, judgement of whether the sequence is empty and the duration is zero is required.
class Solution {public:    int findPoisonedDuration(vector<int>& timeSeries, int duration) {        int sum = duration;        if(timeSeries.empty())            return 0;        for(int i=0; i<timeSeries.size()-1; i++){            if((timeSeries[i+1]-timeSeries[i])>=duration)                sum += duration;            else                sum += (timeSeries[i + 1] - timeSeries[i]);        }        if(duration == 0)            sum = 0;        return sum;    }};

0 0