-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathrobotPaths.js
More file actions
54 lines (44 loc) · 1.25 KB
/
robotPaths.js
File metadata and controls
54 lines (44 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/////////////////////////////////////////////////////////////
// Robot Paths
// ----------------------------------
// Given a matrix, you can move right, or down.
// how many paths will it take for a robot to go from
// the top left corner to the bottom right corner?
//
// EXTRA CREDIT:
// Solve this with dynamic programming
/////////////////////////////////////////////////////////////
var countPaths = function (matrix, row, col) {
// TODO: Solve me
};
var countPathsDynamic = function (n, m) {
// TODO: Solve me
};
/////////////////////////////////////////////////////////////
// TESTS
/////////////////////////////////////////////////////////////
var testMatrix = makeMatrix(5,5);
console.log(countPathsDynamic(5,5) === 70);
console.log(countPaths(testMatrix) === 70);
/////////////////////////////////////////////////////////////
// HELPERS
/////////////////////////////////////////////////////////////
function makeMatrix (n, m) {
return range(n, range(m, 0));
}
function range (n, elm) {
var out = [];
for (var i = 0; i < n; i++) {
if (Array.isArray(elm)) {
out.push(elm.slice());
} else {
out.push(elm);
}
}
return out;
}
function pluck (arr, prop) {
return arr.map(function (item) {
return item[prop];
});
}