Skip to content

Commit f11e14b

Browse files
committed
Min Rewards
1 parent 7c1f0f0 commit f11e14b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

min_rewards.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Imagine that you're a teacher who's just graded the final exam in a class.
2+
# You have a list of student scores on the final exam in a particular order (not necessarily sorted),
3+
# and you want to reward your students. You decide to do so fairly by giving them arbitrary rewards following two rules:
4+
# first, all students must receive at least one reward;
5+
# second, any given student must receive strictly more rewards
6+
# than an adjacent student (a student immediately to the left or to the right)
7+
# with a lower score and must receive strictly fewer rewards than an adjacent student with a higher score.
8+
# Assume that all students have different scores; in other words, the scores are all unique.
9+
# Write a function that takes in a list of scores and returns the minimum number of rewards that you must give out
10+
# to students, all the while satisfying the two rules.
11+
12+
"""
13+
>>> minRewards([8, 4, 2, 1, 3, 6, 7, 9, 5])
14+
25
15+
"""
16+
17+
18+
def minRewards(scores):
19+
rewards = [1 for _ in scores]
20+
21+
for i in range(1, len(rewards)):
22+
if scores[i] > scores[i - 1]:
23+
rewards[i] = rewards[i - 1] + 1
24+
25+
for i in reversed(range(len(scores) - 1)):
26+
if scores[i] > scores[i + 1]:
27+
rewards[i] = max(rewards[i], rewards[i + 1] + 1)
28+
29+
return sum(rewards)

0 commit comments

Comments
 (0)