-
Notifications
You must be signed in to change notification settings - Fork 8
/
minRewards.py
61 lines (50 loc) · 1.83 KB
/
minRewards.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
# def minRewards(scores):
# rewards = [1 for _ in scores]
# for i in range(1,len(scores)):
# j=i-1
# if scores[i]>scores[j]:
# rewards[i]=rewards[j]+1
# else:
# # j=i-1 === j+1=i
# while j>=0 and scores[j]>scores[j+1]:
# rewards[j]=max(rewards[j],rewards[j+1]+1)
# j-=1
# return sum(rewards)
def minRewards(scores):
rewards = [1 for _ in scores]
localMinIdxs = getLocalMinScores(scores)
for localMinIdx in localMinIdxs:
expandFromLocalMinIdx(localMinIdx, scores, rewards)
return sum(rewards)
def getLocalMinScores(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
# def minRewards(scores):
# rewards = [1 for _ in 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 rewards