[Leetcode] Candy solution report

Source: Internet
Author: User

[Question]

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?

Question]

Multiple children stand in a row and distribute the candy according to their scores. The children with higher scores get more candy than the children with lower scores. Each child gets at least one candy, q: How many sweets do I have to prepare at least?

[Idea]

Scan from left to right, so that the number of children who scored higher on the right is more than that on the left.

Scan again from right to left, so that the number of children who scored higher on the left is more than that on the right.

[Java code]

Public class solution {public int candy (INT [] ratings) {int size = ratings. length; If (size = 0) Return-1; if (size = 1) return 1; int [] cans = new int [size]; cans [0] = 1; // a Forward scan. If the rating on the right side is higher than that on the left side, the right side has one more candy than the left side, otherwise, only one candy for (INT I = 1; I <size; I ++) {If (ratings [I]> ratings [I-1]) cans [I] = cans [I-1] + 1; else cans [I] = 1;} // perform a reverse scan. If the rating on the left is higher than that on the right, and the number of sweets on the left side is less than that on the right side, so the number of sweets on the left side should be greater than that on the right side for (INT I = size-2; I> = 0; I --) {If (ratings [I]> ratings [I + 1] & cans [I] <= cans [I + 1]) {cans [I] = cans [I + 1] + 1 ;}} int ret = 0; For (INT I = 0; I <size; I ++) {RET + = cans [I];} return ret ;}}

This algorithm was indeed self-developed, and later found that the Internet is also such a solution, it is indeed very happy.

Although I thought it was right, I submitted it three or four times before the AC. First, you forget to judge (size = 1, in fact, there is no judgment on the second half of ratings [I]> ratings [I + 1] & cans [I] <= cans [I + 1, if the number of sweets on the left is more than that on the right, change it to cans [I] = cans [I + 1] + 1; then the cans [I] may be smaller.


[Leetcode] Candy solution report

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.