Skip to content

Commit 39f792d

Browse files
committed
index the topics
1 parent b3e907e commit 39f792d

39 files changed

+324
-269
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function subsetsWithDup(nums: number[]): number[][] {
77

88
const backtracking = (
99
start = 0,
10-
prevRes = [],
10+
prevRes: number[] = [],
1111
visited = Array.from({ length: N }, () => false)
1212
): void => {
1313
if (start === N) {

src/backtracking/sudoku-solver.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
Do not return anything, modify board in-place instead.
3+
*/
4+
function solveSudoku(board: string[][]): void {
5+
const isLegal = (row: number, col: number): boolean => {
6+
const test = (strs: string[], target: string): boolean =>
7+
strs.filter((val) => val === target).length === 1;
8+
const target = board[row][col];
9+
10+
const rowItems = board[row];
11+
if (!test(rowItems, target)) return false;
12+
13+
const colItems = board.map((_row) => _row[col]);
14+
if (!test(colItems, target)) return false;
15+
16+
const boxRowStart = Math.floor(row / 3) * 3;
17+
const boxColStart = Math.floor(col / 3) * 3;
18+
const boxItems = board
19+
.slice(boxRowStart, boxRowStart + 3)
20+
.map((_row) => _row.slice(boxColStart, boxColStart + 3))
21+
.reduce((prev, next) => [...prev, ...next], []);
22+
23+
if (!test(boxItems, target)) return false;
24+
25+
return true;
26+
};
27+
28+
const getNextPos = (row: number, col: number): [number, number] =>
29+
col + 1 === 9 ? [row + 1, 0] : [row, col + 1];
30+
31+
const candidates = "123456789";
32+
let done = false;
33+
34+
const backtracking = (row: number, col: number): void => {
35+
if (row === 9) done = true; // test done
36+
if (done) return;
37+
if (board[row][col] !== ".") return backtracking(...getNextPos(row, col)); // go to fill the next empty pos
38+
39+
for (const cand of candidates) {
40+
// trial and error
41+
board[row][col] = cand;
42+
if (isLegal(row, col)) {
43+
backtracking(...getNextPos(row, col));
44+
}
45+
if (done) return;
46+
board[row][col] = ".";
47+
}
48+
};
49+
50+
backtracking(0, 0);
51+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function subsetXORSum(nums: number[]): number {
2+
const N = nums.length;
3+
const sets: number[][] = [];
4+
5+
const backtracking = (start = 0, prevRes: number[] = []): void => {
6+
if (start === N) {
7+
prevRes.length !== 0 && sets.push([...prevRes]);
8+
return;
9+
}
10+
// includes nums[start] set
11+
prevRes.push(nums[start]);
12+
backtracking(start + 1, prevRes);
13+
prevRes.pop();
14+
15+
// excludes nums[start] set
16+
backtracking(start + 1, prevRes);
17+
};
18+
backtracking();
19+
20+
return sets
21+
.map((set) => set.reduce((prev, next) => prev ^ next, 0))
22+
.reduce((prev, next) => prev + next, 0);
23+
}

src/backtracking/word-search-2.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export {};
2+
class TrieNode {
3+
children: Map<string, TrieNode> = new Map();
4+
word: string = "";
5+
}
6+
7+
class Trie {
8+
root: TrieNode = new TrieNode();
9+
insert = (word: string): void => {
10+
let node = this.root;
11+
for (const ch of word) {
12+
if (!node.children.has(ch)) node.children.set(ch, new TrieNode());
13+
node = node.children.get(ch)!;
14+
}
15+
node.word = word; // store in the leaf node
16+
};
17+
}
18+
19+
function findWords(board: string[][], words: string[]): string[] {
20+
/**
21+
22+
Backtracking with Trie structure.
23+
24+
Make a trie, and insert the word, we can put the word to the leaf node (so if we can travel here, we found it), and do backtracking, while we found it, we can de-dup the leaf node.
25+
26+
**/
27+
const ret: string[] = [];
28+
const M = board.length;
29+
const N = board[0].length;
30+
const trie = new Trie();
31+
for (const word of words) {
32+
trie.insert(word);
33+
}
34+
const backtracking = (i: number, j: number, node: TrieNode): void => {
35+
if (node.word !== "") {
36+
ret.push(node.word);
37+
node.word = ""; // de-dup
38+
}
39+
if (i < 0 || j < 0 || i >= M || j >= N) return; // boundary
40+
41+
const cur = board[i][j];
42+
if (!node.children.has(cur)) return;
43+
44+
node = node.children.get(cur)!;
45+
board[i][j] = "";
46+
backtracking(i + 1, j, node);
47+
backtracking(i, j + 1, node);
48+
backtracking(i - 1, j, node);
49+
backtracking(i, j - 1, node);
50+
board[i][j] = cur;
51+
};
52+
53+
for (let i = 0; i < M; i++) {
54+
for (let j = 0; j < N; j++) {
55+
backtracking(i, j, trie.root);
56+
}
57+
}
58+
59+
return ret;
60+
}

src/search-insert-position.ts renamed to src/binary-search/search-insert-position.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export {};
12
const findIndex = (
23
A: number[],
34
fn: (val: number, idx: number) => number
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export {};
2+
const findIndex = (
3+
arr: number[],
4+
fn: (val: number, index: number) => number
5+
): number => {
6+
let lo = 0,
7+
hi = arr.length;
8+
while (lo <= hi) {
9+
const mid = Math.floor((lo + hi) / 2);
10+
11+
if (fn(arr[mid], mid) === 0) return mid;
12+
if (fn(arr[mid], mid) < 0) lo = mid + 1;
13+
else hi = mid - 1;
14+
}
15+
return lo - 1;
16+
};
17+
18+
function search(nums: number[], target: number): boolean {
19+
if (nums[nums.length - 1] === target) return true;
20+
while (nums[0] === nums[nums.length - 1]) {
21+
nums.pop();
22+
}
23+
// [4,4,4,5]
24+
const maxIdx = findIndex(nums, (val: number, index: number): number => {
25+
if (val < nums[0]) {
26+
return 1;
27+
} else {
28+
return -1;
29+
}
30+
});
31+
32+
nums = [...nums.slice(maxIdx + 1), ...nums.slice(0, maxIdx + 1)];
33+
34+
const targetIdx = findIndex(nums, (val) => val - target);
35+
return nums[targetIdx] === target;
36+
}

src/serialize-and-deserialize-binary-tree-queue-recursive.ts renamed to src/binary-tree/serialize-and-deserialize-binary-tree-queue-recursive.ts

File renamed without changes.

0 commit comments

Comments
 (0)