算法設計與應用基礎:第一週(1)

495. Teemo Attacking

Description Submission Solutions
  • Total Accepted: 4627
  • Total Submissions: 8953
  • Difficulty: Medium
  • Contributors: love_FDU_llp

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.



My solution

codes:

class Solution {
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
        int size=timeSeries.size();
        int ans=size*duration;
        for(int i=1;i<size;i++)
        {
            if(timeSeries[i]-timeSeries[i-1]<duration)
            {
                ans-=duration-(timeSeries[i]-timeSeries[i-1]);
            }
        }
        //int ans1=ans;
        return ans;
    }
};


解題思路:循環遍歷timeSeries每找到相鄰兩個數字差小於duration,就修改相應的ans值(初始值設爲size*duration)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章