Maximize the happiness of the groups on the Trip
Last Updated :
13 Sep, 2023
A trip to mystical land is going to be organized in ByteLand, the city of Bytes. Unfortunately, there are limited seats say A and there are N number of groups of people. Every group can have old person o, child c, man m and woman w. The organizing committee wants to maximize the happiness value of the trip. Happiness value of the trip is the sum of the happiness value of all the groups that are going. A group will go for the trip if every member can get a seat (Breaking a group is not a good thing).
- The happiness of child c = 4
- The happiness of woman w = 3
- The happiness of man m = 2
- The happiness of the old person o = 1
The happiness of group G, H(G) = (sum of happiness of people in it) * (number of people in the group).
The happiness of the group (‘coow’) = (4 + 1 + 1 + 3) * 4 = 36.
Given the groups and the total seating capacity, the task is to maximize the happiness and print the maximized happiness of the groups going on the trip.
Examples:
Input: groups[] = {“mmo”, “oo”, “cmw”, “cc”, “c”}, A = 5
Output: 43
Pick these groups [‘cmw’, ‘cc’] to get the maximum profit of (4 + 2 + 3) * 3 + (4 + 4) * 2 = 43
Input: groups[] = {“ccc”, “oo”, “cm”, “mm”, “wwo”}, A = 10
Output: 77
Approach: The problem can be considered as a slight modification of the 0-1 knapsack problem. The total seats available can be considered as the size of the knapsack. The happiness of each group can be considered as the profit of each item and the number of people in each group can be considered as the weight of each item. Now similar to the dynamic programming approach for 0-1 knapsack problem apply dynamic programming here to get the maximum happiness.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaxHappiness( int A, int N, vector<string> v)
{
string str;
int val[N], wt[N], c = 0;
for ( int i = 0; i < N; i++) {
str = v[i];
c = 0;
for ( int j = 0; str[j]; j++) {
if (str[j] == 'c' )
c += 4;
else if (str[j] == 'w' )
c += 3;
else if (str[j] == 'm' )
c += 2;
else
c++;
}
c *= str.length();
val[i] = c;
wt[i] = str.length();
}
int k[N + 1][A + 1];
for ( int i = 0; i <= N; i++) {
for ( int w = 0; w <= A; w++) {
if (i == 0 || w == 0)
k[i][w] = 0;
else if (wt[i - 1] <= w)
k[i][w] = max(val[i - 1]
+ k[i - 1][w - wt[i - 1]],
k[i - 1][w]);
else
k[i][w] = k[i - 1][w];
}
}
return k[N][A];
}
int main()
{
int A = 5;
vector<string> v = { "mmo" , "oo" , "cmw" , "cc" , "c" };
int N = v.size();
cout << MaxHappiness(A, N, v);
return 0;
}
|
Java
class GFG
{
static int maxHappiness( int A, int N, String[] v)
{
String str;
int [] val = new int [N];
int [] wt = new int [N];
int c = 0 ;
for ( int i = 0 ; i < N; i++)
{
str = v[i];
c = 0 ;
for ( int j = 0 ; j < str.length(); j++)
{
if (str.charAt(j) == 'c' )
c += 4 ;
else if (str.charAt(j) == 'w' )
c += 3 ;
else if (str.charAt(j) == 'm' )
c += 2 ;
else
c++;
}
c *= str.length();
val[i] = c;
wt[i] = str.length();
}
int [][] k = new int [N + 1 ][A + 1 ];
for ( int i = 0 ; i <= N; i++)
{
for ( int w = 0 ; w <= A; w++)
{
if (i == 0 || w == 0 )
k[i][w] = 0 ;
else if (wt[i - 1 ] <= w)
{
k[i][w] = Math.max(val[i - 1 ]+ k[i - 1 ][w - wt[i - 1 ]], k[i- 1 ][w]);
}
else
{
k[i][w] = k[i - 1 ][w];
}
}
}
return k[N][A];
}
public static void main(String[] args)
{
int A = 5 ;
String[] v = { "mmo" , "oo" , "cmw" , "cc" , "c" };
int N = v.length;
System.out.println(maxHappiness(A, N, v));
}
}
|
Python3
import numpy as np
def MaxHappiness(A, N, v) :
val = [ 0 ] * N; wt = [ 0 ] * N; c = 0 ;
for i in range (N) :
string = v[i];
c = 0 ;
for j in range ( len (string)) :
if (string[j] = = 'c' ) :
c + = 4 ;
elif (string[j] = = 'w' ) :
c + = 3 ;
elif (string[j] = = 'm' ) :
c + = 2 ;
else :
c + = 1 ;
c * = len (string);
val[i] = c;
wt[i] = len (string);
k = np.zeros((N + 1 , A + 1 ))
for i in range (N + 1 ) :
for w in range (A + 1 ) :
if (i = = 0 or w = = 0 ) :
k[i][w] = 0 ;
elif (wt[i - 1 ] < = w) :
k[i][w] = max (val[i - 1 ] +
k[i - 1 ][w - wt[i - 1 ]],
k[i - 1 ][w]);
else :
k[i][w] = k[i - 1 ][w];
return k[N][A];
if __name__ = = "__main__" :
A = 5 ;
v = [ "mmo" , "oo" , "cmw" , "cc" , "c" ];
N = len (v);
print (MaxHappiness(A, N, v));
|
C#
using System;
class GFG
{
static int maxHappiness( int A, int N,
String[] v)
{
String str;
int [] val = new int [N];
int [] wt = new int [N];
int c = 0;
for ( int i = 0; i < N; i++)
{
str = v[i];
c = 0;
for ( int j = 0; j < str.Length; j++)
{
if (str[j] == 'c' )
c += 4;
else if (str[j] == 'w' )
c += 3;
else if (str[j] == 'm' )
c += 2;
else
c++;
}
c *= str.Length;
val[i] = c;
wt[i] = str.Length;
}
int [ , ] k = new int [N + 1, A + 1];
for ( int i = 0; i <= N; i++)
{
for ( int w = 0; w <= A; w++)
{
if (i == 0 || w == 0)
k[i, w] = 0;
else if (wt[i - 1] <= w)
{
k[i, w] = Math.Max(val[i - 1]+
k[i - 1, w - wt[i - 1]],
k[i - 1, w]);
}
else
{
k[i, w] = k[i - 1, w];
}
}
}
return k[N, A];
}
public static void Main()
{
int A = 5;
String[] v = { "mmo" , "oo" , "cmw" , "cc" , "c" };
int N = v.Length;
Console.WriteLine(maxHappiness(A, N, v));
}
}
|
Javascript
<script>
function maxHappiness(A, N, v) {
let str;
let val = Array.from({length: N}, (_, i) => 0);
let wt = Array.from({length: N}, (_, i) => 0);
let c = 0;
for (let i = 0; i < N; i++)
{
str = v[i];
c = 0;
for (let j = 0; j < str.length; j++)
{
if (str[j] == 'c' )
c += 4;
else if (str[j] == 'w' )
c += 3;
else if (str[j] == 'm' )
c += 2;
else
c++;
}
c *= str.length;
val[i] = c;
wt[i] = str.length;
}
let k = new Array(N + 1);
for ( var i = 0; i < k.length; i++) {
k[i] = new Array(2);
}
for (let i = 0; i <= N; i++)
{
for (let w = 0; w <= A; w++)
{
if (i == 0 || w == 0)
k[i][w] = 0;
else if (wt[i - 1] <= w)
{
k[i][w] = Math.max(val[i - 1]+ k[i - 1][w - wt[i - 1]], k[i-1][w]);
}
else
{
k[i][w] = k[i - 1][w];
}
}
}
return k[N][A];
}
let A = 5;
let v = [ "mmo" , "oo" , "cmw" , "cc" , "c" ];
let N = v.length;
document.write(maxHappiness(A, N, v));
</script>
|
Time Complexity: O(N *(L + A))
Auxiliary Space: O(N * A), where N is the size of the vector of strings, L is the maximum length of a string in the vector and A is a given input.
Efficient approach : Space optimization
In previous approach the current value K[i][w] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.
Implementation steps:
- Create a 1D vector dp of size A+1 and initialize it with 0.
- Set a base case by initializing the values of DP .
- Now iterate over subproblems by the help of nested loop and get the current value from previous computations.
- Update Dp by iterating through subproblems
- At last return and print the final answer stored in dp[A] .
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int MaxHappiness( int A, int N, vector<string> v)
{
string str;
int val[N], wt[N], c = 0;
for ( int i = 0; i < N; i++) {
str = v[i];
c = 0;
for ( int j = 0; str[j]; j++) {
if (str[j] == 'c' )
c += 4;
else if (str[j] == 'w' )
c += 3;
else if (str[j] == 'm' )
c += 2;
else
c++;
}
c *= str.length();
val[i] = c;
wt[i] = str.length();
}
vector< int > dp(A + 1 , 0);
for ( int i = 1; i <= N; i++) {
for ( int w = A; w >= wt[i - 1]; w--) {
dp[w] = max(val[i - 1] + dp[w - wt[i - 1]], dp[w]);
}
}
return dp[A];
}
int main()
{
int A = 5;
vector<string> v = { "mmo" , "oo" , "cmw" , "cc" , "c" };
int N = v.size();
cout << MaxHappiness(A, N, v);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int maxHappiness( int A, int N, List<String> v) {
String str;
int [] val = new int [N];
int [] wt = new int [N];
int c;
for ( int i = 0 ; i < N; i++) {
str = v.get(i);
c = 0 ;
for ( int j = 0 ; j < str.length(); j++) {
char ch = str.charAt(j);
if (ch == 'c' )
c += 4 ;
else if (ch == 'w' )
c += 3 ;
else if (ch == 'm' )
c += 2 ;
else
c++;
}
c *= str.length();
val[i] = c;
wt[i] = str.length();
}
int [] dp = new int [A + 1 ];
for ( int i = 1 ; i <= N; i++) {
for ( int w = A; w >= wt[i - 1 ]; w--) {
dp[w] = Math.max(val[i - 1 ] + dp[w - wt[i - 1 ]], dp[w]);
}
}
return dp[A];
}
public static void main(String[] args) {
int A = 5 ;
List<String> v = Arrays.asList( "mmo" , "oo" , "cmw" , "cc" , "c" );
int N = v.size();
System.out.println(maxHappiness(A, N, v));
}
}
|
Python3
def MaxHappiness(A, N, v) - > int :
val = [ 0 ] * N
wt = [ 0 ] * N
for i in range (N):
c = 0
for j in range ( len (v[i])):
if v[i][j] = = 'c' :
c + = 4
elif v[i][j] = = 'w' :
c + = 3
elif v[i][j] = = 'm' :
c + = 2
else :
c + = 1
c * = len (v[i])
val[i] = c
wt[i] = len (v[i])
dp = [ 0 ] * (A + 1 )
for i in range ( 1 , N + 1 ):
for w in range (A, wt[i - 1 ] - 1 , - 1 ):
dp[w] = max (val[i - 1 ] + dp[w - wt[i - 1 ]], dp[w])
return dp[A]
if __name__ = = '__main__' :
A = 5
v = [ "mmo" , "oo" , "cmw" , "cc" , "c" ]
N = len (v)
print (MaxHappiness(A, N, v))
|
C#
using System;
public class MainClass {
public static int MaxHappiness( int A, int N, string [] v)
{
int [] val = new int [N];
int [] wt = new int [N];
for ( int i = 0; i < N; i++) {
int c = 0;
for ( int j = 0; j < v[i].Length; j++) {
if (v[i][j] == 'c' ) {
c += 4;
}
else if (v[i][j] == 'w' ) {
c += 3;
}
else if (v[i][j] == 'm' ) {
c += 2;
}
else {
c += 1;
}
}
c *= v[i].Length;
val[i] = c;
wt[i] = v[i].Length;
}
int [] dp = new int [A + 1];
for ( int i = 1; i <= N; i++) {
for ( int w = A; w >= wt[i - 1]; w--) {
dp[w] = Math.Max(
val[i - 1] + dp[w - wt[i - 1]], dp[w]);
}
}
return dp[A];
}
public static void Main()
{
int A = 5;
string [] v = { "mmo" , "oo" , "cmw" , "cc" , "c" };
int N = v.Length;
Console.WriteLine(
MaxHappiness(A, N, v));
}
}
|
Javascript
function MaxHappiness(A, N, v) {
let val = [];
let wt = [];
let c = 0;
for (let i = 0; i < N; i++) {
let str = v[i];
c = 0;
for (let j = 0; j < str.length; j++) {
if (str[j] === 'c' )
c += 4;
else if (str[j] === 'w' )
c += 3;
else if (str[j] === 'm' )
c += 2;
else
c++;
}
c *= str.length;
val[i] = c;
wt[i] = str.length;
}
let dp = new Array(A + 1).fill(0);
for (let i = 1; i <= N; i++) {
for (let w = A; w >= wt[i - 1]; w--) {
dp[w] = Math.max(val[i - 1] + dp[w - wt[i - 1]], dp[w]);
}
}
return dp[A];
}
function main() {
let A = 5;
let v = [ "mmo" , "oo" , "cmw" , "cc" , "c" ];
let N = v.length;
console.log(MaxHappiness(A, N, v));
}
main();
|
Time Complexity: O(N *(L + A))
Auxiliary Space: O(A)
Share your thoughts in the comments
Please Login to comment...