[leedcode 135] Candy

There are N children standing in a line. Each child is assigned a rating value.

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.

What is the minimum candies you must give?

public class Solution {
    public int candy(int[] ratings) {
        /*其实只要左右不比他大,就可以只分1个。如果左右比他大,左右就多分一个。这样的结果就是最省的。

        其实题目的描述本身就是一个很好的方案:初始化将每个人的糖果数都初始化为1。每次遍历只考虑左右一边,
        即从左向右遍历,如果i>i-1 则  candy[i]=candy[i-1]+1;再从右向左遍历一次,如果i>i+1 并且 candy[i]<=candy[i+1](注意!!)
        则 cand[i]=candy[i+1]+1; 这样的两次遍历,左右两边都顾及到了。最后将candy[i]相加求和就好了。
        每一边只考虑递增序列!!!*/
        if(ratings==null||ratings.length<1) return 0;
        int len=ratings.length;
        int candy[]=new int[len];
        int res=0;
        for(int i=0;i<len;i++){
            candy[i]=1;
        }
        for(int i=1;i<len;i++){
            if(ratings[i-1]<ratings[i]){
                candy[i]=candy[i-1]+1;
            }

        }
        for(int i=len-2;i>=0;i--){
            if(ratings[i+1]<ratings[i]&&candy[i]<=candy[i+1]){////
                candy[i]=candy[i+1]+1;
            }
        }
        for(int i=0;i<len;i++){
            res+=candy[i];
        }
        return res;

    }
}
时间: 2024-08-03 05:42:49

[leedcode 135] Candy的相关文章

LeetCode 135 Candy(贪心算法)

135. Candy There are N children standing in a line. Each child is assigned a rating value. 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 mo

[leet code 135]candy

1 题目 There are N children standing in a line. Each child is assigned a rating value. 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 can

LeetCode 135. Candy (O(n)时间 O(n)和O(1)空间的两种实现)

贪心算法,从局部最优推广成全局最优. 这里介绍O(n)时间 O(n)和O(1)空间的两种实现方法. O(n)时间 O(n)空间实现,参考了cnblog, 1957的解法 创建candy数组,初始化为1. 用pre_child_candy记录前一个孩子拿到的糖果数 1. 从左往右遍历 1) 如果ratings[i] > ratings[i-1], 那么candy[i] = ++ pre_child_candy, 亦即这个孩子比前一个孩子多拿一个糖果 2) 反之,candy[i] = pre_chi

leetcode[135] Candy

最少糖果问题.一排小孩,每个孩子有一个优先级,每个孩子至少要发给一个糖果,优先级高的比周围的孩子的糖果要多. 需要注意的是,优先级一样的没有要求说一样多糖果! 先初始化,每人一糖. 为了保证优先级大的比相邻的且优先级小的要糖果多.所以我们分两次处理,一次处理比左边的多,一次处理兼顾左边的多的情况下比右边的多.(如果优先级满足多的前提) 那么第一次从左到右,if (ratings[i] > ratings[i-1]) 那么cd[i] = cd[i-1]+1;    cd指糖果数组 从右边到左边需要

leetcode 135. Candy ----- java

There are N children standing in a line. Each child is assigned a rating value. 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

135. Candy(Array; Sort)

There are N children standing in a line. Each child is assigned a rating value. 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

135. Candy

There are N children standing in a line. Each child is assigned a rating value. 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

Java for LeetCode 135 Candy

There are N children standing in a line. Each child is assigned a rating value. 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

135 Candy 分配糖果

There are N children standing in a line. Each child is assigned a rating value.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 can