Simple Java DP Solution O(N*M)


  • 0
    F
        /**
         * The States array (int[][] states) saves the state value ,and states[i][j] to indicate the
         * number of different paths to the (i, j) location
         * 
         */
        public int uniquePaths(int m, int n) {
            int[][] states = new int[101][101];
            states[1][1] = 1;
    
            for (int i = 1; i < m + 1; i++) {
                for (int j = 1; j < n + 1; j++) {
                    if (i == 1 && j == 1) {
                        continue;
                    }
                    states[i][j] = states[i - 1][j] + states[i][j - 1];
                }
            }
            states[1][1] = 0;
            return states[m][n];
        }
    

Log in to reply
 

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