In case it is a boundary point two options are not available for taking max but one option still needs to be considered and value updated accordingly .
You have initialized very low negative values to 0th row and 0th column which is Okay but you need to remove the check for i > 1 and j > 1 for it to be effective .
Regards
Vineet Paliwal .
In addition to the issue Vineet pointed out, I see you are using float…
I would recommend you to use double whenever it’s possible, as double allows more control when it comes to precision formatting, as it was the case in this problem…
Also, you can check my solution to this problem (I was the setter), where I use a slightly different approach than the one used on the editorial…
//constructs DP solution
int max_score(int*** array, int N)
{
int D[N][N];
D[0][0] = (*array)[0][0];
for(int i = 1; i < N; ++i)
D[i][0] = D[i-1][0]+(*array)[i][0];
for(int i = 1; i < N; ++i)
D[0][i] = D[0][i-1]+(*array)[0][i];
for(int i = 1; i < N; ++i)
{
for(int j = 1; j < N; ++j)
{
D[i][j] = max(D[i-1][j], D[i][j-1]) + (*array)[i][j];
}
}
return D[N-1][N-1];
}
The idea I use, takes advantage of the fact that on the 1st column and 1st row, you can only move down or to the right, so, the values of the sums are easy to compute for these two particular cases…
Then, once these 2 cases are handled, in the nested for loop, one can generate all the answers as:
without having to worry about setting some values to -inf, as both “bounds” are already defined after the computations for 1st row and column are done…
Hi vineet,
but the check does no harm, I got the correct answer anyway using double instead of float. but its needed when i=j=1
yeah but it indeed is a useless instruction that slows down the algo