2ms Beats 98% Java submissions


  • 0
    A
    public class Solution {
        
        public int findPath(int[][]grid, int m, int n, int[][] memo) {
            
            if(m == 0 && n == 0)
                return grid[0][0];
                
            if(m < 0 || n < 0)
                return 1000 ;
            
            if(memo[m][n] > 0)
                return memo[m][n];
            
            memo[m][n] = grid[m][n] + Math.min(findPath(grid,m - 1, n,memo) , findPath(grid,m, n - 1,memo));
            return memo[m][n];
            
        }
        public int minPathSum(int[][] grid) {
            
            int[][] memo = new int[grid.length][grid[0].length];
            
            return findPath(grid, grid.length - 1, grid[0].length - 1,memo);
            
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.