Skip to content

Commit ab54dab

Browse files
author
Partho Biswas
committed
minn rewards code updated.
1 parent 545b336 commit ab54dab

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

algoexpert.io/python/Min_Rewards.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,39 @@ def minRewards(score):
1919

2020
# Solution #2
2121
# O(n) time | O(n) space
22-
def minRewards(score):
23-
pass
22+
def minRewards(scores):
23+
rewards = [1 for _ in scores]
24+
localMinIdxs = getLocalMinIdxs(scores)
25+
for localMinIdx in localMinIdxs:
26+
expandFromLocalMinIdx(localMinIdx, scores, rewards)
27+
return sum(rewards)
28+
29+
30+
def getLocalMinIdxs(array):
31+
if len(array) == 1:
32+
return [0]
33+
localMinIdxs = []
34+
for i in range(len(array)):
35+
if i == 0 and array[i] < array[i + 1]:
36+
localMinIdxs.append(i)
37+
if i == len(array) - 1 and array[i] < array[i - 1]:
38+
localMinIdxs.append(i)
39+
if i == 0 or i == len(array) - 1:
40+
continue
41+
if array[i] < array[i + 1] and array[i] < array[i - 1]:
42+
localMinIdxs.append(i)
43+
return localMinIdxs
44+
45+
46+
def expandFromLocalMinIdx(localMinIdx, scores, rewards):
47+
leftIdx = localMinIdx - 1
48+
while leftIdx >= 0 and scores[leftIdx] > scores[leftIdx + 1]:
49+
rewards[leftIdx] = max(rewards[leftIdx], rewards[leftIdx + 1] + 1)
50+
leftIdx -= 1
51+
rightIdx = localMinIdx + 1
52+
while rightIdx < len(scores) and scores[rightIdx] > scores[rightIdx - 1]:
53+
rewards[rightIdx] = rewards[rightIdx - 1] + 1
54+
rightIdx += 1
2455

2556

2657

0 commit comments

Comments
 (0)