Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
Contribute: Added algorithm and tests for Unique Paths DP problem#1118
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
Open
Jaiharishan wants to merge6 commits intoTheAlgorithms:masterChoose a base branch fromJaiharishan:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
781a7cf
Contribute: Added algorithm for Unique Paths problem
Jaiharishancb8b0e1
Fix: Fixed Package conflicts
Jaiharishan0496b33
Fix: Names for test cases are changed
Jaiharishanb721d48
Fix: Made all test cases to go under single block
Jaiharishane1c8bd1
Fix: Made the test cases to come under single Test block
Jaiharishan3dac5aa
Improve: More comments added for better understanding
JaiharishanFile 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* A Dynamic Programming based solution for calculating the number ways to travel from Top-Left of the matrix to Bottom-Right of the matrix | ||
* https://leetcode.com/problems/unique-paths/ | ||
* Problem Statement: | ||
* There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). 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. | ||
* Approach: | ||
* As the given problem can be reduced to smaller and overlapping sub problems, we can use dynamic programming and memoization to solve this problem. | ||
* Time complexity: O(M * N) (M->ROWS | N->COLS) | ||
* Space complexity: O(M * N) (M->ROWS | N->COLS) | ||
*/ | ||
/** | ||
* @param {number} rows | ||
* @param {number} cols | ||
* @return {number} | ||
*/ | ||
// Return the number of unique paths, given the dimensions of rows and columns | ||
const uniquePaths = (rows, cols) => { | ||
let dp = new Array(cols).fill(1) | ||
for (let i = 1; i < rows; i++) { | ||
const tmp = [] | ||
for (let j = 0; j < cols; j++) { | ||
if (j === 0) { | ||
tmp[j] = dp[j] | ||
} else { | ||
tmp[j] = tmp[j - 1] + dp[j] | ||
} | ||
} | ||
dp = tmp | ||
} | ||
return dp.pop() | ||
} | ||
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,7 @@ | ||
import { uniquePaths } from '../UniquePaths' | ||
test('Test case for UniquePaths', () => { | ||
expect(uniquePaths(3, 7)).toBe(28) | ||
expect(uniquePaths(3, 2)).toBe(3) | ||
expect(uniquePaths(8, 14)).toBe(77520) | ||
}) |
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.