Minimum Points To Reach Destination | Practice | GeeksforGeeks

Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
 
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3 
       arr[][] = {{-2,-3,3}, 
                  {-5,-10,1}, 
                  {10,30,-5}}; 

Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Input: M = 3, N = 2
       arr[][] = {{2,3}, 
                  {5,10}, 
                  {10,30}}; 
Output: 1
Explanation: Take any path, all of them 
are positive. So, required one point 
at the start


Your Task:  
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value

Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)

Constraints:
1 ≤ N ≤ 103
0
artistdarkangel2 months ago
JAVA Code
TC: O(N*M) space : O(1) execution time : 0.5 sec
 
public int minPoints(int a[][],int m,int n) {     if(a[m-1][n-1]>0)        a[m-1][n-1]=0;    for(int i=n-2; i>=0; i--)    {        a[m-1][i]+=a[m-1][i+1];        if(a[m-1][i]>0)        {            a[m-1][i]=0;        }    }        for(int i=m-2; i>=0; i--)    {        a[i][n-1]+=a[i+1][n-1];        if(a[i][n-1]>0)        {            a[i][n-1]=0;        }    }            for(int i=m-2; i>=0; i--)    {        for(int j=n-2; j>=0; j--)        {            a[i][j]+=Math.max(a[i+1][j],a[i][j+1]);            if(a[i][j]>0)                a[i][j]=0;        }    }        return Math.abs(a[0][0])+1;} 
0
shivambhadani1236 months ago
	int minPoints(vector<vector<int>> points, int M, int N) 
	{
	    int dp[M][N];
	    dp[M-1][N-1] = 0;
	    if(points[M-1][N-1]<0) dp[M-1][N-1] = points[M-1][N-1];
	    for(int i=M-1; i>=0; i--) {
	        for(int j=N-1; j>=0; j--) {
	            if(i==M-1 && j==N-1) continue;
	            if(i+1<M && j+1<N) {
	                dp[i][j] = points[i][j] + max(dp[i+1][j], dp[i][j+1]);
	                if(dp[i][j]>0) dp[i][j] = 0;
	            }
	            else if(i+1<M) {
	                dp[i][j] = points[i][j] + dp[i+1][j];
	                if(dp[i][j]>0) dp[i][j] = 0;
	            }
	            else if(j+1<N) {
	                dp[i][j] = points[i][j] + dp[i][j+1];
	                if(dp[i][j]>0) dp[i][j] = 0;
	            }
	        }
	    }
	    return abs(dp[0][0])+1;
	}
0
CSE_010_Tanveer Ahmed10 months ago
CSE_010_Tanveer Ahmed
TC: O(N*M)  space : O(n*m)  execution time : 0.5 sec    int dp[1001][1001];    bool issafe(int r,int c, int rows,int cols){    if(r<rows &&="" c<cols)return="" true;="" return="" false;="" }="" int="" paths[2][2]="{{1,0},{0,1}};" int="" solve(vector<vector<int=""> > & points , int rows,int cols,int r,int c){    if(dp[r][c]!=INT_MAX) return dp[r][c];    if(r==rows-1 && c == cols- 1) {        //if the final destination is negative then return negative else return 0;        if(points[r][c]>=0) return dp[r][c]=0;        else return dp[r][c] = points[r][c];    }
   int down= INT_MIN,right= INT_MIN;        if(issafe(r+1,c,rows, cols))        {         down = solve(points,rows,cols,r+1,c) + points[r][c];         if(down >0) down = 0;        }        if(issafe(r,c+1 ,rows,  cols))        {           right  = solve(points,rows,cols,r,c+1)+ points[r][c];           if(right >0) right = 0;        }       dp[r][c] = max(down,right);       return  max(down,right);}int minPoints(vector<vector<int>> points, int M, int N) {     for(int i=0;i<m;i++)for(int j="0;j&lt;N;j++)dp[i][j]" =="" int_max;="" int="" result="solve(points,M,N,0,0);" return="" abs(result)+1;="" }="">
0
wallflower10 months ago
wallflower
Easy bottom up DP java
https://ide.geeksforgeeks.o...
0
AMAN SHAKYA1 year ago
AMAN SHAKYA
HINT :- SIMILAR QUESTION RAT IN  A MAZE PATH 1. DECLARE THE MEANING OF EACH CELL YOU CAN ABLE TO DO THIS QUESTION EASILY
0
yash makadia1 year ago
yash makadia
I read the editoral couple of times , still can't figure out why we can't go top left to bottom right. Can anyone explain
0
raghav2 years ago
raghav
 BEST SOLUTION[ EDITORIAL ] #define ll long long #define vi vector<int>class Solution{public:int minPoints(vector<vector<int>> p , int M, int N) {     vector< vi > t ;  t = p ;    t[M-1][N-1] = t[M-1][N-1] > 0 ? 1 : abs( t[M-1][N-1] ) + 1  ;     for( int i = M-2 ; i >= 0 ; i-- )t[i][N-1] = max( t[i+1][N-1] - p[i][N-1] , 1 );   for( int j = N-2 ; j >= 0 ; j-- )t[M-1][j] = max( t[M-1][j+1] - p[M-1][j] , 1 );     for( int i = M-2 ; i >= 0 ; i-- )   for( int j = N-2 ; j >= 0 ; j-- )   {        int c = min( t[i+1][j] , t[i][j+1] ) - p[i][j] ;         t[i][j] = max( c , 1 ) ;     }      return t[0][0] ;  } };
0
Projjal Sengupta2 years ago
Projjal Sengupta
For example 1, the output should be 8, right?. If not then can someone explain?
0
Wajahat Yusuf2 years ago
Wajahat Yusuf
Should be in the medium categorysimple recursive solutionhttps://ide.geeksforgeeks.o...
0
Vishal Kaushik2 years ago
Vishal Kaushik
In my code am getting error like first ans got print and then some garbage value is printed...help me in finding the errror.....code is given below
#include <iostream>using namespace std;
int helper(int **grid,int r,int c){    int **dp=new int*[r];    for(int i=0;i<r;i++) {="" dp[i]="new" int[c];="" for(int="" j="0;j&lt;c;j++)" {="" dp[i][j]="-1;" }="" }="" dp[r-1][c-1]="grid[r-1][c-1]">0?1:abs(grid[r-1][c-1])+1;    //fill last row    for(int i=r-2;i>=0;i--)    {        dp[i][c-1]=max(1,dp[i+1][c-1]-grid[i][c-1]);    }    for(int i=c-2;i>=0;i--)//fill the last column    {        dp[r-1][i]=max(1,dp[r-1][i+1]-grid[r-1][i]);    }    for(int i=r-2;i>=0;i--)    {        for(int j=c-2;j>=0;j--)        {            int a=min(dp[i+1][j],dp[i][j+1]);            dp[i][j]=max(1,a-grid[i][j]);        }    }    int ans=dp[0][0];    for(int i=0;i<r;i++) delete[]="" dp[i];="" return="" ans;="" }="" int="" main()="" {="" int="" t;="" cin="">>t;while(t!=0){    int r,c;    cin>>r>>c;    int **grid=new int*[r];    for(int i=0;i<r;i++) {="" grid[i]="new" int[c];="" for(int="" j="0;j&lt;c;j++)" {="" cin="">>grid[i][j];        }    }    int a=helper(grid,r,c);    cout<<a; t--;="" }="" return="" 0;="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.