File tree 1 file changed +33
-2
lines changed
1 file changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -19,8 +19,39 @@ def minRewards(score):
19
19
20
20
# Solution #2
21
21
# 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
24
55
25
56
26
57
You can’t perform that action at this time.
0 commit comments