-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDistributeCandy.java
More file actions
33 lines (32 loc) · 805 Bytes
/
DistributeCandy.java
File metadata and controls
33 lines (32 loc) · 805 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
/**
Distribute minimum candies to the students such that one no two adjacent students are having equal number of candies
**/
public class Solution
{
public int candy(int[] A)
{
int candies[] = new int[A.length];
candies[0] = 1;
for(int i=1; i<candies.length; i++)
{
candies[i] = 1;
if(A[i] > A[i-1])
{
candies[i] = candies[i-1] + 1;
}
}
for(int j=A.length-1; j>0; j--)
{
if((A[j-1] > A[j]) && (candies[j-1]<=candies[j]))
{
candies[j-1] = candies[j]+1;
}
}
int sum = 0;
for(int i=0; i<candies.length; i++)
{
sum = sum + candies[i];
}
return sum;
}
}