LeetCode //135. Candy

135. Candy

There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.

You are giving candies to these children subjected to the following requirements:

  • Each child must have at least one candy.

  • Children with a higher rating get more candies than their neighbors.

Return the minimum number of candies you need to have to distribute the candies to the children.

 

Example 1:

Input: ratings = [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.

Example 2:

Input: ratings = [1,2,2]
Output: 4
Explanation: 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.

Constraints:

  • n == ratings.length
  • 1 < = n < = 2 ∗ 1 0 4 1 <= n <= 2 * 10^4 1<=n<=2104
  • 0 < = r a t i n g s [ i ] < = 2 ∗ 1 0 4 0 <= ratings[i] <= 2 * 10^4 0<=ratings[i]<=2104

From: LeetCode
Link: 134. Gas Station


Solution:

Ideas:
1. First of all, we know that each child must receive at least one candy. Therefore, we first allocate an array candies of size ratingsSize and initialize it to 1, representing that each child has received at least one candy.
2. In the first pass, we iterate the ratings from left to right, if a child’s rating is higher than the previous one (i.e., ratings[i] > ratings[i - 1]), then this child should receive more candies than the previous one, so we update the number of candies for this child as candies[i] = candies[i - 1] + 1.
3. In the second pass, we iterate the ratings from right to left, if a child’s rating is higher than the next one (i.e., ratings[i] > ratings[i + 1]), then this child should receive more candies than the next one. However, the child may have received more candies in the first pass, so we only update the number of candies for this child when the current number of candies is less than candies[i + 1] + 1.
4. Finally, we need to sum up all the candies, which is the minimum number of candies we need to distribute.
This algorithm ensures that each child gets more candies than their neighbor with a lower rating, and also makes sure the total number of candies is minimized. The time complexity is O(n) and the space complexity is also O(n), where n is the length of the ratings.
Code:
int candy(int* ratings, int ratingsSize) {
    if (ratingsSize <= 1) {
        return ratingsSize;
    }
    
    int *candies = (int*) malloc(sizeof(int) * ratingsSize);
    
    for (int i = 0; i < ratingsSize; i++) {
        candies[i] = 1;
    }
    
    for (int i = 1; i < ratingsSize; i++) {
        if (ratings[i] > ratings[i - 1]) {
            candies[i] = candies[i - 1] + 1;
        }
    }
    
    for (int i = ratingsSize - 2; i >= 0; i--) {
        if (ratings[i] > ratings[i + 1]) {
            candies[i] = candies[i] > candies[i + 1] ? candies[i] : candies[i + 1] + 1;
        }
    }
    
    int totalCandies = 0;
    
    for (int i = 0; i < ratingsSize; i++) {
        totalCandies += candies[i];
    }
    
    free(candies);
    
    return totalCandies;
}

你可能感兴趣的:(LeetCode,leetcode,算法,c语言)