1. 题目
2. 思路
(1) 模拟法
- 若本次攻击时间大于上一次中毒的结束时间,则直接记录本次中毒的持续时间;否则,记录两次攻击时间的增量时间。
3. 代码
public class Test {
public static void main(String[] args) {
}
}
class Solution {
public int findPoisonedDuration(int[] timeSeries, int duration) {
int res = duration;
int end = timeSeries[0] + duration - 1;
for (int i = 1; i < timeSeries.length; i++) {
if (timeSeries[i] > end) {
res += duration;
} else {
res += (timeSeries[i] - timeSeries[i - 1]);
}
end = timeSeries[i] + duration - 1;
}
return res;
}
}