An Interest In:
Web News this Week
- March 28, 2024
- March 27, 2024
- March 26, 2024
- March 25, 2024
- March 24, 2024
- March 23, 2024
- March 22, 2024
April 25, 2021 03:56 am GMT
Original Link: https://dev.to/cod3pineapple/leetcode-62-unique-paths-javascript-solution-54m5
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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To