-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandies.java
More file actions
74 lines (64 loc) · 2.17 KB
/
Copy pathCandies.java
File metadata and controls
74 lines (64 loc) · 2.17 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Alice is a kindergarden teacher. She wants to give some
candies to the children in her class. All the children
sit in a line (their positions are fixed), and each of
them has a rating score according to his or her
performance in the class. Alice wants to give at least
1 candy to each child. If two children sit next to each
other, then the one with the higher rating must get more
candies. Alice wants to save money, so she needs to
minimize the total number of candies given to the children.
Link: https://www.hackerrank.com/challenges/candies
*/
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int numChildren = s.nextInt();
int[] ratings = new int[numChildren];
for (int i = 0; i < numChildren; i++) {
ratings[i] = s.nextInt();
}
long candies = solveMinCandies(ratings);
System.out.println(candies);
}
public static long solveMinCandies(int[] ratings) {
int[] candy = new int[ratings.length];
Arrays.fill(candy, 0);
candy[0] = 1;
for (int i = 1; i < ratings.length; i++) {
if (candy[i] != 0)
continue;
if (ratings[i] > ratings[i-1]) {
candy[i] = candy[i-1] + 1;
}else if (ratings[i] < ratings[i-1]) {
findMinCandy(ratings, candy, i-1);
}else {
candy[i] = 1;
}
}
return sum(candy);
}
private static int findMinCandy(int[] rating, int[] candy, int index) {
if (index >= rating.length)
return 0;
if (index == rating.length-1) {
candy[index] = 1;
}else if (rating[index] > rating[index+1]) {
int newCount = 1 + findMinCandy(rating, candy, index+1);
candy[index] = Math.max(candy[index], newCount);
}else {
candy[index] = 1;
}
return candy[index];
}
private static long sum(int[] arr) {
long sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
}