Skip to content

Commit bc96d6f

Browse files
committed
Add solution #62
1 parent 9b54c25 commit bc96d6f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
53|[Maximum Subarray](./0053-maximum-subarray.js)|Easy|
5555
54|[Spiral Matrix](./0054-spiral-matrix.js)|Medium|
5656
58|[Length of Last Word](./0058-length-of-last-word.js)|Easy|
57+
62|[Unique Paths](./0062-unique-paths.js)|Medium|
5758
66|[Plus One](./0066-plus-one.js)|Easy|
5859
67|[Add Binary](./0067-add-binary.js)|Easy|
5960
69|[Sqrt(x)](./0069-sqrtx.js)|Medium|

solutions/0062-unique-paths.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 62. Unique Paths
3+
* https://leetcode.com/problems/unique-paths/
4+
* Difficulty: Medium
5+
*
6+
* There is a robot on an m x n grid. The robot is initially located at the top-left
7+
* corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner
8+
* (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any
9+
* point in time.
10+
*
11+
* Given the two integers m and n, return the number of possible unique paths that
12+
* the robot can take to reach the bottom-right corner.
13+
*/
14+
15+
/**
16+
* @param {number} m
17+
* @param {number} n
18+
* @return {number}
19+
*/
20+
var uniquePaths = function(m, n) {
21+
const row = new Array(n);
22+
23+
for (let i = 0; i < n; i++) {
24+
row[i] = 1;
25+
}
26+
27+
for (let i = 1; i < m; i++) {
28+
for (let j = 1; j < n; j++) {
29+
row[j] += row[j - 1];
30+
}
31+
}
32+
33+
return row[n - 1];
34+
};

0 commit comments

Comments
 (0)