I am getting TLE by doing this question by recursion and dp
heres the code:
#include<bits/stdc++.h>
using namespace std;
struct activity{
int A, B, C;
};
int hello1( int i, vectorv , int n );
int hello2( int i , vectorv , int n );
int hello3( int i , vectorv , int n );
int n;
int dp[100001][3];
int hello1( int i, vectorv , int n )
{
if(i == n)
return 0;
if(dp[i][0]!=INT_MAX )
return dp[i][0];
dp[i][0] = v[i].A + max( hello2(i+1, v , n), hello3(i+1 , v , n) );
return dp[i][0] ;
}
int hello2( int i , vectorv , int n)
{
if(i == n)
return 0;
if(dp[i][1] != INT_MAX )
return dp[i][1];
dp[i][1]= v[i].B + max( hello1(i+1 , v, n), hello3(i+1 , v, n));
return dp[i][1] ;
}
int hello3( int i, vectorv ,int n)
{
if(i == n)
return 0;
if( dp[i][2] != INT_MAX )
return dp[i][2];
dp[i][2] = v[i].C + max( hello2(i+1, v,n), hello1(i+1 , v,n));
return dp[i][2];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
vector<activity>v;
for(int i = 0; i<n; i++)
{
int x, y, z;
cin >> x >> y >> z;
v.push_back( {x,y,z} );
}
for(int i=0 ;i <=n;i++)
for(int j=0;j<3;j++)
dp[i][j]=INT_MAX;
int ans = max(hello1(0, v , n), max(hello2(0, v, n),hello3(0, v, n)) );
/*for(int i=0;i<n;i++)
{
for(int j=0;j<3;j++)
cout<<dp[i][j]<<" ";
cout<<endl;
}*/
cout<<ans<<endl;
}