GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — tech-code-interview/min_rewards.py at master · BarthJr/tech-code-interview Expand file tree/ Copy pathmin_rewards.pyMore file actions 29 lines (23 loc) · 1.25 KB / Copy pathmin_rewards.pyFile metadata and controls29 lines (23 loc) · 1.25 KB 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 # Imagine that you're a teacher who's just graded the final exam in a class. # You have a list of student scores on the final exam in a particular order (not necessarily sorted), # and you want to reward your students. You decide to do so fairly by giving them arbitrary rewards following two rules: # first, all students must receive at least one reward; # second, any given student must receive strictly more rewards # than an adjacent student (a student immediately to the left or to the right) # with a lower score and must receive strictly fewer rewards than an adjacent student with a higher score. # Assume that all students have different scores; in other words, the scores are all unique. # Write a function that takes in a list of scores and returns the minimum number of rewards that you must give out # to students, all the while satisfying the two rules. """ >>> minRewards([8, 4, 2, 1, 3, 6, 7, 9, 5]) 25 """ def minRewards(scores): rewards = [1 for _ in scores] for i in range(1, len(rewards)): 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 sum(rewards) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |