-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeemoAttacking.java
30 lines (29 loc) · 938 Bytes
/
TeemoAttacking.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.namanh.array;
/**
* https://leetcode.com/problems/teemo-attacking
* You are given a non-decreasing integer array timeSeries, where timeSeries[i] denotes that Teemo attacks Ashe at
* second timeSeries[i], and an integer duration. Return the total number of seconds that Ashe is poisoned.
*
* S1: Set end is time when end of attack
* S2: At time, if time <= end, we minus end - time + 1
* S3: At time, if time > end, add duration into result
* S4: Return result
*
* Time: O(n)
* Space: O(1)
*/
public class TeemoAttacking {
public int findPoisonedDuration(int[] timeSeries, int duration) {
int result = 0;
int end = -1;
for (int time : timeSeries) {
if (end >= time) {
result += duration - (end - time + 1);
} else {
result += duration;
}
end = time + duration - 1;
}
return result;
}
}