-
Notifications
You must be signed in to change notification settings - Fork 0
/
Candy
50 lines (30 loc) · 897 Bytes
/
Candy
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// User function Template for Java
class Solution {
static int minCandy(int N, int ratings[])
{
// code here
int n = ratings.length;
int[] mincandy = new int[n];
Arrays.fill(mincandy, 1);
for (int i = 1; i < n; i++)
{
if (ratings[i]> ratings[i - 1])
{
mincandy[i] = mincandy[i - 1] + 1;
}
}
for (int i = n - 2; i >= 0; i--)
{
if (ratings[i] >ratings[i + 1])
{
mincandy[i] = Math.max(mincandy[i], mincandy[i +1] + 1);
}
}
int sum = 0;
for (int c : mincandy)
{
sum += c;
}
return sum;
}
}