Created
May 30, 2014 18:50
-
-
Save zyzyis/2b6ffee4648ecd534f67 to your computer and use it in GitHub Desktop.
Candy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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