-
Notifications
You must be signed in to change notification settings - Fork 228
/
min-rewards.py
61 lines (53 loc) · 1.5 KB
/
min-rewards.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
52
53
54
55
56
57
58
59
60
61
# MIN REWARDS
# O(N^2) time and O(N) space
def minRewards(scores):
rewards = [1] * len(scores)
i = 1
while i < len(scores):
if scores[i] > scores[i-1]:
rewards[i] = rewards[i-1] + 1
else:
j = i - 1
while j >= 0 and scores[j] > scores[j+1]:
rewards[j] = max(rewards[j], rewards[j+1]+1)
j -= 1
i += 1
return sum(rewards)
# O(N) time and space
def minRewards(scores):
# Write your code here.
localMins = getAllLocalMins(scores)
rewards = [1] * len(scores)
for localMin in localMins:
i = localMin + 1
while i < len(scores) and scores[i] > scores[i - 1]:
rewards[i] = max(rewards[i-1] + 1, rewards[i])
i += 1
i = localMin - 1
while i >= 0 and scores[i] > scores[i+1]:
rewards[i] = max(rewards[i+1] + 1, rewards[i])
i -= 1
return sum(rewards)
def getAllLocalMins(array):
if len(array) == 1:
return [0]
localMins = []
for i in range(len(array)):
if i == 0 and array[i] < array[i+1]:
localMins.append(i)
if i == len(array) - 1 and array[i] < array[i - 1]:
localMins.append(i)
if i not in [0, len(array) - 1] and array[i] < array[i+1] and array[i] < array[i-1]:
localMins.append(i)
return localMins
# O(N) time and space
def minRewards(scores):
# Write your code here.
rewards = [1] * len(scores)
for i in range(1, len(scores)):
if scores[i] > scores[i-1]:
rewards[i] = rewards[i-1] + 1
for i in reversed(range(len(scores) - 1)):
if scores[i] > scores[i+1]:
rewards[i] = max(rewards[i], rewards[i+1] + 1)
return sum(rewards)