-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeemo_Attacking.py
51 lines (40 loc) · 2.38 KB
/
Teemo_Attacking.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds.
# More formally, an attack at second t will mean Ashe is poisoned during the inclusive time interval [t, t + duration - 1]. If Teemo attacks
# again before the poison effect ends, the timer for it is reset, and the poison effect will end duration seconds after the new attack.
# 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.
# Example 1:
# Input: timeSeries = [1,4], duration = 2
# Output: 4
# Explanation: Teemo's attacks on Ashe go as follows:
# - At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
# - At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
# Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
# Example 2:
# Input: timeSeries = [1,2], duration = 2
# Output: 3
# Explanation: Teemo's attacks on Ashe go as follows:
# - At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
# - At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
# Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.
class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
poisoned_total = 0
# loop and calculate the total time Ashe is poisoned
for i in range(len(timeSeries)):
# initialize the poison time series
poison_time = timeSeries[i]
# if attacked at end, we know Ashe is poisoned the whole duration of poison
if i + 1 == len(timeSeries):
poisoned_total += duration
break
# check the difference in time between current time slot and the next
difference = timeSeries[i+1]-timeSeries[i]
# if it is greater than the duration, than we know Ashe is posined the whole duration of poison
if difference > duration:
poisoned_total += duration
# if it is less, we know the poison duration is interrupted by the following attack
else:
poisoned_total += difference
return poisoned_total