Skip to content

Added Unique path problem #4

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

Merged
merged 2 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js");
var permutationWithDuplicates = require("./PermutationsWithDuplicates.js");
var subsets = require("./Subsets.js");
var uniquePaths = require("./UniquePaths.js");
var floodFill = require("./FloodFill.js")

// Invocation

// permutationWithoutDuplicates.main();
// permutationWithDuplicates.main();
// subsets.main();
// floodFill.main();
// uniquePaths.main();
// floodFill.main();
105 changes: 105 additions & 0 deletions UniquePaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*

62. Unique Paths
https://leetcode.com/problems/unique-paths/description/

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

Example 1:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right
Example 2:

Input: m = 7, n = 3
Output: 28

*/

// Solution 1
// This solution is a naive solution implementing a binary tree and visiting each node.

var uniquePaths1 = function(m, n) {
return uniquePathsAux(0, 0, m, n)
};

var uniquePathsAux = function(row, col, rowLength, colLength) {
if(row >= rowLength || col >= colLength) {
return 0;
}
if(row == rowLength - 1 && col == colLength - 1) {
return 1;
}

return uniquePathsAux(row + 1, col, rowLength, colLength) +
uniquePathsAux(row, col + 1, rowLength, colLength)
};

// Solution 2
// This solution is solution 1 but memoized.
var uniquePaths2 = function(m, n) {
var memo = {};
return uniquePathsAux2(0, 0, m, n, memo)
};

var uniquePathsAux2 = function(row, col, rowLength, colLength, memo) {
if(memo[memoKey(row, col)]) {
return memo[row + "-" + col];
}
if(row >= rowLength || col >= colLength) {
return 0;
}
if(row == rowLength - 1 && col == colLength - 1) {
return 1;
}

var result = uniquePathsAux(row + 1, col, rowLength, colLength, memo) +
uniquePathsAux(row, col + 1, rowLength, colLength, memo);
memo[memoKey(row, col)] = result;
return result;
};

var memoKey = function(row, col) {
return row + "-" + col;
}

// Solution 3
// This solution uses Dinamic Programming
var uniquePaths3 = function(m, n) {
var matrix = [];
for(var i = 0; i < m; i++) {
matrix[i] = [];
for(var j = 0; j < n; j++) {
if(i == 0 || j == 0) {
matrix[i][j] = 1;
} else{
matrix[i][j] = 0;
}
}
}

for(var row = 1; row < m; row++) {
for(var col = 1; col < n; col++) {
matrix[row][col] = matrix[row - 1][col] + matrix[row][col - 1]
}
}

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

var main = function() {
console.log(uniquePaths1(10,4));
console.log(uniquePaths2(10,4));
console.log(uniquePaths3(10,4));
}

module.exports.main = main;