|
6 | 6 | // https://leetcode.com/problems/unique-paths-ii/ |
7 | 7 | public class uniquePath { |
8 | 8 |
|
9 | | - |
10 | 9 |
|
11 | | - public static boolean uniquePathone() { |
| 10 | + //O(M*N) |
| 11 | + public static int uniquePathwithObstacles(int arr[][]) { |
12 | 12 |
|
13 | 13 |
|
| 14 | + int r = arr.length; |
| 15 | + int c = arr[0].length; |
14 | 16 |
|
| 17 | + if (arr[0][0] == 1) { |
| 18 | + return 0; |
| 19 | + } |
| 20 | + |
| 21 | + arr[0][0] = 1; |
| 22 | + |
| 23 | + for (int i = 1; i < r; i++) { |
| 24 | + |
| 25 | + arr[i][0] = (arr[i][0] == 0 && arr[i - 1][0] == 1) ? 1 : 0; |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + for (int i = 1; i < r; i++) { |
| 30 | + |
| 31 | + arr[0][i] = (arr[0][i] == 0 && arr[0][i - 1] == 1) ? 1 : 0; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + for (int i = 1; i < r; i++) { |
| 36 | + |
| 37 | + for (int j = 1; j < c; j++) { |
| 38 | + |
| 39 | + if (arr[i][j] == 0) { |
| 40 | + arr[i][j] = arr[i - 1][j] + arr[i][j - 1]; |
| 41 | + } else { |
| 42 | + arr[i][j] = 0; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + return arr[r - 1][c - 1]; |
15 | 49 |
|
16 | 50 |
|
17 | 51 | } |
18 | 52 |
|
19 | 53 |
|
| 54 | + // Unique paths without any obstacles recursive and dp solution ... |
| 55 | + public int uniquePaths(int m, int n) { |
| 56 | + |
| 57 | + return solve(0, 0, m, n); |
| 58 | + } |
| 59 | +// recursive |
| 60 | + public int solve(int i, int j, int m, int n) { |
| 61 | + if (i >= m || j >= n) |
| 62 | + return 0; |
| 63 | + if (i == m - 1 && j == n - 1) |
| 64 | + return 1; |
| 65 | + return solve(i + 1, j, m, n) + solve(i, j + 1, m, n); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + int[][] dp = new int[101][101]; |
| 70 | +// dp ... |
| 71 | + public int uniquePathsDP(int m, int n) { |
| 72 | + |
| 73 | + for (int i = 0; i < m; i++) |
| 74 | + for (int j = 0; j < n; j++) |
| 75 | + dp[i][j] = -1; |
| 76 | + |
| 77 | + return solvedp(0, 0, m, n); |
| 78 | + } |
| 79 | + |
| 80 | + public int solvedp(int i, int j, int m, int n) { |
| 81 | + if (i >= m || j >= n) |
| 82 | + return 0; |
| 83 | + if (i == m - 1 && j == n - 1) |
| 84 | + return 1; |
| 85 | + if (dp[i][j] != -1) |
| 86 | + return dp[i][j]; |
| 87 | + return dp[i][j] = solve(i + 1, j, m, n) + solve(i, j + 1, m, n); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + // convert into 1d dp method... |
| 92 | + static int numberOfPaths(int m, int n) { |
| 93 | + // Create a 1D array to store results of subproblems |
| 94 | + int[] dp = new int[n]; |
| 95 | + dp[0] = 1; |
| 96 | + |
| 97 | + for (int i = 0; i < m; i++) { |
| 98 | + for (int j = 1; j < n; j++) { |
| 99 | + dp[j] += dp[j - 1]; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return dp[n - 1]; |
| 104 | + } |
| 105 | + |
| 106 | + // using combinatorics.. |
| 107 | + static int numberOfPaths2(int m, int n) { |
| 108 | + // We have to calculate m+n-2 C n-1 here |
| 109 | + // which will be (m+n-2)! / (n-1)! (m-1)! |
| 110 | + int path = 1; |
| 111 | + for (int i = n; i < (m + n - 1); i++) { |
| 112 | + path *= i; |
| 113 | + path /= (i - n + 1); |
| 114 | + } |
| 115 | + return path; |
| 116 | + } |
20 | 117 |
|
21 | 118 | public static void main(String[] args) { |
22 | 119 |
|
23 | | - int arr[][]={{0,0,0},{0,1,0},{0,0,0}}; |
24 | | - |
25 | | - |
| 120 | + int arr[][] = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}; |
| 121 | + |
| 122 | + |
26 | 123 |
|
27 | 124 | System.out.println(); |
28 | 125 |
|
|
0 commit comments