from typing import List class Solution: def candy(self, ratings: List[int]) -> int: n = len(ratings) candies = [1] * n # Iterate from left to right for i in range(1, n): if ratings[i] > ratings[i - 1]: candies[i] = candies[i - 1] + 1 total_candies = candies[n - 1] # Iterate from right to left and update candies for i in range(n - 2, -1, -1): if ratings[i] > ratings[i + 1]: candies[i] = max(candies[i], candies[i + 1] + 1) total_candies += candies[i] return total_candies # Example usage if __name__ == "__main__": solution = Solution() ratings1 = [1, 0, 2] ratings2 = [1, 2, 2] print(solution.candy(ratings1)) # Expected output: 5 print(solution.candy(ratings2)) # Expected output: 4