medium +20 pts

Candy Distribution

Give each child at least one candy and more than neighbors with minimum total candies.

You are given a list of non-negative integers `ratings` representing each child's rating. You must distribute candies to the children according to these rules: 1. Each child must have at least one candy. 2. Children with a higher rating than their immediate neighbor must receive more candies than that neighbor. Write a function `minimum_candies(ratings)` that returns the **minimum** total number of candies required to satisfy the rules. The list may be empty — return `0` in that case. For example, for `ratings = [1, 0, 2]`, one optimal distribution is `[2, 1, 2]`, giving a total of `5`. Note that the distribution is not unique, but the minimum total is what matters.

Constraints

- `0 <= len(ratings) <= 10^5` - `0 <= ratings[i] <= 10^5` - Your solution should run in `O(n)` time and `O(n)` or `O(1)` extra space (beyond the output/input).

Example

```python
>>> minimum_candies([1, 0, 2])
5
>>> minimum_candies([1, 2, 2])
4
>>> minimum_candies([])
0
```
20 points ~20 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Try a two-pass approach: left to right, then right to left.
Think of each child's candies as needing to satisfy only the neighbor they are higher than.
An empty list should return 0 — handle that first.
The final candies for each child is the maximum of the left and right constraints, and at least 1.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.