public int candy(int[] ratings) {
        if(ratings == null) return Integer.MAX_VALUE;
        if(ratings.length == 1) return 1;
        int start = 1;//descend sequence start value
        int former = start;//Former element value
        int total = 1;
        int counter = 0;//the serial number of descend sequence
        for(int i = 1;i<ratings.length;i++)
        {
            if(ratings[i] < ratings[i-1])
            {
                counter++;
               if(--start > 0) {
                   total += counter;
//                   counter = 0;
               }
                else
               {
                   total += counter+1;
               }
                former = 1;
            }
            else if(ratings[i] == ratings[i-1])
            {
               start = 1;
                total +=start;
                counter = 0;
                former = start;
            }
            else
            {
                ++former;
                total += former;
                start = former;
                counter = 0;
            }
        }
        return total;
    }