Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0f71258

Browse files
Merge pull request ignacio-chiazzo#4 from ignacio-chiazzo/unique-paths
Added Unique path problem
2 parents 81d5f3d + f39308b commit 0f71258

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

Main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js");
33
var permutationWithDuplicates = require("./PermutationsWithDuplicates.js");
44
var subsets = require("./Subsets.js");
5+
var uniquePaths = require("./UniquePaths.js");
56
var floodFill = require("./FloodFill.js")
67

78
// Invocation
89

910
// permutationWithoutDuplicates.main();
1011
// permutationWithDuplicates.main();
1112
// subsets.main();
12-
// floodFill.main();
13+
// uniquePaths.main();
14+
// floodFill.main();

UniquePaths.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
3+
62. Unique Paths
4+
https://leetcode.com/problems/unique-paths/description/
5+
6+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
7+
8+
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).
9+
10+
How many possible unique paths are there?
11+
12+
Example 1:
13+
14+
Input: m = 3, n = 2
15+
Output: 3
16+
Explanation:
17+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
18+
1. Right -> Right -> Down
19+
2. Right -> Down -> Right
20+
3. Down -> Right -> Right
21+
Example 2:
22+
23+
Input: m = 7, n = 3
24+
Output: 28
25+
26+
*/
27+
28+
// Solution 1
29+
// This solution is a naive solution implementing a binary tree and visiting each node.
30+
31+
var uniquePaths1 = function(m, n) {
32+
return uniquePathsAux(0, 0, m, n)
33+
};
34+
35+
var uniquePathsAux = function(row, col, rowLength, colLength) {
36+
if(row >= rowLength || col >= colLength) {
37+
return 0;
38+
}
39+
if(row == rowLength - 1 && col == colLength - 1) {
40+
return 1;
41+
}
42+
43+
return uniquePathsAux(row + 1, col, rowLength, colLength) +
44+
uniquePathsAux(row, col + 1, rowLength, colLength)
45+
};
46+
47+
// Solution 2
48+
// This solution is solution 1 but memoized.
49+
var uniquePaths2 = function(m, n) {
50+
var memo = {};
51+
return uniquePathsAux2(0, 0, m, n, memo)
52+
};
53+
54+
var uniquePathsAux2 = function(row, col, rowLength, colLength, memo) {
55+
if(memo[memoKey(row, col)]) {
56+
return memo[row + "-" + col];
57+
}
58+
if(row >= rowLength || col >= colLength) {
59+
return 0;
60+
}
61+
if(row == rowLength - 1 && col == colLength - 1) {
62+
return 1;
63+
}
64+
65+
var result = uniquePathsAux(row + 1, col, rowLength, colLength, memo) +
66+
uniquePathsAux(row, col + 1, rowLength, colLength, memo);
67+
memo[memoKey(row, col)] = result;
68+
return result;
69+
};
70+
71+
var memoKey = function(row, col) {
72+
return row + "-" + col;
73+
}
74+
75+
// Solution 3
76+
// This solution uses Dinamic Programming
77+
var uniquePaths3 = function(m, n) {
78+
var matrix = [];
79+
for(var i = 0; i < m; i++) {
80+
matrix[i] = [];
81+
for(var j = 0; j < n; j++) {
82+
if(i == 0 || j == 0) {
83+
matrix[i][j] = 1;
84+
} else{
85+
matrix[i][j] = 0;
86+
}
87+
}
88+
}
89+
90+
for(var row = 1; row < m; row++) {
91+
for(var col = 1; col < n; col++) {
92+
matrix[row][col] = matrix[row - 1][col] + matrix[row][col - 1]
93+
}
94+
}
95+
96+
return matrix[m - 1][n - 1];
97+
};
98+
99+
var main = function() {
100+
console.log(uniquePaths1(10,4));
101+
console.log(uniquePaths2(10,4));
102+
console.log(uniquePaths3(10,4));
103+
}
104+
105+
module.exports.main = main;

0 commit comments

Comments
 (0)