FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Dynamic-Programming/UniquePaths.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Dynamic-Programming
/
UniquePaths.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
40 lines (35 loc) · 1.15 KB
Breadcrumbs
JavaScript
/
Dynamic-Programming
/
UniquePaths.js
Copy path
File metadata and controls
40 lines (35 loc) · 1.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
*
* Unique Paths
*
* There is a robot on an `m x n` grid.
* The robot is initially located at the top-left corner.
* The robot tries to move to the bottom-right corner.
* The robot can only move either down or right at any point in time.
*
* Given the two integers `m` and `n`,
* return the number of possible unique paths that the robot can take to reach the bottom-right corner.
* More info: https://leetcode.com/problems/unique-paths/
*/
/*
*
@param
{
number
} m
*
@param
{
number
} n
*
@return
{
number
}
*/
const
uniquePaths
=
(
m
,
n
)
=>
{
// only one way to reach end
if
(
m
===
1
||
n
===
1
)
return
1
// build a linear grid of size m
// base case, position 1 has only 1 move
const
paths
=
new
Array
(
m
)
.
fill
(
1
)
for
(
let
i
=
1
;
i
<
n
;
i
++
)
{
for
(
let
j
=
1
;
j
<
m
;
j
++
)
{
// paths[j] in RHS represents the cell value stored above the current cell
// paths[j-1] in RHS represents the cell value stored to the left of the current cell
// paths [j] on the LHS represents the number of distinct pathways to the cell (i, j)
paths
[
j
]
=
paths
[
j
-
1
]
+
paths
[
j
]
}
}
return
paths
[
m
-
1
]
}
export
{
uniquePaths
}
Back
|
FazBrowse Home
|
New Git URL