Advertisement
Guest User

62. Unique Paths | Time: O(nm) | Space: O(nm)

a guest
Aug 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. // Problem: https://leetcode.com/problems/unique-paths/
  2. // Solution: https://www.youtube.com/watch?v=6qMFjFC9YSc
  3.  
  4. class Solution {
  5.     public int uniquePaths(int m, int n) {
  6.         int[][] dp = new int[m][n];
  7.         for(int i = 0; i < m; i++) {
  8.             dp[i][0] = 1;
  9.         }
  10.        
  11.         for(int j = 0; j < n; j++) {
  12.             dp[0][j] = 1;
  13.         }
  14.        
  15.         for(int i = 1; i < m; i++) {
  16.             for(int j = 1; j < n; j++) {
  17.                 dp[i][j] = dp[i-1][j] + dp[i][j-1];
  18.             }
  19.         }
  20.        
  21.         return dp[m-1][n-1];
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement