Problem 374557 · medium · Level 03 Linear Management & Searching

Prize Ribbons by Score

greedy · two passes · local constraints

Children stand in a line; scores[i] is the score of the i-th child. Every child receives at least one ribbon, and any child whose score is strictly higher than an immediate neighbour must receive strictly more ribbons than that neighbour. Children with equal scores may receive any amounts. Return the minimum total number of ribbons.

Examples

Input:  scores = [1, 3, 2, 2, 1]
Output: 7
Explanation: Ribbons [1, 2, 1, 2, 1]: the 3 beats both neighbours, the second 2 beats the final 1.
Input:  scores = [5, 5, 5]
Output: 3

Constraints

  • 0 <= len(scores) <= 10**5, 0 <= scores[i] <= 10**6.
  • Target complexity: O(n) time, O(n) space.

Goals

  • Satisfy the left-neighbour rule in a forward pass and the right-neighbour rule in a backward pass
  • Combine the two passes with a maximum so both rules hold
Starting Python…