forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Min_Rewards.py
68 lines (55 loc) · 1.93 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
62
63
64
65
66
67
68
# Solution #1
# O(n^2) time | O(n) space
def minRewards(score):
rewards = [1 for _ in score]
for i in range(1, len(score)):
j = i - 1
if score[i] > score[j]:
rewards[i] = rewards[j] + 1
else:
while j >=0 and score[j] > score[j + 1]:
rewards[j] = max(rewards[j], rewards[j + 1] + 1)
j -= 1
return sum(rewards)
# Solution #2
# O(n) time | O(n) space
def minRewards(scores):
rewards = [1 for _ in scores]
localMinIdxs = getLocalMinIdxs(scores)
for localMinIdx in localMinIdxs:
expandFromLocalMinIdx(localMinIdx, scores, rewards)
return sum(rewards)
def getLocalMinIdxs(array):
if len(array) == 1:
return [0]
localMinIdxs = []
for i in range(len(array)):
if i == 0 and array[i] < array[i + 1]:
localMinIdxs.append(i)
if i == len(array) - 1 and array[i] < array[i - 1]:
localMinIdxs.append(i)
if i == 0 or i == len(array) - 1:
continue
if array[i] < array[i + 1] and array[i] < array[i - 1]:
localMinIdxs.append(i)
return localMinIdxs
def expandFromLocalMinIdx(localMinIdx, scores, rewards):
leftIdx = localMinIdx - 1
while leftIdx >= 0 and scores[leftIdx] > scores[leftIdx + 1]:
rewards[leftIdx] = max(rewards[leftIdx], rewards[leftIdx + 1] + 1)
leftIdx -= 1
rightIdx = localMinIdx + 1
while rightIdx < len(scores) and scores[rightIdx] > scores[rightIdx - 1]:
rewards[rightIdx] = rewards[rightIdx - 1] + 1
rightIdx += 1
# Solution #3
# O(n) time | O(n) space
def minRewards(score):
rewards = [1 for _ in score]
for i in range(1, len(score)):
if score[i] > score[i - 1]:
rewards[i] = rewards[i - 1] + 1
for i in reversed((range(len(score) - 1))):
if score[i] > score[i + 1]:
rewards[i] = max(rewards[i], rewards[i + 1] + 1)
return sum(rewards)