From a1b4b5afe025d939220610782a1afc0e6f139fe7 Mon Sep 17 00:00:00 2001 From: hiitesh1127 Date: Tue, 18 Oct 2022 19:27:39 -0400 Subject: [PATCH 1/3] dp problem --- Dynamic-Programming/UniquePaths.js | 41 +++++++++++++++++++ Dynamic-Programming/tests/UniquePaths.test.js | 13 ++++++ 2 files changed, 54 insertions(+) create mode 100644 Dynamic-Programming/UniquePaths.js create mode 100644 Dynamic-Programming/tests/UniquePaths.test.js diff --git a/Dynamic-Programming/UniquePaths.js b/Dynamic-Programming/UniquePaths.js new file mode 100644 index 0000000000..bea522c28e --- /dev/null +++ b/Dynamic-Programming/UniquePaths.js @@ -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 mxn grid + const paths = [] + for (let i = 0; i < n; i++) paths.push([...new Array(m).fill(1)]) + + // base case, position 1x1 has only 1 move + paths[1][1] = 1 + + for (let i = 1; i < n; i++) { + for (let j = 1; j < m; j++) { + paths[i][j] = paths[i - 1][j] + paths[i][j - 1] + } + } + return paths[n - 1][m - 1] +} + +export { uniquePaths } \ No newline at end of file diff --git a/Dynamic-Programming/tests/UniquePaths.test.js b/Dynamic-Programming/tests/UniquePaths.test.js new file mode 100644 index 0000000000..dce497de74 --- /dev/null +++ b/Dynamic-Programming/tests/UniquePaths.test.js @@ -0,0 +1,13 @@ +import { uniquePaths } from '../UniquePaths' + +describe('Unique Paths', () => { + it('should return 28 when m is 3 and n is 7', () => { + const data = uniquePaths(3, 7); + expect(data).toBe(28); + }) + + it('should return 48620 when m is 10 and n is 10', () => { + const data = uniquePaths(10, 10); + expect(data).toBe(48620); + }) +}) \ No newline at end of file From 807bbc03f9f2f4d352f405cf46db5c70e195df79 Mon Sep 17 00:00:00 2001 From: hiitesh1127 Date: Wed, 19 Oct 2022 03:01:18 -0400 Subject: [PATCH 2/3] update Directory.md --- DIRECTORY.md | 1 + Dynamic-Programming/UniquePaths.js | 12 ++++++------ Dynamic-Programming/tests/UniquePaths.test.js | 10 +++++----- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index e1bbd77000..b5cf0077a8 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -105,6 +105,7 @@ * [RodCutting](Dynamic-Programming/RodCutting.js) * [Shuf](Dynamic-Programming/Shuf.js) * [SieveOfEratosthenes](Dynamic-Programming/SieveOfEratosthenes.js) + * [UniquePaths](Dynamic-Programming/UniquePaths.js) * **Sliding-Window** * [LongestSubstringWithoutRepeatingCharacters](Dynamic-Programming/Sliding-Window/LongestSubstringWithoutRepeatingCharacters.js) * [PermutationinString](Dynamic-Programming/Sliding-Window/PermutationinString.js) diff --git a/Dynamic-Programming/UniquePaths.js b/Dynamic-Programming/UniquePaths.js index bea522c28e..e8e4250279 100644 --- a/Dynamic-Programming/UniquePaths.js +++ b/Dynamic-Programming/UniquePaths.js @@ -3,12 +3,12 @@ * * 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. + * 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`, + * + * 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/ */ @@ -38,4 +38,4 @@ const uniquePaths = (m, n) => { return paths[n - 1][m - 1] } -export { uniquePaths } \ No newline at end of file +export { uniquePaths } diff --git a/Dynamic-Programming/tests/UniquePaths.test.js b/Dynamic-Programming/tests/UniquePaths.test.js index dce497de74..c76c205828 100644 --- a/Dynamic-Programming/tests/UniquePaths.test.js +++ b/Dynamic-Programming/tests/UniquePaths.test.js @@ -2,12 +2,12 @@ import { uniquePaths } from '../UniquePaths' describe('Unique Paths', () => { it('should return 28 when m is 3 and n is 7', () => { - const data = uniquePaths(3, 7); - expect(data).toBe(28); + const data = uniquePaths(3, 7) + expect(data).toBe(28) }) it('should return 48620 when m is 10 and n is 10', () => { - const data = uniquePaths(10, 10); - expect(data).toBe(48620); + const data = uniquePaths(10, 10) + expect(data).toBe(48620) }) -}) \ No newline at end of file +}) From a3c8492a00abd4cd3eb8f8b2b170f103282498ce Mon Sep 17 00:00:00 2001 From: hiitesh1127 Date: Wed, 19 Oct 2022 06:30:13 -0400 Subject: [PATCH 3/3] suggested changes --- Dynamic-Programming/UniquePaths.js | 16 ++++++++-------- Dynamic-Programming/tests/UniquePaths.test.js | 6 ++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Dynamic-Programming/UniquePaths.js b/Dynamic-Programming/UniquePaths.js index e8e4250279..4b3d67a1a3 100644 --- a/Dynamic-Programming/UniquePaths.js +++ b/Dynamic-Programming/UniquePaths.js @@ -23,19 +23,19 @@ const uniquePaths = (m, n) => { // only one way to reach end if (m === 1 || n === 1) return 1 - // build a mxn grid - const paths = [] - for (let i = 0; i < n; i++) paths.push([...new Array(m).fill(1)]) - - // base case, position 1x1 has only 1 move - paths[1][1] = 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[i][j] = paths[i - 1][j] + paths[i][j - 1] + // 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[n - 1][m - 1] + return paths[m - 1] } export { uniquePaths } diff --git a/Dynamic-Programming/tests/UniquePaths.test.js b/Dynamic-Programming/tests/UniquePaths.test.js index c76c205828..eb6f8c74d4 100644 --- a/Dynamic-Programming/tests/UniquePaths.test.js +++ b/Dynamic-Programming/tests/UniquePaths.test.js @@ -2,12 +2,10 @@ import { uniquePaths } from '../UniquePaths' describe('Unique Paths', () => { it('should return 28 when m is 3 and n is 7', () => { - const data = uniquePaths(3, 7) - expect(data).toBe(28) + expect(uniquePaths(3, 7)).toBe(28) }) it('should return 48620 when m is 10 and n is 10', () => { - const data = uniquePaths(10, 10) - expect(data).toBe(48620) + expect(uniquePaths(10, 10)).toBe(48620) }) })