LeetCode135(2020-7-18)

题目:https://leetcode.com/problems/candy/
解答:

public int candy(int[] ratings) {
        if(ratings==null || ratings.length==0) {
            return 0;
        }
        int n = ratings.length;
        int[] right = new int[n];  // 相比右边
        int[] left = new int[n];  // 相比左边
        for(int i=0;iratings[i]) {
                    left[i+1]=left[i]+1;
                }
            }
        }
        for(int i=n-1;i>=0;i--) {
            if(i!=0) {
                if(ratings[i-1]>ratings[i]) {
                    right[i-1]=right[i]+1;
                }
            }
        }
        int index = 0;
        int min = left[0]+right[0];
        int sum = n;
        for(int i=0;i

你可能感兴趣的:(LeetCode135(2020-7-18))