Skip to content

Commit

Permalink
Problem 62
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvictorli committed Feb 24, 2019
1 parent d370bb8 commit 7cac00d
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"jest": true,
"es6": true
},
"plugins": ["prettier"],
"plugins": ["import", "prettier"],
"rules": {
"no-plusplus": "off",
"no-param-reassign": "off",
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -962,4 +962,21 @@ For example, pow(2, 10) should return 1024.

[Solution](https://github.com/Li-Victor/daily-coding-problem/blob/master/solutions/61-70/Problem61.js)

---

## Problem 62

This problem was asked by Facebook.

There is an N by M matrix of zeroes. Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. You can only move right or down.

For example, given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right:

- Right, then down
- Down, then right

Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right.

[Solution](https://github.com/Li-Victor/daily-coding-problem/blob/master/solutions/61-70/Problem62.js)

---
17 changes: 17 additions & 0 deletions companies/Facebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,21 @@ Given the multiset {15, 5, 20, 10, 35}, it would return false, since we can't sp

[Solution](https://github.com/Li-Victor/daily-coding-problem/blob/master/solutions/51-60/Problem60.js)

---

## Problem 62

This problem was asked by Facebook.

There is an N by M matrix of zeroes. Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. You can only move right or down.

For example, given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right:

- Right, then down
- Down, then right

Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right.

[Solution](https://github.com/Li-Victor/daily-coding-problem/blob/master/solutions/61-70/Problem62.js)

---
85 changes: 85 additions & 0 deletions solutions/61-70/Problem62.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^uniquePaths" }] */

// Problem 62
//
// This problem was asked by Facebook.
//
// There is an N by M matrix of zeroes.
// Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner.
// You can only move right or down.
//
// For example, given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right:
//
// Right, then down
// Down, then right
// Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right.
//
// https://leetcode.com/problems/unique-paths/description/
//
// O(NM) Time complexity
// O(S) Space complexity
// N is the number of rows, M is the number of columns, S is the smaller number of N and M.

/**
* Count the number of ways of starting at the top-left corner and getting to the bottom-right corner in a N by M matrix. N rows by M columns.
* Each solution builds on the previous
* @param {number} n
* @param {number} m
* @return {number}
*/
function uniquePaths(n, m) {
// return uniquePathsSol(n, m);
return uniquePathsLessSpace(n, m);
}

/**
* O(NM) Time and Space complexity solution
* @param {number} n
* @param {number} m
* @return {number}
*/
function uniquePathsSol(n, m) {
if (n <= 0 || m <= 0) return 0;
if (n === 1 || m === 1) return 1;
const dp = [...Array(n)].map(() => Array(m).fill(0));

// for the top row and left most column, their cell can only be reached by 1
for (let r = 0; r < dp.length; r++) {
dp[0][r] = 1;
}

for (let c = 0; c < dp[0].length; c++) {
dp[c][0] = 1;
}

for (let r = 1; r < dp.length; r++) {
for (let c = 1; c < dp[r].length; c++) {
dp[r][c] = dp[r - 1][c] + dp[r][c - 1];
}
}

return dp[n - 1][m - 1];
}

/**
* O(NM) Time complexity and O(S) Space complexity. S the smaller number of N and M
* @param {number} n
* @param {number} m
* @return {number}
*/
function uniquePathsLessSpace(n, m) {
const shorter = Math.min(n, m);
const larger = Math.max(n, m);

const grid = Array(shorter).fill(1);
for (let i = 1; i < larger; i++) {
for (let j = 1; j < grid.length; j++) {
const prevUp = grid[j];
const prevBefore = grid[j - 1];
grid[j] = prevUp + prevBefore;
}
}
return grid[shorter - 1];
}

export default uniquePaths;
11 changes: 9 additions & 2 deletions solutions/__tests__/61-70.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import pow from '../61-70/Problem61';
import uniquePaths from '../61-70/Problem62';

describe('Problems 61 - 70', () => {
test('Problem 61 ', () => {
test('Problem 61 Pow(x, y)', () => {
expect(pow(2, 10)).toBe(1024);
expect(Number(pow(2.1, 3).toFixed(4))).toBe(9.261);
expect(pow(2, -2)).toBe(0.25);
});

test('Problem 62 Unique Paths', () => {
expect(uniquePaths(2, 2)).toBe(2);
expect(uniquePaths(5, 5)).toBe(70);
expect(uniquePaths(3, 2)).toBe(3);
expect(uniquePaths(7, 3)).toBe(28);
});
});
17 changes: 17 additions & 0 deletions topics/Dynamic-Programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,21 @@ For example, pow(2, 10) should return 1024.

[Solution](https://github.com/Li-Victor/daily-coding-problem/blob/master/solutions/61-70/Problem61.js)

---

## Problem 62

This problem was asked by Facebook.

There is an N by M matrix of zeroes. Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. You can only move right or down.

For example, given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right:

- Right, then down
- Down, then right

Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right.

[Solution](https://github.com/Li-Victor/daily-coding-problem/blob/master/solutions/61-70/Problem62.js)

---

0 comments on commit 7cac00d

Please sign in to comment.