-
- Notifications
You must be signed in to change notification settings - Fork 5.8k
UniquePaths2 and HornerScheme #1240
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Saimon398 wants to merge 5 commits into TheAlgorithms:master Choose a base branch from Saimon398: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.
Open
Changes from 1 commit
Commits
Show all changes
5 commits Select commit Hold shift + click to select a range
d648030 algorithm: add UniquePaths2 algorithm
Saimon398 1005207 test: add test for UniquePaths2.js
Saimon398 b888548 algorithm: add HornerScheme algorithm
Saimon398 d4e1ec9 test: add test for Maths/HornerScheme.js
Saimon398 be56ea7 fix: fixed spelling errors in Horner Scheme tests
Saimon398 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
algorithm: add UniquePaths2 algorithm
This algorithm allows to count ways to reach right-bottom corner on the grid with obtacles
- Loading branch information
commit d64803057fe74dd6756024d789ae03104cbec10d
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,80 @@ | ||
| /* | ||
| * Unique Paths 2 | ||
| * | ||
| * 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 grid with obstacles | ||
| * An obstacle and space are marked as 1 or 0 respectively in grid. | ||
| * A path that the robot takes cannot include any square that is an obstacle. | ||
| * 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-ii/ | ||
| */ | ||
| | ||
| /** | ||
| * @description Return 'rows x columns' grid with cells filled by 'filler' | ||
| * @param {Number} rows Number of rows in the grid | ||
| * @param {Number} columns Number of columns in the grid | ||
| * @param {String | Number | Boolean} filler The value to fill cells | ||
| * @returns {Object []} | ||
| */ | ||
| const generateMatrix = (rows, columns, filler = 0) => { | ||
| const matrix = [] | ||
| for (let i = 0; i < rows; i += 1) { | ||
| const submatrix = [] | ||
| for (let k = 0; k < columns; k += 1) { | ||
| submatrix[k] = filler | ||
| } | ||
| matrix[i] = submatrix | ||
| } | ||
| return matrix | ||
| } | ||
| | ||
| /** | ||
| * @description Return number of unique paths | ||
| * @param {Object []} obstacles Obstacles grid | ||
| * @returns {Number} | ||
| */ | ||
| const uniquePaths2 = (obstacles) => { | ||
| if (!(obstacles instanceof Object)) { | ||
Saimon398 marked this conversation as resolved. Show resolved Hide resolved | ||
| throw new Error('Input data must be type of Array') | ||
| } | ||
| // Create grid for calculating number of unique ways | ||
| const rows = obstacles.length | ||
| const columns = obstacles[0].length | ||
| const grid = generateMatrix(rows, columns) | ||
| // Fill the outermost cell with 1 b/c it has | ||
| // the only way to reach neighbor | ||
| for (let i = 0; i < rows; i += 1) { | ||
| // If robot encounters an obstacle in these cells, | ||
| // he cannot continue movind in that direction | ||
Saimon398 marked this conversation as resolved. Show resolved Hide resolved | ||
| if (obstacles[i][0]) { | ||
| break | ||
| } | ||
| grid[i][0] = 1 | ||
| } | ||
| for (let j = 0; j < columns; j += 1) { | ||
| if (obstacles[0][j]) { | ||
| break | ||
| } | ||
| grid[0][j] = 1 | ||
| } | ||
| // Fill the rest of grid by dynamic programming | ||
| // using following reccurent formula: | ||
| // K[i][j] = K[i - 1][j] + K[i][j - 1] | ||
| for (let i = 1; i < rows; i += 1) { | ||
| for (let j = 1; j < columns; j += 1) { | ||
| if (obstacles[i][j]) { | ||
Saimon398 marked this conversation as resolved. Show resolved Hide resolved | ||
| grid[i][j] = 0 | ||
| } else { | ||
| grid[i][j] = grid[i - 1][j] + grid[i][j - 1] | ||
| } | ||
| } | ||
| } | ||
| return grid[rows - 1][columns - 1] | ||
| } | ||
| | ||
| export { uniquePaths2 } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.