forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
27 lines (26 loc) · 818 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Arrays;
public class Solution {
public int candy(int[] ratings) {
int len = ratings.length;
int[] left = new int[len];
int[] right = new int[len];
Arrays.fill(left, 1);
Arrays.fill(right, 1);
for (int i = 1; i < len; i++) {
if (ratings[i - 1] < ratings[i]) left[i] = left[i - 1] + 1;
}
for (int i = len - 2; i >= 0; i--) {
if (ratings[i + 1] < ratings[i]) right[i] = right[i + 1] + 1;
}
int total = 0;
for (int i = 0; i < len; i++) {
total += Math.max(left[i], right[i]);
}
return total;
}
public static void main(String[] args) {
int[] rat = {1, 2, 2};
Solution s = new Solution();
System.out.println(s.candy(rat));
}
}