因为时间序列有序,从后往前遍历一次,总时间要么是前一个时间点减后一个时间点,要么是吃满中毒持续时间。
class Solution { public: int findPoisonedDuration(vector<int>& timeSeries, int duration) { int count = 0; for(int i = timeSeries.size()-1; i>=0; i--) { if(i == (timeSeries.size()-1)) count = duration; else{ if(timeSeries[i+1] - timeSeries[i]<duration) count += timeSeries[i+1] - timeSeries[i]; else count += duration; } } return count; } };