Friday, April 24, 2015

HackerRank::Contests::Project Euler::Project Euler #18: Maximum path sum I

   This is a little dynamic task. So, we have an input Array A and the answer array ANS where ANS[I][J] shows us the best result of all the paths which end on the cell (I ; J) Initially all the elements of ANS equal to 0 except for the first cell which equals to A[1][1]. Now we loop over ANS for every cell ( I, J ) we can move either to the cell (I+1, J) or (I+1, J+1) and simply
ANS[i+1][j]=max(ANS[i+1][j],ANS[i][j]+A[i+1][j]);
ANS[i+1][j+1]=max(ANS[i+1][j+1],ANS[i][j]+A[i+1][j+1]);
 this basically means that we either leave the cell the way it was, or if the new path is better then we take the new path.



No comments:

Post a Comment