Skip to content

Commit d75bb52

Browse files
committed
submit
1 parent 2108d29 commit d75bb52

15 files changed

+760
-1
lines changed

‎.dir-locals.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(defun find-file-func ()
55
(when (eq 0 (buffer-size))
66
(insert-buffer-substring "javascript-template.js"))
7-
(setq-local compile-command (format "DEBUG=1 node %s" (buffer-name)))
7+
(setq-local compile-command (format "DEBUG=1 node --harmony %s" (buffer-name)))
88
(local-set-key (kbd "<f9>") 'compile)
99
)
1010
(add-hook 'find-file-hook 'find-file-func)

‎binary-tree-postorder-traversal.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var DEBUG = process.env.DEBUG;
2+
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+
* @param {TreeNode} root
12+
* @return {number[]}
13+
*/
14+
var postorderTraversal = function(root) {
15+
if (!root) return [];
16+
var stack = [];
17+
var ans = [];
18+
stack.push({
19+
node: root,
20+
next: 'L'
21+
});
22+
while (stack.length) {
23+
var c = stack.pop();
24+
if (!c.node) continue;
25+
if (c.next === 'L') {
26+
stack.push({
27+
node: c.node,
28+
next: 'R'
29+
});
30+
stack.push({
31+
node: c.node.left,
32+
next: 'L'
33+
});
34+
} else if (c.next === 'R') {
35+
stack.push({
36+
node: c.node
37+
});
38+
stack.push({
39+
node: c.node.right,
40+
next: 'L'
41+
});
42+
} else {
43+
ans.push(c.node.val);
44+
}
45+
}
46+
return ans;
47+
};
48+
49+
function test(f) {
50+
51+
[
52+
[{val: 9, left: {val: 1}, right: {val: 10}}]
53+
].forEach(function (input) {
54+
console.log(f.apply(undefined, input));
55+
});
56+
}
57+
58+
if (DEBUG) test(postorderTraversal);

‎candy-input1.js

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

‎candy-input2.js

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

‎candy.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
4+
/**
5+
* @param {number[]} ratings
6+
* @return {number}
7+
*/
8+
var candy = function(ratings) {
9+
if (ratings.length === 0) return 0;
10+
var v = [1];
11+
var count = 1;
12+
for (var i = 1; i < ratings.length; i++) {
13+
if (ratings[i-1] < ratings[i]) {
14+
count++;
15+
v.push(count);
16+
} else {
17+
count = 1;
18+
v.push(count);
19+
}
20+
}
21+
for (var i = ratings.length - 2; i >= 0; i--) {
22+
if (ratings[i+1] < ratings[i]) {
23+
v[i] = Math.max(v[i], v[i+1]+1);
24+
}
25+
}
26+
return v.reduce(function (prev, item) {
27+
return prev + item;
28+
}, 0);
29+
};
30+
31+
32+
function test(f) {
33+
34+
[
35+
[[1]],
36+
[[2]],
37+
[[2,2]],
38+
[[3,2,1]],
39+
[[3,2,1,1,2,3]],
40+
[[3,3,2,1,1,2,3,3]],
41+
[require('./candy-input1.js')],
42+
[require('./candy-input2.js')]
43+
].forEach(function (input) {
44+
console.log(f.apply(undefined, input));
45+
});
46+
}
47+
48+
if (DEBUG) test(candy);

‎different-ways-to-add-parentheses.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
var ops = {
4+
'+': function (a, b) { return a + b;},
5+
'-': function (a, b) { return a - b;},
6+
'*': function (a, b) { return a * b;}
7+
};
8+
9+
/**
10+
* @param {string} input
11+
* @return {number[]}
12+
*/
13+
var diffWaysToCompute = function(input) {
14+
var m = aExpr(input);
15+
return m.sort(function (a, b) {return a - b;});
16+
};
17+
18+
function aExpr(input) {
19+
var ans = [];
20+
for (var i = 0; i < input.length; i++) {
21+
if (input[i] in ops) {
22+
var a1 = aExpr(input.slice(0, i));
23+
var a2 = aExpr(input.slice(i+1));
24+
for (var v1 in a1)
25+
for (var v2 in a2)
26+
ans.push(ops[input[i]](a1[v1], a2[v2]));
27+
}
28+
}
29+
if (ans.length === 0)
30+
ans.push(sExpr(input).value());
31+
return ans;
32+
}
33+
34+
function sExpr(str) {
35+
var i = 0;
36+
while (i < str.length && ! (str[i] in ops)) i++;
37+
if (i === str.length) {
38+
return new Expr(str);
39+
} else {
40+
return new Expr(str[i], sExpr(str.slice(0, i)), sExpr(str.slice(i+1)));
41+
}
42+
}
43+
44+
function Expr(op, expr1, expr2) {
45+
this.op = op;
46+
this.expr1 = expr1;
47+
this.expr2 = expr2;
48+
}
49+
50+
Expr.prototype.value = function () {
51+
if (isNaN(Number(this.op))) {
52+
var v1 = this.expr1.value();
53+
var v2 = this.expr2.value();
54+
return ops[this.op](v1, v2);
55+
} else {
56+
return Number(this.op);
57+
}
58+
};
59+
60+
61+
function test(f) {
62+
63+
[
64+
["2*3-4*5"],
65+
["2-1-1"]
66+
].forEach(function (input) {
67+
console.log(f.apply(undefined, input));
68+
});
69+
}
70+
71+
if (DEBUG) test(diffWaysToCompute);

‎dungeon-game.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[][]} dungeon
5+
* @return {number}
6+
*/
7+
var calculateMinimumHP = function(dungeon) {
8+
var n = dungeon.length;
9+
var m = dungeon[0].length;
10+
var ans = [];
11+
for (var i = 0; i < m; i++) ans.push(0);
12+
ans.push(Math.pow(2, 31));
13+
ans[m-1] = dungeon[n-1][m-1] > 0 ? 1 : -dungeon[n-1][m-1] + 1;
14+
for (var i = m-2; i >= 0; i--) {
15+
var t = ans[i+1];
16+
t = t - dungeon[n-1][i];
17+
if (t <= 0) t = 1;
18+
ans[i] = t;
19+
}
20+
for (var j = n-2; j >= 0; j--) {
21+
for (var i = m - 1; i >= 0; i--) {
22+
t = Math.min(ans[i+1], ans[i]);
23+
t = t - dungeon[j][i];
24+
if (t <= 0) t = 1;
25+
ans[i] = t;
26+
}
27+
}
28+
return ans[0];
29+
};
30+
31+
32+
function test(f) {
33+
34+
[
35+
[[[-2, -3, 3],
36+
[-5, -10, 1],
37+
[10, 30, -5]]
38+
]
39+
].forEach(function (input) {
40+
console.log(f.apply(undefined, input));
41+
});
42+
}
43+
44+
if (DEBUG) test(calculateMinimumHP);

‎maximum-gap.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
9+
Array.prototype.max = function () {
10+
return this.reduce((p, c) => Math.max(p, c), -1);
11+
};
12+
13+
Array.prototype.min = function () {
14+
return this.reduce((prev, curr) => Math.min(prev, curr), Math.pow(2, 32));
15+
};
16+
var maximumGap = function(nums) {
17+
if (nums.length < 2) return 0;
18+
var nb = nums.length*2;
19+
var min = nums.min();
20+
var max = nums.max();
21+
var len = Math.ceil( (max-min) / nb);
22+
23+
var b = {};
24+
nums.forEach(num => {
25+
var idx = Math.floor((num - min) / len);
26+
b[idx] = b[idx] || {nums: []};
27+
b[idx].nums.push(num);
28+
});
29+
for (var idx in b) {
30+
b[idx].max = b[idx].nums.max();
31+
b[idx].min = b[idx].nums.min();
32+
}
33+
var ans = 0;
34+
for (var i = 0; i <= nb;) {
35+
var j = i + 1;
36+
while (j <= nb && !b[j]) j++;
37+
if (j > nb) break;
38+
ans = Math.max(ans, b[j].min - b[i].max);
39+
i = j;
40+
}
41+
return ans;
42+
};
43+
44+
function test(f) {
45+
46+
[
47+
[[1, 2]],
48+
[[2, 2, 2, 2, 2, 2]],
49+
[[1,2,3,4,5]],
50+
[[77, 55, 22, 33, 1]]
51+
].forEach(function (input) {
52+
console.log(f.apply(undefined, input));
53+
});
54+
}
55+
56+
if (DEBUG) test(maximumGap);

‎n-queens.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
4+
/**
5+
* @param {number} n
6+
* @return {string[][]}
7+
*/
8+
var loc = [];
9+
var ans = [];
10+
var usedCols = {};
11+
var N;
12+
function cons(t) {
13+
var s = '';
14+
for (var i = 0; i < t; i++)
15+
s += '.';
16+
s += 'Q';
17+
for (var i = t + 1; i < N; i++)
18+
s += '.';
19+
return s;
20+
}
21+
22+
function dfs(d) {
23+
if (d === N) {
24+
var tmp = [], t1 = [];
25+
for (var j = 0; j < N; j++) {
26+
tmp.push(cons(loc[j]));
27+
//t1.push(cons(N-loc[j]));
28+
}
29+
ans.push(tmp);
30+
return;
31+
}
32+
for (var i = 0; i < N; i++) {
33+
if (!usedCols[i]) {
34+
var bad = false;
35+
for (var j = 0; j < d; j++)
36+
if (d - j === Math.abs(i - loc[j]))
37+
bad = true;
38+
if (!bad) {
39+
usedCols[i] = true;
40+
loc.push(i);
41+
dfs(d+1);
42+
loc.pop();
43+
usedCols[i] = false;
44+
}
45+
}
46+
}
47+
}
48+
var solveNQueens = function(n) {
49+
N = n;
50+
ans = [];
51+
usedCols = {};
52+
//for (var i = 0; i < Math.floor((n + 1) / 2); i++) {
53+
loc = [];
54+
dfs(0);
55+
//}
56+
return ans;
57+
};
58+
59+
60+
function test(f) {
61+
62+
[
63+
[4],
64+
[5],
65+
[6],
66+
[7],
67+
[8],
68+
[9]
69+
].forEach(function (input) {
70+
console.log(f.apply(undefined, input));
71+
});
72+
}
73+
74+
if (DEBUG) test(solveNQueens);

0 commit comments

Comments
 (0)