-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathUniquePaths.java
72 lines (66 loc) · 2.73 KB
/
UniquePaths.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Author: Siddhant Swarup Mallick
* Github: https://github.com/siddhant2002
* <p>
* Problem Description:
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
* The robot can only move either down or right at any point in time.
* The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
* How many possible unique paths are there?
* <p>
* Program Description:
* This program calculates the number of unique paths possible for a robot to reach the bottom-right corner
* of an m x n grid using dynamic programming.
*/
package com.thealgorithms.dynamicprogramming;
import java.util.Arrays;
public final class UniquePaths {
private UniquePaths() {
}
/**
* Calculates the number of unique paths using a 1D dynamic programming array.
* Time complexity O(n*m)
* Space complexity O(min(n,m))
*
* @param m The number of rows in the grid.
* @param n The number of columns in the grid.
* @return The number of unique paths.
*/
public static int uniquePaths(final int m, final int n) {
if (m > n) {
return uniquePaths(n, m); // Recursive call to handle n > m cases
}
int[] dp = new int[n]; // Create a 1D array to store unique paths for each column
Arrays.fill(dp, 1); // Initialize all values to 1 (one way to reach each cell)
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[j] = Math.addExact(dp[j], dp[j - 1]); // Update the number of unique paths for each cell
}
}
return dp[n - 1]; // The result is stored in the last column of the array
}
/**
* Calculates the number of unique paths using a 2D dynamic programming array.
* Time complexity O(n*m)
* Space complexity O(n*m)
*
* @param m The number of rows in the grid.
* @param n The number of columns in the grid.
* @return The number of unique paths.
*/
public static int uniquePaths2(final int m, final int n) {
int[][] dp = new int[m][n]; // Create a 2D array to store unique paths for each cell
for (int i = 0; i < m; i++) {
dp[i][0] = 1; // Initialize the first column to 1 (one way to reach each cell)
}
for (int j = 0; j < n; j++) {
dp[0][j] = 1; // Initialize the first row to 1 (one way to reach each cell)
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = Math.addExact(dp[i - 1][j], dp[i][j - 1]); // Update the number of unique paths for each cell
}
}
return dp[m - 1][n - 1]; // The result is stored in the bottom-right cell of the array
}
}