Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Jaiharishan wants to merge 6 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from Jaiharishan:master
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Dynamic-Programming/UniquePaths.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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/
*/

// 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 }
19 changes: 19 additions & 0 deletions Dynamic-Programming/tests/UniquePaths.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { uniquePaths } from '../UniquePaths'

test('Base Case 1', () => {
const rows = 3
const cols = 7
expect(uniquePaths(rows, cols)).toBe(28)
})

test('Base Case 2', () => {
const rows = 3
const cols = 2
expect(uniquePaths(rows, cols)).toBe(3)
})

test('Base Case 3', () => {
const rows = 8
const cols = 14
expect(uniquePaths(rows, cols)).toBe(77520)
})

AltStyle によって変換されたページ (->オリジナル) /