Practice/PayPal/Leetcode 135. Candy
CodingMust
You are organizing a competition where children stand in a line, each assigned a performance rating. You need to distribute rewards to the children following these rules:
Your goal is to determine the minimum total number of rewards needed to satisfy these constraints.
Example 1:
Input: ratings = [1, 2, 3] Output: 6 Explanation: An optimal reward distribution is [1, 2, 3]. Each child has a higher rating than their left neighbor and receives more rewards accordingly. Total: 1 + 2 + 3 = 6
Example 2:
Input: ratings = [3, 2, 1] Output: 6 Explanation: An optimal reward distribution is [3, 2, 1]. Each child has a lower rating than their left neighbor and receives fewer rewards accordingly. Total: 3 + 2 + 1 = 6
Example 3:
` Input: ratings = [1, 0, 2] Output: 5 Explanation: An optimal reward distribution is [2, 1, 2].
Example 4:
Input: ratings = [1, 2, 2, 5, 4, 3, 2] Output: 12 Explanation: One optimal distribution is [1, 2, 1, 4, 3, 2, 1]. Note that children with equal ratings (indices 1 and 2 both have rating=2) can receive different rewards.