Skip to content

Commit a731fc6

Browse files
committed
update
1 parent 287cfa5 commit a731fc6

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

src/4-sum-ii.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function fourSumCount(
2+
A: number[],
3+
B: number[],
4+
C: number[],
5+
D: number[]
6+
): number {
7+
// group {C,D}, collect their sum, n*n
8+
9+
const map: Map<number, number> = new Map();
10+
for (const c of C) {
11+
for (const d of D) {
12+
const sum = c + d;
13+
map.set(sum, (map.get(sum) || 0) + 1);
14+
}
15+
}
16+
17+
// group {A,B} to check if there's a pair sum, which -sum could match in map
18+
let ret = 0;
19+
for (const a of A) {
20+
for (const b of B) {
21+
const sum = a + b;
22+
if (map.has(-1 * sum)) ret += map.get(-1 * sum) || 0;
23+
}
24+
}
25+
return ret;
26+
}

src/Node.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Node {
2+
val: number;
3+
prev: Node | null;
4+
next: Node | null;
5+
child: Node | null;
6+
constructor(val?: number, prev?: Node, next?: Node, child?: Node) {
7+
this.val = val === undefined ? 0 : val;
8+
this.prev = prev === undefined ? null : prev;
9+
this.next = next === undefined ? null : next;
10+
this.child = child === undefined ? null : child;
11+
}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function flatten(head: Node | null): Node | null {
2+
const stack: Node[] = []; // store the next
3+
let ptr = head;
4+
while (ptr !== null) {
5+
if (ptr.child !== null) {
6+
const next = ptr.next;
7+
const child = ptr.child;
8+
next !== null && stack.push(next);
9+
ptr.next = child;
10+
ptr.child = null;
11+
}
12+
if (ptr.next === null && stack.length !== 0) ptr.next = stack.pop() as Node;
13+
if (ptr.next !== null) ptr.next.prev = ptr;
14+
15+
ptr = ptr.next;
16+
}
17+
18+
return head;
19+
}

src/game-of-life.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
function gameOfLife(board: number[][]): void {
2+
/**
3+
1. State change rule
4+
dead cell
5+
- 3 live neighbors become live
6+
7+
live cell:
8+
- < 2 neighbor dies
9+
- 2-3 neighbor live
10+
- >3 neighbor dies
11+
12+
2. To implement the in-place approach, we rely other two state
13+
s1 s2
14+
die -> die : 00 -> 00
15+
die -> live : 00 -> 10
16+
live -> live : 01 -> 11
17+
live -> die : 01 -> 01
18+
19+
using %2 to get s1
20+
using >>1 to get s2
21+
**/
22+
23+
const M = board.length;
24+
const N = board[0].length;
25+
26+
const getNeighbors = (i: number, j: number): number => {
27+
let ret = 0;
28+
for (let ptr1 = i - 1; ptr1 <= i + 1; ptr1++) {
29+
// check boundary
30+
if (ptr1 < 0 || ptr1 >= M) continue;
31+
for (let ptr2 = j - 1; ptr2 <= j + 1; ptr2++) {
32+
// check boundary
33+
if (ptr2 < 0 || ptr2 >= N) continue;
34+
35+
// check non-self
36+
if (ptr1 === i && ptr2 === j) continue;
37+
38+
if (board[ptr1][ptr2] % 2 === 1) ret++; // using %2 to check the s1
39+
}
40+
}
41+
return ret;
42+
};
43+
44+
const transform = (prevState: number, neighbors: number): number => {
45+
if (prevState === 0) {
46+
if (neighbors === 3) return 2;
47+
// 00->10
48+
else return 0; // 00->00
49+
} else {
50+
if (neighbors < 2 || neighbors > 3) return 1;
51+
// 01 -> 01
52+
else return 3; // 01->11
53+
}
54+
};
55+
56+
// transform
57+
for (let i = 0; i < M; i++) {
58+
for (let j = 0; j < N; j++) {
59+
board[i][j] = transform(board[i][j], getNeighbors(i, j));
60+
}
61+
}
62+
63+
// recover for next iteration
64+
for (let i = 0; i < M; i++) {
65+
for (let j = 0; j < N; j++) {
66+
board[i][j] >>= 1;
67+
}
68+
}
69+
}

src/reshape-the-matrix.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function matrixReshape(mat: number[][], r: number, c: number): number[][] {
2+
const M = mat.length,
3+
N = mat[0].length;
4+
if (M * N !== r * c) return mat; // illegal case
5+
6+
const flat = mat.reduce((prev, next) => [...prev, ...next], []);
7+
return Array.from({ length: r }, () =>
8+
Array.from({ length: c }, () => flat.shift() as number)
9+
);
10+
}

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
66
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
77
"lib": [
8-
"es2015",
9-
"dom"
8+
"es2015"
109
] /* Specify library files to be included in the compilation. */,
1110
// "allowJs": true, /* Allow javascript files to be compiled. */
1211
// "checkJs": true, /* Report errors in .js files. */

0 commit comments

Comments
 (0)