495. Teemo Attacking
- 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)