Geek is going for n days training program, he can perform any one of these three activities Running, Fighting, and Learning Practice. Each activity has some point on each day. As Geek wants to improve all his skills, he can’t do the same activity on two consecutive days, so help Geek to maximize his merit points as we were given a 2D array of n*3 points corresponding to each day and activity.
Examples:
Input: n = 3, point[] = [[1, 2, 5], [3, 1, 1], [3, 3, 3]] Output: 11 Explanation: Geek will learn a new move and earn 5 point then on second day he will do running and earn 3 point and on third day he will do fighting and earn 3 points so, maximum point is 11.
Approach: This can be solved with the following idea:
Using Dynamic Programming + Recursion, we can calculate the maximum points that we can get from choosing best activity for every index from given 3 activity.
Steps involved in the implementation of code:
- Choose the best activity at the current index with respect to the last activity.
- Base case if we are done with all day’s activity return 0.
- start with -1 or 0 as the last activity.
- Let 1 2 3 be the activity that we can do.
- If have done 1 as last then try to maximize through 2 and 3 activities at the current index.
- Store all possibilities and return the maximum that we can get.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int dp[100001][4];
int sub(vector<vector< int > >& points, int last, int index)
{
if (index == points.size())
return 0;
if (dp[index][last] != -1)
return dp[index][last];
int ans = 0;
if (last == 0) {
ans = max(ans,
max({ points[index][0]
+ sub(points, 1, index + 1),
points[index][1]
+ sub(points, 2, index + 1),
points[index][2]
+ sub(points, 3, index + 1) }));
}
else if (last == 1) {
ans = max(ans,
max(points[index][1]
+ sub(points, 2, index + 1),
points[index][2]
+ sub(points, 3, index + 1)));
}
else if (last == 2) {
ans = max(ans,
max(points[index][0]
+ sub(points, 1, index + 1),
points[index][2]
+ sub(points, 3, index + 1)));
}
else {
ans = max(ans,
max(points[index][1]
+ sub(points, 2, index + 1),
points[index][0]
+ sub(points, 1, index + 1)));
}
return dp[index][last] = ans;
}
int main()
{
int n = 3;
vector<vector< int > > points{ { 1, 2, 5 },
{ 3, 1, 1 },
{ 3, 3, 3 } };
memset (dp, -1, sizeof (dp));
cout << "Maximum Points We Can Make : "
<< sub(points, 0, 0) << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static int [][] dp;
static int sub( int [][] points, int last, int index)
{
if (index == points.length)
return 0 ;
if (dp[index][last] != - 1 )
return dp[index][last];
int ans = 0 ;
if (last == 0 ) {
ans = Math.max(
ans,
Math.max(
points[index][ 0 ]
+ sub(points, 1 , index + 1 ),
Math.max(
points[index][ 1 ]
+ sub(points, 2 , index + 1 ),
points[index][ 2 ]
+ sub(points, 3 , index + 1 ))));
}
else if (last == 1 ) {
ans = Math.max(
ans,
Math.max(points[index][ 1 ]
+ sub(points, 2 , index + 1 ),
points[index][ 2 ]
+ sub(points, 3 , index + 1 )));
}
else if (last == 2 ) {
ans = Math.max(
ans,
Math.max(points[index][ 0 ]
+ sub(points, 1 , index + 1 ),
points[index][ 2 ]
+ sub(points, 3 , index + 1 )));
}
else {
ans = Math.max(
ans,
Math.max(points[index][ 1 ]
+ sub(points, 2 , index + 1 ),
points[index][ 0 ]
+ sub(points, 1 , index + 1 )));
}
return dp[index][last] = ans;
}
public static void main(String[] args)
{
int n = 3 ;
int [][] points
= { { 1 , 2 , 5 }, { 3 , 1 , 1 }, { 3 , 3 , 3 } };
dp = new int [points.length][ 4 ];
for ( int i = 0 ; i < points.length; i++) {
Arrays.fill(dp[i], - 1 );
}
System.out.println( "Maximum Points We Can Make : "
+ sub(points, 0 , 0 ));
}
}
|
Python3
def sub(points, last, index, dp):
if index = = len (points):
return 0
if dp[index][last] ! = - 1 :
return dp[index][last]
ans = 0
if last = = 0 :
ans = max (
ans,
max (
points[index][ 0 ] + sub(points, 1 , index + 1 , dp),
points[index][ 1 ] + sub(points, 2 , index + 1 , dp),
points[index][ 2 ] + sub(points, 3 , index + 1 , dp),
),
)
elif last = = 1 :
ans = max (
ans,
max (
points[index][ 1 ] + sub(points, 2 , index + 1 , dp),
points[index][ 2 ] + sub(points, 3 , index + 1 , dp),
),
)
elif last = = 2 :
ans = max (
ans,
max (
points[index][ 0 ] + sub(points, 1 , index + 1 , dp),
points[index][ 2 ] + sub(points, 3 , index + 1 , dp),
),
)
else :
ans = max (
ans,
max (
points[index][ 1 ] + sub(points, 2 , index + 1 , dp),
points[index][ 0 ] + sub(points, 1 , index + 1 , dp),
),
)
dp[index][last] = ans
return ans
if __name__ = = "__main__" :
n = 3
points = [[ 1 , 2 , 5 ], [ 3 , 1 , 1 ], [ 3 , 3 , 3 ]]
dp = [[ - 1 for _ in range ( 4 )] for _ in range ( len (points))]
print ( "Maximum Points We Can Make:" , sub(points, 0 , 0 , dp))
|
C#
using System;
class Program {
static int [, ] dp = new int [100001, 4];
static int Sub( int [, ] points, int last, int index)
{
if (index == points.GetLength(0))
return 0;
if (dp[index, last] != -1)
return dp[index, last];
int ans = 0;
if (last == 0) {
ans = Math.Max(
ans,
Math.Max(
points[index, 0]
+ Sub(points, 1, index + 1),
Math.Max(
points[index, 1]
+ Sub(points, 2, index + 1),
points[index, 2]
+ Sub(points, 3, index + 1))));
}
else if (last == 1) {
ans = Math.Max(
ans,
Math.Max(points[index, 1]
+ Sub(points, 2, index + 1),
points[index, 2]
+ Sub(points, 3, index + 1)));
}
else if (last == 2) {
ans = Math.Max(
ans,
Math.Max(points[index, 0]
+ Sub(points, 1, index + 1),
points[index, 2]
+ Sub(points, 3, index + 1)));
}
else {
ans = Math.Max(
ans,
Math.Max(points[index, 1]
+ Sub(points, 2, index + 1),
points[index, 0]
+ Sub(points, 1, index + 1)));
}
return dp[index, last] = ans;
}
static void Main()
{
int [, ] points
= { { 1, 2, 5 }, { 3, 1, 1 }, { 3, 3, 3 } };
for ( int i = 0; i < dp.GetLength(0); i++) {
for ( int j = 0; j < dp.GetLength(1); j++) {
dp[i, j] = -1;
}
}
Console.WriteLine( "Maximum Points We Can Make: "
+ Sub(points, 0, 0));
}
}
|
Javascript
let dp = [];
function sub(points, last, index) {
if (index == points.length)
return 0;
if (dp[index][last] != -1)
return dp[index][last];
let ans = 0;
if (last == 0) {
ans = Math.max(
ans,
Math.max(
points[index][0] +
sub(points, 1, index + 1),
Math.max(
points[index][1] +
sub(points, 2, index + 1),
points[index][2] +
sub(points, 3, index + 1))));
}
else if (last == 1) {
ans = Math.max(
ans,
Math.max(points[index][1] +
sub(points, 2, index + 1),
points[index][2] +
sub(points, 3, index + 1)));
}
else if (last == 2) {
ans = Math.max(
ans,
Math.max(points[index][0] +
sub(points, 1, index + 1),
points[index][2] +
sub(points, 3, index + 1)));
}
else {
ans = Math.max(
ans,
Math.max(points[index][1] +
sub(points, 2, index + 1),
points[index][0] +
sub(points, 1, index + 1)));
}
return dp[index][last] = ans;
}
const n = 3;
const points = [
[1, 2, 5],
[3, 1, 1],
[3, 3, 3]
];
dp = Array.from({
length: points.length
}, () => Array(4).fill(-1))
console.log( "Maximum Points We Can Make : " +
sub(points, 0, 0));
|
Output
Maximum Points We Can Make : 11
Time Complexity : O(N*3) Auxiliary Space : O(N*4) or O(100001 * 4)
|