Candy [C++][Easy]
IntuitionThe problem requires distributing candies to children based on their ratings, ensuring that:
Each child gets at least one candy.
A child with a higher
### Intuition
The problem requires distributing candies to children based on their ratings, ensuring that:
1. Each child gets at least one candy.
2. A child with a higher rating than their neighbor gets more candies.

To satisfy these conditions efficiently, we can use a **two-pass greedy approach**.

### Approach
1. **Initialize** a `candies` array where each child starts with `1` candy.
2. **Left-to-right pass:** Ensure that each child with a higher rating than their left neighbor gets more candies.
3. **Right-to-left pass:** Ensure that each child with a higher rating than their right neighbor also gets more candies.
4. **Sum up** all the values in the `candies` array to get the total candies needed.

### Complexity
- **Time complexity:** \(O(n)\)  
  - Two passes over the `ratings` array, each taking \(O(n)\).
- **Space complexity:** \(O(n)\)  
  - The `candies` array of size \(n\) is required.

### Code
```cpp
class Solution {
public:
    int candy(vector<int>& ratings) {
        int n = ratings.size();
        vector<int> candies(n, 1);

        for (int i = 1; i < n; ++i) {
            if (ratings[i] > ratings[i - 1]) {
                candies[i] = candies[i - 1] + 1;
            }
        }

        for (int i = n - 2; i >= 0; --i) {
            if (ratings[i] > ratings[i + 1]) {
                candies[i] = max(candies[i], candies[i + 1] + 1);
            }
        }

        return accumulate(candies.begin(), candies.end(), 0);
    }
};
```
['C++']