From 5bb54977fe63107c22116215023cb18fc5500113 Mon Sep 17 00:00:00 2001 From: Manan Solanki <76104205+Manan-09@users.noreply.github.com> Date: Thu, 14 Sep 2023 23:15:16 +0530 Subject: [PATCH] #4369 Enhance UniquePaths (#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 <65706193+vil02@users.noreply.github.com> --- .../dynamicprogramming/UniquePaths.java | 80 +++++++++++-------- .../dynamicprogramming/UniquePathsTests.java | 58 ++++++++++++++ .../others/UniquePathsTests.java | 49 ------------ 3 files changed, 104 insertions(+), 83 deletions(-) create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/UniquePathsTests.java delete mode 100644 src/test/java/com/thealgorithms/others/UniquePathsTests.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java b/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java index 2cfb8ef25..c48bdea2d 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java @@ -1,59 +1,71 @@ /** - * Author : Siddhant Swarup Mallick - * Github : https://github.com/siddhant2002 - */ - -/** + * Author: Siddhant Swarup Mallick + * Github: https://github.com/siddhant2002 + *

+ * 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? + * 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? + *

+ * 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. */ -/** Program description - To find the number of unique paths possible */ - package com.thealgorithms.dynamicprogramming; -import java.util.*; +import java.util.Arrays; -public class UniquePaths { +public final class UniquePaths { - public static boolean uniquePaths(int m, int n, int ans) { - int[] dp = new int[n]; - Arrays.fill(dp, 1); + 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] += dp[j - 1]; + dp[j] = Math.addExact(dp[j], dp[j - 1]); // Update the number of unique paths for each cell } } - return dp[n - 1] == ans; - // return true if predicted answer matches with expected answer + return dp[n - 1]; // The result is stored in the last column of the array } - // The above method runs in O(n) time - public static boolean uniquePaths2(int m, int n, int ans) { - int[][] dp = new int[m][n]; + /** + * 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; + 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; + 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] = dp[i - 1][j] + dp[i][j - 1]; + 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] == ans; - // return true if predicted answer matches with expected answer + return dp[m - 1][n - 1]; // The result is stored in the bottom-right cell of the array } - // The above mthod takes O(m*n) time } -/** - * OUTPUT : - * Input - m = 3, n = 7 - * Output: it returns either true if expected answer matches with the predicted answer else it - * returns false 1st approach Time Complexity : O(n) Auxiliary Space Complexity : O(n) Input - m = - * 3, n = 7 Output: it returns either true if expected answer matches with the predicted answer else - * it returns false 2nd approach Time Complexity : O(m*n) Auxiliary Space Complexity : O(m*n) - */ diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/UniquePathsTests.java b/src/test/java/com/thealgorithms/dynamicprogramming/UniquePathsTests.java new file mode 100644 index 000000000..f6a86e72a --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/UniquePathsTests.java @@ -0,0 +1,58 @@ +package com.thealgorithms.dynamicprogramming; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +public class UniquePathsTests { + + @Test + public void testUniquePaths_3x3() { + assertEquals(6, UniquePaths.uniquePaths(3, 3)); + } + + @Test + public void testUniquePaths_1x1() { + assertEquals(1, UniquePaths.uniquePaths(1, 1)); + } + + @Test + public void testUniquePaths_3x7() { + assertEquals(28, UniquePaths.uniquePaths(3, 7)); + } + + @Test + public void testUniquePaths_7x3() { + assertEquals(28, UniquePaths.uniquePaths(7, 3)); + } + + @Test + public void testUniquePaths_100x100() { + assertThrows(ArithmeticException.class, () -> UniquePaths.uniquePaths(100, 100)); + } + + @Test + public void testUniquePaths2_3x3() { + assertEquals(6, UniquePaths.uniquePaths2(3, 3)); + } + + @Test + public void testUniquePaths2_1x1() { + assertEquals(1, UniquePaths.uniquePaths2(1, 1)); + } + + @Test + public void testUniquePaths2_3x7() { + assertEquals(28, UniquePaths.uniquePaths2(3, 7)); + } + + @Test + public void testUniquePaths2_7x3() { + assertEquals(28, UniquePaths.uniquePaths2(7, 3)); + } + + @Test + public void testUniquePaths2_100x100() { + assertThrows(ArithmeticException.class, () -> UniquePaths.uniquePaths2(100, 100)); + } +} diff --git a/src/test/java/com/thealgorithms/others/UniquePathsTests.java b/src/test/java/com/thealgorithms/others/UniquePathsTests.java deleted file mode 100644 index 42a11c441..000000000 --- a/src/test/java/com/thealgorithms/others/UniquePathsTests.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.thealgorithms.others; - -import static org.junit.jupiter.api.Assertions.*; - -import com.thealgorithms.dynamicprogramming.UniquePaths; -import org.junit.jupiter.api.Test; - -public class UniquePathsTests { - - @Test - void testForOneElement() { - assertTrue(UniquePaths.uniquePaths(3, 7, 28)); - } - - @Test - void testForTwoElements() { - assertTrue(UniquePaths.uniquePaths(3, 2, 3)); - } - - @Test - void testForThreeElements() { - assertTrue(UniquePaths.uniquePaths(3, 3, 6)); - } - - @Test - void testForFourElements() { - assertTrue(UniquePaths.uniquePaths(4, 6, 56)); - } - - @Test - void testForFiveElements() { - assertTrue(UniquePaths.uniquePaths2(3, 5, 15)); - } - - @Test - void testForSixElements() { - assertTrue(UniquePaths.uniquePaths2(6, 2, 6)); - } - - @Test - void testForSevenElements() { - assertTrue(UniquePaths.uniquePaths2(5, 9, 495)); - } - - @Test - void testForEightElements() { - assertTrue(UniquePaths.uniquePaths2(4, 8, 120)); - } -}