Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
algorithm: unique paths#1211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
raklaptudirm merged 3 commits intoTheAlgorithms:masterfromhiitesh1127:hiitesh1127-patch-4Oct 20, 2022
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.