Minimize the total number of teddies to be distributed
Given N number of students sitting in a line and each of them is having marks they got scored in the exam. The task is to distribute teddy as they satisfy given conditions
- All students must have at least 1 teddy
- If two students sit next to each other then the one with the higher marks must get more teddies.
So, the task is to minimize the total number of teddies.
Examples:
Input: n = 3, marks = [ 1, 2, 2 ]
Output: 4
Here 1, 2, 2 is the marks.
Note that when two students have equal marks
they are allowed to have a different number of teddies.
Hence optimal distribution will be 1, 2, 1.
Input: n = 6, marks = [ 1, 4, 5, 2, 2, 1 ]
Output: 10
Approach: The approach is to use the dynamic programming to solve given problem:
- Initialize the table of size n.
- Run loop for n times.
- If left adjacent student is having higher marks review and change all the table values assigned before as solution until assigned table values as a solution are found wrong according to given constraints.
- If right adjacent student is having higher marks add one in solution for left adjacent and assign to solution for right one.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
long long fun( int marks[], int n)
{
long long dp[n], temp;
fill(dp, dp + n, 1);
for ( int i = 0; i < n - 1; i++)
{
if (marks[i] > marks[i + 1])
{
temp = i;
while ( true )
{
if ((marks[temp] > marks[temp + 1]) &&
temp >= 0)
{
if (dp[temp] > dp[temp + 1])
{
temp -= 1;
continue ;
}
else
{
dp[temp] = dp[temp + 1] + 1;
temp -= 1;
}
}
else
break ;
}
}
else if ( marks[i] < marks[i + 1])
dp[i + 1] = dp[i] + 1;
}
int sum = 0;
for ( int i = 0; i < n; i++)
sum += dp[i];
return sum;
}
int main()
{
int n = 6;
int marks[6] = { 1, 4, 5, 2, 2, 1};
cout << fun(marks, n);
return 0;
}
|
Java
public class GFG {
static long fun( int marks[], int n)
{
long dp[] = new long [n] ;
int temp;
for ( int i = 0 ;i < n;i ++)
dp[i] = 1 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (marks[i] > marks[i + 1 ])
{
temp = i;
while ( true )
{
if ((marks[temp] > marks[temp + 1 ]) &&
temp >= 0 )
{
if (dp[temp] > dp[temp + 1 ])
{
temp -= 1 ;
continue ;
}
else
{
dp[temp] = dp[temp + 1 ] + 1 ;
temp -= 1 ;
}
}
else
break ;
}
}
else if ( marks[i] < marks[i + 1 ])
dp[i + 1 ] = dp[i] + 1 ;
}
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += dp[i];
return sum;
}
public static void main(String args[])
{
int n = 6 ;
int marks[] = { 1 , 4 , 5 , 2 , 2 , 1 };
System.out.println(fun(marks, n));
}
}
|
Python 3
def fun(marks, n):
dp = [ 1 for i in range ( 0 , n) ]
for i in range ( 0 , n - 1 ):
if marks[i] > marks[i + 1 ]:
temp = i
while True :
if marks[temp] > marks[temp + 1 ] and temp > = 0 :
if dp[temp] > dp[temp + 1 ]:
temp - = 1
continue
else :
dp[temp] = dp[temp + 1 ] + 1
temp - = 1
else :
break
elif marks[i] < marks[i + 1 ]:
dp[i + 1 ] = dp[i] + 1
return ( sum (dp))
n = 6
marks = [ 1 , 4 , 5 , 2 , 2 , 1 ]
print (fun(marks, n))
|
C#
using System;
class GFG
{
public static long fun( int [] marks, int n)
{
long [] dp = new long [n];
long temp;
for ( int i = 0; i < n; i++)
dp[i] = 1;
for ( int i = 0; i < n - 1; i++)
{
if (marks[i] > marks[i + 1])
{
temp = i;
while ( true )
{
if ((marks[temp] > marks[temp + 1]) &&
temp >= 0)
{
if (dp[temp] > dp[temp + 1])
{
temp -= 1;
continue ;
}
else
{
dp[temp] = dp[temp + 1] + 1;
temp -= 1;
}
}
else
break ;
}
}
else if ( marks[i] < marks[i + 1])
dp[i + 1] = dp[i] + 1;
}
long sum = 0;
for ( int i = 0; i < n; i++)
sum += dp[i];
return sum;
}
static void Main()
{
int n = 6;
int [] marks = new int []{ 1, 4, 5, 2, 2, 1};
Console.Write( fun(marks, n) );
}
}
|
Javascript
<script>
function fun(marks, n)
{
let dp = new Array(n);
let temp;
for (let i = 0; i < n; i++)
dp[i] = 1;
for (let i = 0; i < n - 1; i++)
{
if (marks[i] > marks[i + 1])
{
temp = i;
while ( true )
{
if ((marks[temp] > marks[temp + 1]) &&
temp >= 0)
{
if (dp[temp] > dp[temp + 1])
{
temp -= 1;
continue ;
}
else
{
dp[temp] = dp[temp + 1] + 1;
temp -= 1;
}
}
else
break ;
}
}
else if ( marks[i] < marks[i + 1])
dp[i + 1] = dp[i] + 1;
}
let sum = 0;
for (let i = 0; i < n; i++)
sum += dp[i];
return sum;
}
let n = 6;
let marks = [ 1, 4, 5, 2, 2, 1];
document.write( fun(marks, n) );
</script>
|
Last Updated :
02 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...