135. Candy

class Solution {
    public int candy(int[] ratings) {
        int[] candies=new int[ratings.length];
        for(int i=0;i<ratings.length;i++)
            candies[i]=(i!=0&&ratings[i-1]<ratings[i])?candies[i-1]+1:1;
        for(int i=ratings.length-2;i>=0;i--)
            if(ratings[i]>ratings[i+1])
                candies[i]=Math.max(candies[i],candies[i+1]+1);
        int res=0;
        for(int candy:candies)
            res+=candy;
        return res;
    }
}

  

posted @ 2017-10-08 12:10  Weiyu Wang  阅读(149)  评论(0编辑  收藏  举报