A recursive solution times out


  • 0
    G

    A recursive solution is a natural fit for this problem but the following solution times out. Could somebody improve it.

    class Solution {
        private int mps(int[][]grid, int r, int c) {
            int top = r==0 ? Integer.MAX_VALUE : mps(grid, r-1, c);
            int left = c==0 ? Integer.MAX_VALUE : mps(grid, r, c-1);
            return grid[r][c] + (r > 0 || c > 0 ? Math.min(top, left) : 0);
        }
        public int minPathSum(int[][] grid) {
            return mps(grid, grid.length-1, grid[0].length-1);
        }
    }
    

Log in to reply
 

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