Skip to content

Commit 5bb5497

Browse files
Manan-09vil02
andauthored
TheAlgorithms#4369 Enhance UniquePaths (TheAlgorithms#4373)
* Enhance UnquiePaths DP problem solution * Update testcases * Linter issue resolved * Code review comments * Code review comments * Code review comments * Code review comments --------- Co-authored-by: Piotr Idzik <[email protected]>
1 parent 34cf6da commit 5bb5497

File tree

3 files changed

+104
-83
lines changed

3 files changed

+104
-83
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,71 @@
11
/**
2-
* Author : Siddhant Swarup Mallick
3-
* Github : https://github.com/siddhant2002
4-
*/
5-
6-
/**
2+
* Author: Siddhant Swarup Mallick
3+
* Github: https://github.com/siddhant2002
4+
* <p>
5+
* Problem Description:
76
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
87
* The robot can only move either down or right at any point in time.
9-
* The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram
10-
* below). How many possible unique paths are there?
8+
* The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
9+
* How many possible unique paths are there?
10+
* <p>
11+
* Program Description:
12+
* This program calculates the number of unique paths possible for a robot to reach the bottom-right corner
13+
* of an m x n grid using dynamic programming.
1114
*/
1215

13-
/** Program description - To find the number of unique paths possible */
14-
1516
package com.thealgorithms.dynamicprogramming;
1617

17-
import java.util.*;
18+
import java.util.Arrays;
1819

19-
public class UniquePaths {
20+
public final class UniquePaths {
2021

21-
public static boolean uniquePaths(int m, int n, int ans) {
22-
int[] dp = new int[n];
23-
Arrays.fill(dp, 1);
22+
private UniquePaths(){};
23+
24+
/**
25+
* Calculates the number of unique paths using a 1D dynamic programming array.
26+
* Time complexity O(n*m)
27+
* Space complexity O(min(n,m))
28+
*
29+
* @param m The number of rows in the grid.
30+
* @param n The number of columns in the grid.
31+
* @return The number of unique paths.
32+
*/
33+
public static int uniquePaths(final int m, final int n) {
34+
if (m > n) {
35+
return uniquePaths(n, m); // Recursive call to handle n > m cases
36+
}
37+
int[] dp = new int[n]; // Create a 1D array to store unique paths for each column
38+
Arrays.fill(dp, 1); // Initialize all values to 1 (one way to reach each cell)
2439
for (int i = 1; i < m; i++) {
2540
for (int j = 1; j < n; j++) {
26-
dp[j] += dp[j - 1];
41+
dp[j] = Math.addExact(dp[j], dp[j - 1]); // Update the number of unique paths for each cell
2742
}
2843
}
29-
return dp[n - 1] == ans;
30-
// return true if predicted answer matches with expected answer
44+
return dp[n - 1]; // The result is stored in the last column of the array
3145
}
3246

33-
// The above method runs in O(n) time
34-
public static boolean uniquePaths2(int m, int n, int ans) {
35-
int[][] dp = new int[m][n];
47+
/**
48+
* Calculates the number of unique paths using a 2D dynamic programming array.
49+
* Time complexity O(n*m)
50+
* Space complexity O(n*m)
51+
*
52+
* @param m The number of rows in the grid.
53+
* @param n The number of columns in the grid.
54+
* @return The number of unique paths.
55+
*/
56+
public static int uniquePaths2(final int m, final int n) {
57+
int[][] dp = new int[m][n]; // Create a 2D array to store unique paths for each cell
3658
for (int i = 0; i < m; i++) {
37-
dp[i][0] = 1;
59+
dp[i][0] = 1; // Initialize the first column to 1 (one way to reach each cell)
3860
}
3961
for (int j = 0; j < n; j++) {
40-
dp[0][j] = 1;
62+
dp[0][j] = 1; // Initialize the first row to 1 (one way to reach each cell)
4163
}
4264
for (int i = 1; i < m; i++) {
4365
for (int j = 1; j < n; j++) {
44-
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
66+
dp[i][j] = Math.addExact(dp[i - 1][j], dp[i][j - 1]); // Update the number of unique paths for each cell
4567
}
4668
}
47-
return dp[m - 1][n - 1] == ans;
48-
// return true if predicted answer matches with expected answer
69+
return dp[m - 1][n - 1]; // The result is stored in the bottom-right cell of the array
4970
}
50-
// The above mthod takes O(m*n) time
5171
}
52-
/**
53-
* OUTPUT :
54-
* Input - m = 3, n = 7
55-
* Output: it returns either true if expected answer matches with the predicted answer else it
56-
* returns false 1st approach Time Complexity : O(n) Auxiliary Space Complexity : O(n) Input - m =
57-
* 3, n = 7 Output: it returns either true if expected answer matches with the predicted answer else
58-
* it returns false 2nd approach Time Complexity : O(m*n) Auxiliary Space Complexity : O(m*n)
59-
*/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class UniquePathsTests {
8+
9+
@Test
10+
public void testUniquePaths_3x3() {
11+
assertEquals(6, UniquePaths.uniquePaths(3, 3));
12+
}
13+
14+
@Test
15+
public void testUniquePaths_1x1() {
16+
assertEquals(1, UniquePaths.uniquePaths(1, 1));
17+
}
18+
19+
@Test
20+
public void testUniquePaths_3x7() {
21+
assertEquals(28, UniquePaths.uniquePaths(3, 7));
22+
}
23+
24+
@Test
25+
public void testUniquePaths_7x3() {
26+
assertEquals(28, UniquePaths.uniquePaths(7, 3));
27+
}
28+
29+
@Test
30+
public void testUniquePaths_100x100() {
31+
assertThrows(ArithmeticException.class, () -> UniquePaths.uniquePaths(100, 100));
32+
}
33+
34+
@Test
35+
public void testUniquePaths2_3x3() {
36+
assertEquals(6, UniquePaths.uniquePaths2(3, 3));
37+
}
38+
39+
@Test
40+
public void testUniquePaths2_1x1() {
41+
assertEquals(1, UniquePaths.uniquePaths2(1, 1));
42+
}
43+
44+
@Test
45+
public void testUniquePaths2_3x7() {
46+
assertEquals(28, UniquePaths.uniquePaths2(3, 7));
47+
}
48+
49+
@Test
50+
public void testUniquePaths2_7x3() {
51+
assertEquals(28, UniquePaths.uniquePaths2(7, 3));
52+
}
53+
54+
@Test
55+
public void testUniquePaths2_100x100() {
56+
assertThrows(ArithmeticException.class, () -> UniquePaths.uniquePaths2(100, 100));
57+
}
58+
}

src/test/java/com/thealgorithms/others/UniquePathsTests.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)