diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java b/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java new file mode 100644 index 000000000000..87f861f1de48 --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java @@ -0,0 +1,46 @@ +package com.thealgorithms.dynamicprogramming; + +/** + * 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 is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). + * How many possible unique paths are there? + */ + +public class UniquePaths { + public static void main(String[] args) { + int m = 3; + int n = 7; + System.out.println(uniquePaths2(3,7)); // result 28 + System.out.println(uniquePaths2(3,2)); // result 3 + System.out.println(uniquePaths2(3,3)); // result 6 + + System.out.println(uniquePaths(3,7)); // result 28 + System.out.println(uniquePaths(3,2)); // result 3 + System.out.println(uniquePaths(3,3)); // result 6 + } + + // O(n) space, dp + public static int uniquePaths(int m, int n) { + int []dp = new int[n]; + for (int j=0; j