Skip to content

Commit 457d88b

Browse files
Merge pull request #507 from kashika13/unique-paths
Added unique paths DP problem in Java
2 parents e9844d2 + 06ffa99 commit 457d88b

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
/*
3+
Description: Unique Paths
4+
5+
A robot is located at the top-left corner of an m x n grid.
6+
The robot can only move either down or right at any point in time.
7+
8+
The robot is trying to reach the bottom-right corner of the grid.
9+
How many possible unique paths are there?
10+
11+
Example:
12+
Input: m = 3, n = 7
13+
Output: 28
14+
*/
15+
16+
public class unique_paths {
17+
18+
// -----------------------------------------------------------
19+
// 1. BRUTE FORCE (Recursive)
20+
// -----------------------------------------------------------
21+
// Idea:
22+
// From any cell (i, j), you can move either:
23+
// → Right (i, j+1)
24+
// → Down (i+1, j)
25+
//
26+
// So total paths = paths from right + paths from down.
27+
//
28+
// Base cases:
29+
// - If we reach the last row or last column, there’s only 1 path.
30+
//
31+
// Time Complexity: O(2^(m+n)) (exponential)
32+
// Space Complexity: O(m + n) (recursion call stack)
33+
public static int uniquePathsBrute(int m, int n) {
34+
// Base case: if we are at the bottom or right edge, only one path
35+
if (m == 1 || n == 1) return 1;
36+
37+
// Recursive calls: go down and right
38+
return uniquePathsBrute(m - 1, n) + uniquePathsBrute(m, n - 1);
39+
}
40+
41+
42+
// -----------------------------------------------------------
43+
// 2. OPTIMIZED DP (Bottom-Up Tabulation)
44+
// -----------------------------------------------------------
45+
// Idea:
46+
// Each cell [i][j] represents number of ways to reach that cell.
47+
// You can come either from the top [i-1][j] or from the left [i][j-1].
48+
//
49+
// dp[i][j] = dp[i-1][j] + dp[i][j-1]
50+
//
51+
// Base case:
52+
// - First row and first column have only 1 way to reach.
53+
//
54+
// Time Complexity: O(m * n)
55+
// Space Complexity: O(m * n)
56+
public static int uniquePathsDP(int m, int n) {
57+
int[][] dp = new int[m][n];
58+
59+
// Fill the first row and first column with 1
60+
for (int i = 0; i < m; i++) dp[i][0] = 1;
61+
for (int j = 0; j < n; j++) dp[0][j] = 1;
62+
63+
// Fill remaining cells using the recurrence relation
64+
for (int i = 1; i < m; i++) {
65+
for (int j = 1; j < n; j++) {
66+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
67+
}
68+
}
69+
70+
return dp[m - 1][n - 1];
71+
}
72+
73+
74+
// -----------------------------------------------------------
75+
// 3. OPTIMIZED (Space-Optimized DP)
76+
// -----------------------------------------------------------
77+
// Observation:
78+
// We only need the previous row to calculate the current row.
79+
//
80+
// Use a 1D array instead of 2D.
81+
//
82+
// Time Complexity: O(m * n)
83+
// Space Complexity: O(n)
84+
public static int uniquePathsOptimized(int m, int n) {
85+
int[] dp = new int[n];
86+
87+
// Initialize first row as 1
88+
for (int j = 0; j < n; j++) dp[j] = 1;
89+
90+
// Update dp for each subsequent row
91+
for (int i = 1; i < m; i++) {
92+
for (int j = 1; j < n; j++) {
93+
dp[j] += dp[j - 1];
94+
}
95+
}
96+
97+
return dp[n - 1];
98+
}
99+
100+
101+
// -----------------------------------------------------------
102+
// MAIN METHOD (Test all versions)
103+
// -----------------------------------------------------------
104+
public static void main(String[] args) {
105+
int m = 3, n = 7; // Example grid size
106+
107+
System.out.println("Brute Force Result: " + uniquePathsBrute(m, n));
108+
System.out.println("DP Result: " + uniquePathsDP(m, n));
109+
System.out.println("Optimized Result: " + uniquePathsOptimized(m, n));
110+
}
111+
}

0 commit comments

Comments
 (0)