#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>
This commit is contained in:
Manan Solanki
2023-09-14 23:15:16 +05:30
committed by GitHub
parent 34cf6dab28
commit 5bb54977fe
3 changed files with 104 additions and 83 deletions

View File

@ -1,59 +1,71 @@
/** /**
* Author: Siddhant Swarup Mallick * Author: Siddhant Swarup Mallick
* Github: https://github.com/siddhant2002 * 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). * 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 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 * The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
* below). How many possible unique paths are there? * 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.
*/ */
/** Program description - To find the number of unique paths possible */
package com.thealgorithms.dynamicprogramming; 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) { private UniquePaths(){};
int[] dp = new int[n];
Arrays.fill(dp, 1); /**
* 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 i = 1; i < m; i++) {
for (int j = 1; j < n; j++) { 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 dp[n - 1]; // The result is stored in the last column of the array
// return true if predicted answer matches with expected answer
} }
// The above method runs in O(n) time /**
public static boolean uniquePaths2(int m, int n, int ans) { * Calculates the number of unique paths using a 2D dynamic programming array.
int[][] dp = new int[m][n]; * 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++) { 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++) { 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 i = 1; i < m; i++) {
for (int j = 1; j < n; j++) { 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 dp[m - 1][n - 1]; // The result is stored in the bottom-right cell of the array
// return true if predicted answer matches with expected answer
} }
// 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)
*/

View File

@ -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));
}
}

View File

@ -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));
}
}