|
1 | 1 | /**
|
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: |
7 | 6 | * A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
|
8 | 7 | * 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. |
11 | 14 | */
|
12 | 15 |
|
13 |
| -/** Program description - To find the number of unique paths possible */ |
14 |
| - |
15 | 16 | package com.thealgorithms.dynamicprogramming;
|
16 | 17 |
|
17 |
| -import java.util.*; |
| 18 | +import java.util.Arrays; |
18 | 19 |
|
19 |
| -public class UniquePaths { |
| 20 | +public final class UniquePaths { |
20 | 21 |
|
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) |
24 | 39 | for (int i = 1; i < m; i++) {
|
25 | 40 | 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 |
27 | 42 | }
|
28 | 43 | }
|
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 |
31 | 45 | }
|
32 | 46 |
|
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 |
36 | 58 | 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) |
38 | 60 | }
|
39 | 61 | 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) |
41 | 63 | }
|
42 | 64 | for (int i = 1; i < m; i++) {
|
43 | 65 | 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 |
45 | 67 | }
|
46 | 68 | }
|
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 |
49 | 70 | }
|
50 |
| - // The above mthod takes O(m*n) time |
51 | 71 | }
|
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 |
| - */ |
|
0 commit comments