题目
In LOL 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.
Input: [1,4], 2
Output: 4
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately.
This poisoned status will last 2 seconds until the end of time point 2.
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds.
So you finally need to output 4.
分析
提姆致盲艾希的例子,一次攻击有持续伤害,给定攻击的时间序列和一次攻击持续伤害的时间,求共受伤害的时间。
这是一个数组重合的问题
1 4 // + 2
2 5
没有重合所以持续时间是4。主要考虑时间重合的情况。需要减去重合造成的重复计时。即减去中间xxx的部分。
[ 【xxxx] 】
代码
public int findPoisonedDuration(int[] timeSeries, int duration) {
int sum = 0;
for(int i = 1; i < timeSeries.length; ++i){
int diff = timeSeries[i] - timeSeries[i-1];
sum += diff > duration ? 0 : duration - diff; //重复计时之和
}
return duration * timeSeries.length - sum;
}
网友评论