-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum_no_of_chocolates.java
58 lines (45 loc) · 1.54 KB
/
Minimum_no_of_chocolates.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
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
Minimum Number of Chocolates
Send Feedback
Noor is a teacher. She wants to give some chocolates to the students in her class. All the students sit in a line and each of them has a score according to performance. Noor wants to give at least 1 chocolate to each student. She distributes chocolates to them such that If two students sit next to each other then the one with the higher score must get more chocolates. Noor wants to save money, so she wants to minimise the total number of chocolates.
Note that when two students have equal score they are allowed to have different number of chocolates.
Input Format:
First Line: Integer N, the number of students in Noor’s class.
Second Line: Each of the student's score separated by spaces.
Output Format:
Output a single line containing the minimum number of chocolates Noor must give.
Input Constraints
1 <= N <= 100000
1 <= score <= 100000
Sample Input:
4
1 4 4 6
sample Output:
6
Sample Input:
3
8 7 5
sample Output:
6
public class Solution {
public static int getMin(int arr[], int n){
int max =0;
int dp[] = new int[n+1];
dp[0]=1;
for(int i=1;i<n;i++){
if(arr[i]>arr[i-1]){
dp[i] = 1 + dp[i-1];
}else{
dp[i] = 1;
}
}
for(int i=n-2;i>=0;i--){
if(arr[i]>arr[i+1] && dp[i] <= dp[i+1]){
dp[i] = 1 + dp[i+1];
}
}
for(Integer num:dp){
max += num;
}
return max;
}
}