Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java (Java)

Code before:
     * @param n The number of columns in the grid.
     * @return The number of unique paths.
     */
    public static int uniquePaths(int m, 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]; // Update the number of unique paths for each cell
            }
        }
        return dp[n - 1]; // The result is stored in the last column of the array
    }

    /**
     * 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(int m, 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; // Initialize the first column to 1 (one way to reach each cell)
        }
        for (int j = 0; j < n; j++) {
            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]; // Update the number of unique paths for each cell
            }
        }
        return dp[m - 1][n - 1]; // The result is stored in the bottom-right cell of the array
    }
}


Review comment (suggestion):
```suggestion
    public static int uniquePaths2(final int m, final int n) {
```

Code after:
     * @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] = Math.addExact(dp[j], dp[j - 1]); // Update the number of unique paths for each cell
            }
        }
        return dp[n - 1]; // The result is stored in the last column of the array
    }

    /**
     * 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; // Initialize the first column to 1 (one way to reach each cell)
        }
        for (int j = 0; j < n; j++) {
            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] = 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]; // The result is stored in the bottom-right cell of the array
    }
}