forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Number_Of_Chocolates.cpp
More file actions
54 lines (52 loc) · 1.48 KB
/
Minimum_Number_Of_Chocolates.cpp
File metadata and controls
54 lines (52 loc) · 1.48 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
/* 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 */
#include <iostream>
using namespace std;
int getMin(int *arr, int n)
{
int *dp = new int[n];
dp[0] = 1;
for (int i = 1; i < n; i++)
{
if (arr[i] > arr[i - 1])
{
dp[i] = dp[i - 1] + 1;
}
else
{
dp[i] = 1;
}
}
for (int i = n - 2; i >= 0; i--)
{
if (arr[i + 1] < arr[i] && dp[i] <= dp[i + 1])
{
dp[i] = dp[i + 1] + 1;
}
}
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += dp[i];
}
delete[] dp;
return sum;
}