Simple and clear JAVA Solution


  • 0
    J

    Only one array is given for dp storage.

    public class Solution {
        public int uniquePaths(int m, int n) {
            if (m == 0 || n == 0) {
                return 0;
            }
    
            int[] dp = new int[n];
            for (int i = 0; i < dp.length; i++) {
                dp[i] = 1;
            }
            for (int i = 1; i < m; i++) {
                for (int j = 1; j < n; j++) {
                    dp[j] = dp[j - 1] + dp[j];
                }
            }
            return dp[dp.length - 1];
        }
    }
    

Log in to reply
 

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