Skip to content

Commit 693c871

Browse files
committed
4 soulutions, 3 are dp.
1 parent 5fbc3d0 commit 693c871

5 files changed

+186
-0
lines changed

‎first-missing-positive.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
/**
4+
* @param {number[]} nums
5+
* @return {number}
6+
*/
7+
var firstMissingPositive = function(nums) {
8+
// make the array 1-indexed. If you dont like this trick,
9+
// the following loop needs to be more carefully,
10+
nums.unshift(-1);
11+
for (var i = 1; i < nums.length; i++) {
12+
if (nums[i] !== i && nums[i] > 0 && nums[i] < nums.length) {
13+
var to = nums[i];
14+
while (to > 0 && to < nums.length && nums[to] !== to && to !== i) {
15+
var tmp = nums[to];
16+
nums[to] = to;
17+
to = tmp;
18+
}
19+
if (to === i) nums[i] = to;
20+
}
21+
}
22+
console.log(nums);
23+
for (var i = 1; i < nums.length; i++)
24+
if (nums[i] !== i) return i;
25+
return nums.length;
26+
};
27+
28+
function test(f) {
29+
30+
[
31+
[[1, 2, 0]],
32+
[[3,4,-1,1]],
33+
[[1]],
34+
[[1,2,3,5]],
35+
].forEach(function (input) {
36+
console.log(f.apply(undefined, input));
37+
});
38+
}
39+
40+
if (DEBUG) test(firstMissingPositive);

‎jump-game-ii.in1.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎jump-game-ii.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
/**
4+
* @param {number[]} nums
5+
* @return {number}
6+
*/
7+
var jump = function(nums) {
8+
var n = nums.length;
9+
var f = [];
10+
f[n-1] = 0;
11+
for (var i = n - 2; i >= 0; i--) {
12+
if (i + nums[i] >= n - 1) f[i] = 1;
13+
else {
14+
var ans = Number.MAX_SAFE_INTEGER;
15+
for (var j = nums[i]; j >= 1; j--) {
16+
ans = Math.min(ans, f[i+j] + 1);
17+
if (ans === 2 || ans === f[i+1] && nums[i] < nums[i+1])
18+
break;
19+
}
20+
f[i] = ans;
21+
}
22+
}
23+
return f[0];
24+
};
25+
26+
function test(f) {
27+
28+
[
29+
[[5]],
30+
[[2,3,1,1,4]],
31+
require('./jump-game-ii.in1.js'),
32+
].forEach(function (input) {
33+
console.log(f.apply(undefined, input));
34+
});
35+
}
36+
37+
if (DEBUG) test(jump);

‎largest-rectangle-in-histogram.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
/**
4+
* @param {number[]} height
5+
* @return {number}
6+
*/
7+
var largestRectangleArea = function(height) {
8+
if (height.length === 0) return 0;
9+
function recttol(height) {
10+
var ans = [];
11+
var stack = [];
12+
height.forEach((h, idx) => {
13+
while (stack.length && stack[stack.length - 1].h > h) {
14+
var curr = stack.pop();
15+
ans[curr.idx] = curr.h * (idx - curr.idx);
16+
}
17+
stack.push({h: h, idx: idx});
18+
});
19+
while (stack.length) {
20+
var curr = stack.pop();
21+
ans[curr.idx] = curr.h * (height.length - curr.idx);
22+
}
23+
return ans;
24+
}
25+
var l = recttol(height);
26+
var r = recttol(height.reverse());
27+
l.reverse();
28+
var t = l.map((x, idx) => x + r[idx] - height[idx]);
29+
return Math.max.apply(undefined, t);
30+
};
31+
32+
function test(f) {
33+
34+
[
35+
[[5]],
36+
[[5,6, 5]],
37+
[[5,4,5]],
38+
[[2,1,5,6,2,3]],
39+
].forEach(function (input) {
40+
console.log(f.apply(undefined, input));
41+
});
42+
}
43+
44+
if (DEBUG) test(largestRectangleArea);

‎maximal-rectangle.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
var arrayMax = arr => Math.max.apply(undefined, arr);
4+
var deepCopy = matrix => matrix.map(row => row.slice(0));
5+
/**
6+
* @param {character[][]} matrix
7+
* @return {number}
8+
*/
9+
var maximalRectangle = function(matrix) {
10+
var n = matrix.length;
11+
if (n === 0) return 0;
12+
var m = matrix[0].length;
13+
var r = [];
14+
matrix.forEach(row => {
15+
var result = [];
16+
for (var i = row.length - 1; i >= 0; i--) {
17+
if (row[i] === '1') {
18+
result.unshift((result[0] || 0) + 1);
19+
} else {
20+
result.unshift(0);
21+
}
22+
}
23+
r.push(result);
24+
});
25+
// r is also f(i, j, 0);
26+
var f = deepCopy(r);
27+
var ans = arrayMax(r.map(arrayMax));
28+
// updating f and ans.
29+
for (var k = 1; k < matrix.length; k++) {
30+
// for new k
31+
for (var i = 0; i < n; i++) {
32+
for (var j = 0; j < m; j++) {
33+
if (i+k >= matrix.length) {
34+
f[i][j] = 0;
35+
} else {
36+
f[i][j] = Math.min(r[i][j], f[i+1][j]);
37+
ans = Math.max(ans, f[i][j]*(k+1));
38+
}
39+
}
40+
}
41+
}
42+
return ans;
43+
};
44+
45+
function test(f) {
46+
47+
[
48+
[['1011',
49+
'1101',
50+
'1111',
51+
'0110',
52+
]],
53+
[['1111',
54+
'1101',
55+
'1111',
56+
'1110',
57+
]],
58+
[['1']],
59+
].forEach(function (input) {
60+
console.log(f.apply(undefined, input));
61+
});
62+
}
63+
64+
if (DEBUG) test(maximalRectangle);

0 commit comments

Comments
 (0)