Skip to content

Commit 2cb4c2f

Browse files
siddhant2002siriak
andauthored
Add unique paths (fixes #2873) (#2943)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parent a30f08b commit 2cb4c2f

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/** Author : Siddhant Swarup Mallick
2+
* Github : https://github.com/siddhant2002
3+
*/
4+
5+
6+
/**
7+
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
8+
* 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 below).
10+
* How many possible unique paths are there?
11+
*/
12+
13+
/** Program description - To find the number of unique paths possible */
14+
15+
package com.thealgorithms.dynamicprogramming;
16+
17+
import java.util.*;
18+
19+
public class UniquePaths {
20+
public static boolean uniquePaths(int m , int n , int ans) {
21+
int []dp = new int[n];
22+
Arrays.fill(dp,1);
23+
for (int i=1; i<m; i++)
24+
{
25+
for (int j=1; j<n; j++)
26+
{
27+
dp[j] += dp[j-1];
28+
29+
}
30+
}
31+
return dp[n-1]==ans;
32+
// return true if predicted answer matches with expected answer
33+
}
34+
// The above method runs in O(n) time
35+
public static boolean uniquePaths2(int m , int n , int ans) {
36+
int dp[][] = new int[m][n];
37+
for (int i=0; i<m; i++)
38+
{
39+
dp[i][0] = 1;
40+
}
41+
for (int j=0; j<n; j++)
42+
{
43+
dp[0][j] = 1;
44+
}
45+
for (int i=1; i<m; i++)
46+
{
47+
for (int j=1; j<n; j++)
48+
{
49+
dp[i][j]=dp[i-1][j]+dp[i][j-1];
50+
}
51+
}
52+
return dp[m-1][n-1]==ans;
53+
// return true if predicted answer matches with expected answer
54+
}
55+
// The above mthod takes O(m*n) time
56+
}
57+
/**
58+
* OUTPUT :
59+
* Input - m = 3, n = 7
60+
* Output: it returns either true if expected answer matches with the predicted answer else it returns false
61+
* 1st approach Time Complexity : O(n)
62+
* Auxiliary Space Complexity : O(n)
63+
* Input - m = 3, n = 7
64+
* Output: it returns either true if expected answer matches with the predicted answer else it returns false
65+
* 2nd approach Time Complexity : O(m*n)
66+
* Auxiliary Space Complexity : O(m*n)
67+
*/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.others;
2+
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.thealgorithms.dynamicprogramming.UniquePaths;
6+
7+
8+
public class UniquePathsTests {
9+
@Test
10+
void testForOneElement()
11+
{
12+
assertTrue(UniquePaths.uniquePaths(3,7,28));
13+
}
14+
15+
@Test
16+
void testForTwoElements()
17+
{
18+
assertTrue(UniquePaths.uniquePaths(3,2,3));
19+
}
20+
21+
@Test
22+
void testForThreeElements()
23+
{
24+
assertTrue(UniquePaths.uniquePaths(3,3,6));
25+
}
26+
27+
@Test
28+
void testForFourElements()
29+
{
30+
assertTrue(UniquePaths.uniquePaths(4,6,56));
31+
}
32+
33+
@Test
34+
void testForFiveElements()
35+
{
36+
assertTrue(UniquePaths.uniquePaths2(3,5,15));
37+
}
38+
39+
@Test
40+
void testForSixElements()
41+
{
42+
assertTrue(UniquePaths.uniquePaths2(6,2,6));
43+
}
44+
45+
@Test
46+
void testForSevenElements()
47+
{
48+
assertTrue(UniquePaths.uniquePaths2(5,9,495));
49+
}
50+
51+
@Test
52+
void testForEightElements()
53+
{
54+
assertTrue(UniquePaths.uniquePaths2(4,8,120));
55+
}
56+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy