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)