-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 111.java
More file actions
33 lines (27 loc) · 730 Bytes
/
Day 111.java
File metadata and controls
33 lines (27 loc) · 730 Bytes
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
28
29
30
31
32
33
class Solution {
public int minCandy(int arr[]) {
int n = arr.length;
if (n == 0) return 0;
int[] left = new int[n];
int[] right = new int[n];
for (int i = 0; i < n; i++) {
left[i] = 1;
right[i] = 1;
}
for (int i = 1; i < n; i++) {
if (arr[i] > arr[i - 1]) {
left[i] = left[i - 1] + 1;
}
}
for (int i = n - 2; i >= 0; i--) {
if (arr[i] > arr[i + 1]) {
right[i] = right[i + 1] + 1;
}
}
int total = 0;
for (int i = 0; i < n; i++) {
total += Math.max(left[i], right[i]);
}
return total;
}
}