From 82ae8cc1e127bbb0a73fe4faa8114aecdb0f90d6 Mon Sep 17 00:00:00 2001 From: Ziqi Zhang Date: Fri, 10 Dec 2021 19:31:54 -0500 Subject: [PATCH] add UniquePath --- .../dynamicprogramming/UniquePaths.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java 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