Loading...
You are given an integer array scores. Position i has the score scores[i]. You must assign every position a number of units.
The assignment must follow two rules:
Adjacent means the positions directly to the left and to the right. Equal scores place no requirement on the units.
Return the minimum total number of units you can assign while following both rules.
Input: scores = [1,0,2]
Output: 5
Explanation: Assign 2, 1, 2 units. Position 0 outranks position 1 (1 > 0), so it needs more units than position 1. Position 2 also outranks position 1 (2 > 0), so it also needs more units than position 1. The total 2 + 1 + 2 = 5 is the smallest possible.
Input: scores = [1,2,2]
Output: 4
Explanation: Assign 1, 2, 1 units. Position 1 outranks position 0 (2 > 1), so it needs more units than position 0. Positions 1 and 2 have equal scores, so position 2 only needs the minimum of 1 unit. The total 1 + 2 + 1 = 4 is the smallest possible.
scores.length ≤5⋅104scores[i] ≤5⋅104Click "Run" to test with sample cases or "Submit" to run all tests.