Leetcode 495 Teemo Attacking

来源:互联网 发布:淘宝新店怎么增加流量 编辑:程序博客网 时间:2024/06/05 14:24

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.



class Solution {

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