-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0495-teemo-attacking.js
More file actions
29 lines (24 loc) · 853 Bytes
/
0495-teemo-attacking.js
File metadata and controls
29 lines (24 loc) · 853 Bytes
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
/**
* Teemo Attacking
* Time Complexity: O(N)
* Space Complexity: O(1)
*/
var findPoisonedDuration = function (timeSeries, duration) {
let totalPoisonSeconds = 0;
let seriesLength = timeSeries.length;
if (seriesLength === 0) {
return 0;
}
for (let currentAttackIndex = 0; currentAttackIndex < seriesLength; currentAttackIndex++) {
let currentAttackMoment = timeSeries[currentAttackIndex];
let nextAttackMoment;
if (currentAttackIndex + 1 < seriesLength) {
nextAttackMoment = timeSeries[currentAttackIndex + 1];
} else {
nextAttackMoment = currentAttackMoment + duration;
}
let contributionValue = Math.min(duration, nextAttackMoment - currentAttackMoment);
totalPoisonSeconds += contributionValue;
}
return totalPoisonSeconds;
};