Used dfs, time limit exceeding for the longest input


  • 0
    N
    public class Solution {
        private int min = Integer.MAX_VALUE;
        public int minPathSum(int[][] grid) {
            dfs(grid, 0,0,grid[0][0]);
            return min;
        }
        
        private void dfs(int[][] grid, int row, int col, int sum){
            if(row>=grid.length-1 && col>=grid[row].length-1){
                min = Math.min(sum, min);
                return;
            }else if(row==grid.length-1){
                dfs(grid, row, col+1, sum+grid[row][col+1]);
            }else if(col==grid[row].length-1){
                dfs(grid, row+1, col, sum+grid[row+1][col]);
            }else{
                dfs(grid, row+1, col, sum+grid[row+1][col]);
                dfs(grid, row, col+1, sum+grid[row][col+1]);
            }
        }
    }
    

Log in to reply
 

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