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.
思路:
首先假定所有时间下的攻击都带来完全的中毒,之后再把重复的地方去掉。
题解:
int findPoisonedDuration(const std::vector<int>& timeSeries, int duration) {
int accumulated = duration * timeSeries.size();
int lastAttackEnd = std::numeric_limits<int>::min();
for(auto time: timeSeries) {
if (lastAttackEnd > time) {
accumulated -= lastAttackEnd - time;
}
lastAttackEnd = time + duration;
}
return accumulated;
}