Skip to content

Commit e0908d0

Browse files
committed
four solutions.
1 parent dfe6473 commit e0908d0

4 files changed

+201
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
var maxProfit = function(prices) {
4+
var inf = Math.pow(2, 31);
5+
var p = prices; p.push(-inf);
6+
var np = p.length;
7+
var f = [], b = [];
8+
for (var i = 0; i <= np; i++) {
9+
f.push(0);
10+
b.push(0);
11+
}
12+
b[np] = -inf; b[np-1] = inf;
13+
for (var t = 1; t <= 2; t++) {
14+
// b for new k
15+
for (var i = np - 2; i >= 0; i--) {
16+
b[i] = Math.max(b[i+1], f[i+2]) + p[i+1] - p[i];
17+
}
18+
// f for new k
19+
for (var i = np - 2; i >= 0; i--) {
20+
f[i] = Math.max(b[i], f[i+1]);
21+
}
22+
}
23+
return f[0];
24+
};

‎binary-tree-maximum-path-sum.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var DEBUG = process.env.DEBUG;
2+
var inf = Math.pow(2, 32);
3+
/**
4+
* Definition for a binary tree node.
5+
* function TreeNode(val) {
6+
* this.val = val;
7+
* this.left = this.right = null;
8+
* }
9+
*/
10+
11+
function c(node) {
12+
if (!node) return -inf;
13+
if (node.c) return node.c;
14+
return node.c = Math.max(c(node.left), c(node.right), 0) + node.val;
15+
}
16+
17+
18+
19+
/**
20+
* @param {TreeNode} root
21+
* @return {number}
22+
*/
23+
var maxPathSum = function(root) {
24+
if (!root) return -inf;
25+
var ans = Math.max(c(root), c(root.left) + c(root.right) + root.val);
26+
return ans = Math.max(ans, maxPathSum(root.left), maxPathSum(root.right));
27+
};
28+
29+
30+
function test(f) {
31+
32+
[
33+
[{val: 1, left: {val:2}, right: {val:3}}],
34+
].forEach(function (input) {
35+
console.log(f.apply(undefined, input));
36+
});
37+
}
38+
39+
if (DEBUG) test(maxPathSum);

‎max-points-on-a-line.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
/*
4+
* Definition for a point.
5+
* function Point(x, y) {
6+
* this.x = x;
7+
* this.x = y;
8+
* }
9+
*/
10+
11+
/**
12+
* @param {Point[]} points
13+
* @return {number}
14+
*/
15+
var maxPoints = function(points) {
16+
points = points.sort((p1, p2) => {
17+
if (p1.x === p2.x) return p1.y - p2.y;
18+
else return p1.x - p2.x;
19+
});
20+
var newpoints = [];
21+
for (var i = 0; i < points.length; i++) {
22+
if ( newpoints.length === 0 ||
23+
points[i].x !== newpoints[newpoints.length - 1].x ||
24+
points[i].y !== newpoints[newpoints.length - 1].y ) {
25+
newpoints.push(points[i]);
26+
newpoints[newpoints.length - 1].ndup = 1;
27+
} else {
28+
newpoints[newpoints.length - 1].ndup += 1;
29+
}
30+
}
31+
points = newpoints;
32+
if (points.length === 1) return points[0].ndup;
33+
var inf = Math.pow(2, 48);
34+
var max = 0;
35+
for (i = 0; i < points.length; i++) {
36+
var h = {};
37+
for (var j = 0; j < points.length; j++) {
38+
if (i === j) continue;
39+
if (points[j].y >= points[i].y) {
40+
var k, b;
41+
if (points[j].x === points[i].x) {
42+
k = inf;
43+
b = points[i].x;
44+
} else {
45+
k = (points[j].y - points[i].y) / (points[j].x - points[i].x);
46+
b = (points[i].x*points[j].y - points[j].x*points[i].y) / (points[i].x - points[j].x);
47+
}
48+
h[[k,b]] = h[[k,b]] || points[i].ndup;
49+
h[[k,b]] += points[j].ndup;
50+
}
51+
}
52+
var curr = 0;
53+
for (var l in h) curr = Math.max(curr, h[l]);
54+
max = Math.max(max, curr);
55+
}
56+
return max;
57+
};
58+
59+
function buildp(p) {
60+
return {
61+
x:p[0], y:p[1]
62+
};
63+
}
64+
65+
66+
function test(f) {
67+
68+
[
69+
[[{x: 1, y:2}]],
70+
[[{x: 1, y:2}, {x:1, y:2}]],
71+
[[{x: 1, y:2}, {x:1, y:3}]],
72+
[[{x: 1, y:2}, {x:1, y:3}, {x: 1, y: 4}, {x:2, y: 2}]],
73+
[[{x:4,y:0},{x:4,y:-1},{x:4,y:5}]],
74+
[[[-4,1],[-7,7],[-1,5],[9,-25]].map(buildp)],
75+
].forEach(function (input) {
76+
console.log(f.apply(undefined, input));
77+
});
78+
}
79+
80+
if (DEBUG) test(maxPoints);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var DEBUG = process.env.DEBUG;
2+
var M = 1572869;
3+
var M1 = 100663319;
4+
5+
String.prototype.hash = function () {
6+
var result = 0;
7+
for (var i = 0; i < this.length; i++) {
8+
result *= 26;
9+
result += this.charCodeAt(i);
10+
result %= M;
11+
}
12+
return result;
13+
};
14+
15+
/**
16+
* @param {string} s
17+
* @param {string[]} words
18+
* @return {number[]}
19+
*/
20+
var findSubstring = function(s, words) {
21+
if (words.length === 0) return [];
22+
var sl = s.length;
23+
var wl = words[0].length;
24+
var nw = words.length;
25+
26+
words.forEach(word => word.h = word.hash());
27+
var T = 1;
28+
for (var i = 0; i < s.length - 1; i++) T *= 26;
29+
var h = s.substring(0, wl).hash();
30+
var sh = [];
31+
for (var i = wl; i < sl; i++) {
32+
sh.push(h);
33+
h = Math.floor((h / 26)) + s.charCodeAt(i) * T;
34+
}
35+
36+
var wordsHashSum = 0;
37+
words.forEach(word => wordsHashSum = (wordsHashSum + word.h) % M1);
38+
var hashSum = [];
39+
for (var t = 0; t < wl; t++) {
40+
var currSum = 0;
41+
if (t + (nw-1) * wl < sh.length) break;
42+
for (var i = 0; i < nw; i++)
43+
currSum = (currSum + sh[t + i*wl]) % M1;
44+
i = t + nw * wl;
45+
while (9 )
46+
}
47+
};
48+
49+
function test(f) {
50+
51+
[
52+
[[]]
53+
].forEach(function (input) {
54+
console.log(f.apply(undefined, input));
55+
});
56+
}
57+
58+
if (DEBUG) test(findSubstring);

0 commit comments

Comments
 (0)