Class Solution

java.lang.Object
g0101_0200.s0135_candy.Solution

public class Solution extends Object
135 - Candy.<p>Hard</p> <p>There are <code>n</code> children standing in a line. Each child is assigned a rating value given in the integer array <code>ratings</code>.</p> <p>You are giving candies to these children subjected to the following requirements:</p> <ul> <li>Each child must have at least one candy.</li> <li>Children with a higher rating get more candies than their neighbors.</li> </ul> <p>Return <em>the minimum number of candies you need to have to distribute the candies to the children</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> ratings = [1,0,2]</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> You can allocate to the first, second and third child with 2, 1, 2 candies respectively.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> ratings = [1,2,2]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> You can allocate to the first, second and third child with 1, 2, 1 candies respectively. The third child gets 1 candy because it satisfies the above two conditions.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == ratings.length</code></li> <li><code>1 <= n <= 2 * 10<sup>4</sup></code></li> <li><code>0 <= ratings[i] <= 2 * 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • candy

      public int candy(int[] ratings)