mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
algorithm: unique paths (#1211)
* dp problem * update Directory.md * suggested changes
This commit is contained in:
@ -105,6 +105,7 @@
|
|||||||
* [RodCutting](Dynamic-Programming/RodCutting.js)
|
* [RodCutting](Dynamic-Programming/RodCutting.js)
|
||||||
* [Shuf](Dynamic-Programming/Shuf.js)
|
* [Shuf](Dynamic-Programming/Shuf.js)
|
||||||
* [SieveOfEratosthenes](Dynamic-Programming/SieveOfEratosthenes.js)
|
* [SieveOfEratosthenes](Dynamic-Programming/SieveOfEratosthenes.js)
|
||||||
|
* [UniquePaths](Dynamic-Programming/UniquePaths.js)
|
||||||
* **Sliding-Window**
|
* **Sliding-Window**
|
||||||
* [LongestSubstringWithoutRepeatingCharacters](Dynamic-Programming/Sliding-Window/LongestSubstringWithoutRepeatingCharacters.js)
|
* [LongestSubstringWithoutRepeatingCharacters](Dynamic-Programming/Sliding-Window/LongestSubstringWithoutRepeatingCharacters.js)
|
||||||
* [PermutationinString](Dynamic-Programming/Sliding-Window/PermutationinString.js)
|
* [PermutationinString](Dynamic-Programming/Sliding-Window/PermutationinString.js)
|
||||||
|
41
Dynamic-Programming/UniquePaths.js
Normal file
41
Dynamic-Programming/UniquePaths.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* 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 }
|
11
Dynamic-Programming/tests/UniquePaths.test.js
Normal file
11
Dynamic-Programming/tests/UniquePaths.test.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { uniquePaths } from '../UniquePaths'
|
||||||
|
|
||||||
|
describe('Unique Paths', () => {
|
||||||
|
it('should return 28 when m is 3 and n is 7', () => {
|
||||||
|
expect(uniquePaths(3, 7)).toBe(28)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return 48620 when m is 10 and n is 10', () => {
|
||||||
|
expect(uniquePaths(10, 10)).toBe(48620)
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user