Teemo Attacking

来源:互联网 发布:q3 1.4t 2.0t 知乎 编辑:程序博客网 时间:2024/06/05 22:45

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.

这道题,采用的是分情况给sum加值的方法,当数组中,timeSeries[i]+duration<=timeSeries[i+1]时,就直接加sum+=duration;,因为有足够的时间让这一枪的毒充分的延迟,对于其他非最后一个数组元素的情况,都是只要加上前后2个时间点的差就可以了。

代码如下:

class Solution {public:    int findPoisonedDuration(vector<int>& timeSeries, int duration) {        int sum=0;        int size=timeSeries.size();        for(int i=0;i<size;i++){            if(size==1){                sum+=duration;                return sum;            }            else if(size>=2){                if(i!=size-1){                    if(timeSeries[i]+duration<=timeSeries[i+1]){                        sum+=duration;                    }                    else{                        sum+=(timeSeries[i+1]-timeSeries[i]);                    }                }                else{                     sum+=duration;                }            }        }        return sum;    }};
0 0
原创粉丝点击