Hi,
I am trying to solve the dp problem:
But it is showing some weird behavior for the input. Whenever I am trying to test with input case one by one it is giving me correct result but while trying to execute the testcases at once it is showing the wrong answere.
Code :
#include<bits/stdc++.h>
using namespace std;
const int N = 100;
vector<vector<int>> dp(N, vector<int>(N, -1));
int solve(vector<vector<int>>& arr , int n, int i, int j)
{
if (i == n || j == n)
{
return dp[i][j] = 0;
}
if (dp[i][j] != -1)
return dp[i][j];
return dp[i][j] = (arr[i][j] + max(solve(arr, n , i + 1, j) , solve(arr, n, i + 1, j + 1)));
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<vector<int>> arr(n, vector<int>(n, 0));
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
cin >> arr[i][j];
}
}
cout << solve(arr, n, 0, 0) << endl;
for(int i = 0;i<100;i++)
{
dp[i].clear();
}
}
}
Please some could suggest where I am doing mistake.
Thanks.