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 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