Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 25, 2021 03:56 am GMT

LeetCode 62. Unique Paths(javascript solution)

Description:

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?

Solution:

Time Complexity : O(n^2)
Space Complexity: O(n)

var uniquePaths = function(m, n) {    // Create dp array    const dp = new Array(n + 1).fill(1);    // Populate dp array    for(let row = m - 1; row > 0; row--){        for(let col = n - 1; col > 0; col--){            dp[col] = dp[col] + dp[col + 1];        }    }    return dp[1];}

Original Link: https://dev.to/cod3pineapple/leetcode-62-unique-paths-javascript-solution-54m5

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To