Skip to content

Instantly share code, notes, and snippets.

@zyzyis
Created May 30, 2014 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zyzyis/2b6ffee4648ecd534f67 to your computer and use it in GitHub Desktop.
Save zyzyis/2b6ffee4648ecd534f67 to your computer and use it in GitHub Desktop.
Candy.java
public class Solution {
public int candy(int[] ratings) {
if (ratings == null || ratings.length == 0)
return 0;
int count = ratings.length;
int[] candy = new int[ratings.length];
for (int i = 1; i < ratings.length; i ++)
if (candy[i] <= candy[i - 1] && ratings[i] > ratings[i - 1])
candy[i] = candy[i - 1] + 1;
for (int i = ratings.length - 2; i >= 0; i --)
if (candy[i] <= candy[i + 1] && ratings[i] > ratings[i + 1])
candy[i] = candy[i + 1] + 1;
for (int i : candy)
count += i;
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment