Candy according to the rank of candy @LeetCode

Source: Internet
Author: User
Tags min

Some examples:

1 2 3 3 3 = "8 because the number of candy can be

1 2 3) 1 1

1 2 3 2 3 = "9 because the number of candy can be

1 2 3) 1 2


Ideas:

1

D[i] is to give the first child at least a few pieces of sugar rank[i] > rank[i-1], must be more than the previous one, d[i] = d[i-1] + 1 rank[i] = = Rank[i-1], two ranked the same, the second one on the line, d[i] = 1 Rank[i] < rank[i-1], lower than the previous ranking, should be less to a piece, but if the previous has only given a piece, you have to push forward one more to give. What time does it push? If the ranking is higher than the next high, the same sugar, you have to give again, until the relationship is broken (ranked or lower than the next, or sugar has been satisfied with the relationship) will not have to push forward. http://leetcodenotes.wordpress.com/2013/10/08/leetcode-candy-%E7%BB%99%E5%B0%8F%E5%AD%A9%E5%AD%90%E5%88%86%E7% b3%96/comment-page-1/#comment-123

Or

Use store[] to store the kid's candy result give first kid[0] Candy start Loop:from index 1 to N if the ratings[i] > Ratings[i-1], ascending order, just store[i] = store[i-1] + 1 if = =, we can make it down to 1 (according to the Condit ION) if the ratings[i] < ratings[i-1], descending order, store[i] = 1; At the same time. We have to check if the store[i] = = Store[i-1] While ratings[i-1] are higher if so, make this previous store + + continue, C Heck previous one, until find previous ratings is no high or previous store was not equal to the later one time (n^2) wors T time is {6,5,4,3,2,1} space (n) http://vialgorithms.blogspot.com/2013/10/candy.html


2 sweep from left to right, then right-to-left, so that the conditions of the problem can be satisfied. Record the total number of candy required during the second scan

Http://joycelearning.blogspot.com/2013/10/leetcode-candy.html

http://blog.csdn.net/violet_program/article/details/12233949


Package Level5; /** Candy There is N children standing in a line.

Each child is assigned a rating value. You is giving candies to these children subjected to the following Requirements:each child must has at least one candy
.
Children with a higher rating get more candies than their neighbors.

 What's the minimum candies you must give?
		*/public class S145 {public static void main (string[] args) {int[] ratings = {5,3,1};
	System.out.println (Candy (ratings));
		}//O (n) public static int candy (int[] ratings) {int len = ratings.length;
		if (len = = 0) {return 0;
		} int min = len;
		
		Int[] Candies = new Int[len];
		int cur = 0;
			From the 2nd child scan to the end, compare each child and his left neighbor for (int i=1; i<len; i++) {if (Ratings[i] > Ratings[i-1]) {cur++;
			}else{cur = 0;
		} Candies[i] = cur;
		} cur = 0;
			Scan from the penultimate child to the first child, compare each child and his right neighbor for (int i=len-2; i>=0; i--) {if (Ratings[i] > Ratings[i+1]) {cur++;
			}else{cur = 0; } Candies[i] =Math.max (Candies[i], cur);
		Min + = Candies[i];
		}//Don't forget to add the last child's candy number min + = candies[len-1];
	return min;
        }//O (n2) TLE public static int candy2 (int[] ratings) {if (ratings.length = = 0) {return 0;
        } int[] Candies = new Int[ratings.length];
        Candies[0] = 1;
        	for (int i=1; i<candies.length; i++) {if (Ratings[i] > Ratings[i-1]) {candies[i] = candies[i-1]+1;
        	}else if (ratings[i] = = Ratings[i-1]) {candies[i] = 1;
        		}else{Candies[i] = 1;
        			if (candies[i-1] = = 1) {int j = i;
        				while (J > 0 && ratings[j-1]>ratings[j]) {candies[j-1]++;
        			j--;
        }}}} int count = 0;
        for (int val:candies) {count + = val;
    } return count;
 }

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.