Skip to content

Latest commit

 

History

History
46 lines (30 loc) · 559 Bytes

File metadata and controls

46 lines (30 loc) · 559 Bytes

Unique Paths

Problem Link

https://leetcode.com/problems/unique-paths/


Pattern

  • Grid DP

Approach

Count ways to reach each cell from the top or left neighbor.


Time Complexity

O(mn)

Space Complexity

O(n)


Java Solution

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