Skip to content

Commit 1717ca1

Browse files
authored
Added tasks 210-222
1 parent d10a54b commit 1717ca1

File tree

16 files changed

+554
-0
lines changed

16 files changed

+554
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
152152

153153
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
154154
|-|-|-|-|-|-
155+
| 0222 |[Count Complete Tree Nodes](src/main/ts/g0201_0300/s0222_count_complete_tree_nodes/solution.ts)| Easy | Depth_First_Search, Tree, Binary_Search, Binary_Tree | 0 | 100.00
155156

156157
#### Day 11
157158

@@ -801,6 +802,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
801802

802803
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
803804
|-|-|-|-|-|-
805+
| 0210 |[Course Schedule II](src/main/ts/g0201_0300/s0210_course_schedule_ii/solution.ts)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 2 | 99.76
804806

805807
#### Day 12 Dynamic Programming
806808

@@ -1088,6 +1090,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10881090
| 0049 |[Group Anagrams](src/main/ts/g0001_0100/s0049_group_anagrams/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 27 | 78.99
10891091
| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 1 | 89.70
10901092
| 0202 |[Happy Number](src/main/ts/g0201_0300/s0202_happy_number/solution.ts)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 0 | 100.00
1093+
| 0219 |[Contains Duplicate II](src/main/ts/g0201_0300/s0219_contains_duplicate_ii/solution.ts)| Easy | Array, Hash_Table, Sliding_Window | 17 | 79.29
10911094
| 0128 |[Longest Consecutive Sequence](src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 34 | 90.07
10921095

10931096
#### Top Interview 150 Intervals
@@ -1137,6 +1140,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11371140
| 0129 |[Sum Root to Leaf Numbers](src/main/ts/g0101_0200/s0129_sum_root_to_leaf_numbers/solution.ts)| Medium | Depth_First_Search, Tree, Binary_Tree | 0 | 100.00
11381141
| 0124 |[Binary Tree Maximum Path Sum](src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 2 | 71.11
11391142
| 0173 |[Binary Search Tree Iterator](src/main/ts/g0101_0200/s0173_binary_search_tree_iterator/solution.ts)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 6 | 98.16
1143+
| 0222 |[Count Complete Tree Nodes](src/main/ts/g0201_0300/s0222_count_complete_tree_nodes/solution.ts)| Easy | Depth_First_Search, Tree, Binary_Search, Binary_Tree | 0 | 100.00
11401144
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/ts/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 61 | 75.97
11411145

11421146
#### Top Interview 150 Binary Tree BFS
@@ -1160,6 +1164,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11601164
|-|-|-|-|-|-
11611165
| 0200 |[Number of Islands](src/main/ts/g0101_0200/s0200_number_of_islands/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 57 | 93.94
11621166
| 0207 |[Course Schedule](src/main/ts/g0201_0300/s0207_course_schedule/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 11 | 81.08
1167+
| 0210 |[Course Schedule II](src/main/ts/g0201_0300/s0210_course_schedule_ii/solution.ts)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 2 | 99.76
11631168

11641169
#### Top Interview 150 Graph BFS
11651170

@@ -1172,6 +1177,8 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11721177
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11731178
|-|-|-|-|-|-
11741179
| 0208 |[Implement Trie (Prefix Tree)](src/main/ts/g0201_0300/s0208_implement_trie_prefix_tree/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 48 | 63.95
1180+
| 0211 |[Design Add and Search Words Data Structure](src/main/ts/g0201_0300/s0211_design_add_and_search_words_data_structure/solution.ts)| Medium | String, Depth_First_Search, Design, Trie | 450 | 92.88
1181+
| 0212 |[Word Search II](src/main/ts/g0201_0300/s0212_word_search_ii/solution.ts)| Hard | Top_Interview_Questions, Array, String, Matrix, Backtracking, Trie | ew 150 | ew 150 Trie
11751182

11761183
#### Top Interview 150 Backtracking
11771184

@@ -1741,8 +1748,13 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
17411748
| 0234 |[Palindrome Linked List](src/main/ts/g0201_0300/s0234_palindrome_linked_list/solution.ts)| Easy | Top_100_Liked_Questions, Two_Pointers, Stack, Linked_List, Recursion, Level_2_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 3 | 92.85
17421749
| 0230 |[Kth Smallest Element in a BST](src/main/ts/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_17_Tree, Level_2_Day_9_Binary_Search_Tree, Top_Interview_150_Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
17431750
| 0226 |[Invert Binary Tree](src/main/ts/g0201_0300/s0226_invert_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1751+
| 0222 |[Count Complete Tree Nodes](src/main/ts/g0201_0300/s0222_count_complete_tree_nodes/solution.ts)| Easy | Depth_First_Search, Tree, Binary_Search, Binary_Tree, Binary_Search_II_Day_10, Top_Interview_150_Binary_Tree_General | 0 | 100.00
17441752
| 0221 |[Maximal Square](src/main/ts/g0201_0300/s0221_maximal_square/solution.ts)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Top_Interview_150_Multidimensional_DP, Big_O_Time_O(m\*n)_Space_O(m\*n) | 18 | 59.02
1753+
| 0219 |[Contains Duplicate II](src/main/ts/g0201_0300/s0219_contains_duplicate_ii/solution.ts)| Easy | Array, Hash_Table, Sliding_Window, Top_Interview_150_Hashmap | 17 | 79.29
17451754
| 0215 |[Kth Largest Element in an Array](src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Data_Structure_II_Day_20_Heap_Priority_Queue, Top_Interview_150_Heap, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 4 | 99.64
1755+
| 0212 |[Word Search II](src/main/ts/g0201_0300/s0212_word_search_ii/solution.ts)| Hard | Top_Interview_Questions, Array, String, Matrix, Backtracking, Trie | ew 150 | ew 150 Trie
1756+
| 0211 |[Design Add and Search Words Data Structure](src/main/ts/g0201_0300/s0211_design_add_and_search_words_data_structure/solution.ts)| Medium | String, Depth_First_Search, Design, Trie, Top_Interview_150_Trie | 450 | 92.88
1757+
| 0210 |[Course Schedule II](src/main/ts/g0201_0300/s0210_course_schedule_ii/solution.ts)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Level_2_Day_11_Graph/BFS/DFS, Top_Interview_150_Graph_General | 2 | 99.76
17461758
| 0209 |[Minimum Size Subarray Sum](src/main/ts/g0201_0300/s0209_minimum_size_subarray_sum/solution.ts)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Binary_Search_II_Day_1, Top_Interview_150_Sliding_Window | 2 | 88.11
17471759
| 0208 |[Implement Trie (Prefix Tree)](src/main/ts/g0201_0300/s0208_implement_trie_prefix_tree/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap, Top_Interview_150_Trie, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 48 | 63.95
17481760
| 0207 |[Course Schedule](src/main/ts/g0201_0300/s0207_course_schedule/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Top_Interview_150_Graph_General, Big_O_Time_O(N)_Space_O(N) | 11 | 81.08
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
210\. Course Schedule II
2+
3+
Medium
4+
5+
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you **must** take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.
6+
7+
* For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.
8+
9+
Return _the ordering of courses you should take to finish all courses_. If there are many valid answers, return **any** of them. If it is impossible to finish all courses, return **an empty array**.
10+
11+
**Example 1:**
12+
13+
**Input:** numCourses = 2, prerequisites = [[1,0]]
14+
15+
**Output:** [0,1]
16+
17+
**Explanation:**
18+
19+
There are a total of 2 courses to take. To take course 1 you should have finished course 0.
20+
So the correct course order is [0,1].
21+
22+
**Example 2:**
23+
24+
**Input:** numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
25+
26+
**Output:** [0,2,1,3]
27+
28+
**Explanation:**
29+
30+
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
31+
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
32+
33+
**Example 3:**
34+
35+
**Input:** numCourses = 1, prerequisites = []
36+
37+
**Output:** [0]
38+
39+
**Constraints:**
40+
41+
* `1 <= numCourses <= 2000`
42+
* `0 <= prerequisites.length <= numCourses * (numCourses - 1)`
43+
* `prerequisites[i].length == 2`
44+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code>
45+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
46+
* All the pairs <code>[a<sub>i</sub>, b<sub>i</sub>]</code> are **distinct**.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// #Medium #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search #Graph
2+
// #Topological_Sort #Level_2_Day_11_Graph/BFS/DFS #Top_Interview_150_Graph_General
3+
// #2025_04_12_Time_2_ms_(99.76%)_Space_58.61_MB_(86.54%)
4+
5+
function findOrder(numCourses: number, prerequisites: number[][]): number[] {
6+
let sortedOrder: number[] = []
7+
if (numCourses < 0) return sortedOrder
8+
let inDegree = new Array(numCourses).fill(0),
9+
graph = new Array(numCourses).fill(0).map(() => new Array()),
10+
source = new Array<number>()
11+
prerequisites.forEach((course: number[]) => {
12+
graph[course[1]].push(course[0])
13+
inDegree[course[0]]++
14+
})
15+
inDegree.forEach((value: number, index: number) => {
16+
if (value === 0) {
17+
source.push(index)
18+
}
19+
})
20+
while (source.length > 0) {
21+
const course = source.shift()
22+
if (course === undefined) break
23+
sortedOrder.push(course)
24+
graph[course].forEach((val) => {
25+
inDegree[val]--
26+
if (inDegree[val] === 0) {
27+
source.push(val)
28+
}
29+
})
30+
}
31+
return sortedOrder.length === numCourses ? sortedOrder : []
32+
}
33+
34+
export { findOrder }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
211\. Design Add and Search Words Data Structure
2+
3+
Medium
4+
5+
Design a data structure that supports adding new words and finding if a string matches any previously added string.
6+
7+
Implement the `WordDictionary` class:
8+
9+
* `WordDictionary()` Initializes the object.
10+
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
11+
* `bool search(word)` Returns `true` if there is any string in the data structure that matches `word` or `false` otherwise. `word` may contain dots `'.'` where dots can be matched with any letter.
12+
13+
**Example:**
14+
15+
**Input**
16+
17+
["WordDictionary","addWord","addWord","addWord","search","search","search","search"] [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
18+
19+
**Output**
20+
21+
[null,null,null,null,false,true,true,true]
22+
23+
**Explanation**
24+
25+
WordDictionary wordDictionary = new WordDictionary();
26+
wordDictionary.addWord("bad");
27+
wordDictionary.addWord("dad");
28+
wordDictionary.addWord("mad");
29+
wordDictionary.search("pad"); // return False
30+
wordDictionary.search("bad"); // return True
31+
wordDictionary.search(".ad"); // return True
32+
wordDictionary.search("b.."); // return True
33+
34+
**Constraints:**
35+
36+
* `1 <= word.length <= 500`
37+
* `word` in `addWord` consists lower-case English letters.
38+
* `word` in `search` consist of `'.'` or lower-case English letters.
39+
* At most `50000` calls will be made to `addWord` and `search`.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// #Medium #String #Depth_First_Search #Design #Trie #Top_Interview_150_Trie
2+
// #2025_04_12_Time_450_ms_(92.88%)_Space_92.76_MB_(88.14%)
3+
4+
interface TrieNode {
5+
[key: string]: TrieNode | boolean
6+
}
7+
8+
class WordDictionary {
9+
root: TrieNode
10+
11+
constructor() {
12+
this.root = {}
13+
}
14+
15+
addWord(word: string): void {
16+
let current = this.root
17+
for (let i = 0; i < word.length; i++) {
18+
const letter = word[i]
19+
if (!current.hasOwnProperty(letter)) {
20+
current[letter] = {}
21+
}
22+
current = current[letter] as TrieNode
23+
}
24+
current.end = true
25+
}
26+
27+
search(word: string): boolean {
28+
const searchSubtree = (word: string, index: number, subtree: TrieNode) => {
29+
if (word.length === index) {
30+
return subtree.hasOwnProperty('end')
31+
}
32+
const currentLetter = word[index]
33+
index += 1
34+
35+
if (currentLetter === '.') {
36+
return Object.keys(subtree).some((letter) => searchSubtree(word, index, subtree[letter] as TrieNode))
37+
} else {
38+
if (subtree.hasOwnProperty(currentLetter)) {
39+
return searchSubtree(word, index, subtree[currentLetter] as TrieNode)
40+
} else {
41+
return false
42+
}
43+
}
44+
}
45+
46+
return searchSubtree(word, 0, this.root)
47+
}
48+
}
49+
50+
/**
51+
* Your WordDictionary object will be instantiated and called as such:
52+
* var obj = new WordDictionary()
53+
* obj.addWord(word)
54+
* var param_2 = obj.search(word)
55+
*/
56+
57+
export { WordDictionary }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
212\. Word Search II
2+
3+
Hard
4+
5+
Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_.
6+
7+
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/07/search1.jpg)
12+
13+
**Input:** board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
14+
15+
**Output:** ["eat","oath"]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2020/11/07/search2.jpg)
20+
21+
**Input:** board = [["a","b"],["c","d"]], words = ["abcb"]
22+
23+
**Output:** []
24+
25+
**Constraints:**
26+
27+
* `m == board.length`
28+
* `n == board[i].length`
29+
* `1 <= m, n <= 12`
30+
* `board[i][j]` is a lowercase English letter.
31+
* <code>1 <= words.length <= 3 * 10<sup>4</sup></code>
32+
* `1 <= words[i].length <= 10`
33+
* `words[i]` consists of lowercase English letters.
34+
* All the strings of `words` are unique.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// #Hard #Top_Interview_Questions #Array #String #Matrix #Backtracking #Trie #Top_Interview_150_Trie
2+
// #2025_04_12_Time_62_ms_(99.46%)_Space_60.47_MB_(72.83%)
3+
4+
class Tree {
5+
children: Map<string, Tree>
6+
end: string | null
7+
8+
constructor() {
9+
this.children = new Map()
10+
this.end = null
11+
}
12+
13+
static addWord(node: Tree, word: string): void {
14+
let cur = node
15+
for (const char of word) {
16+
if (!cur.children.has(char)) {
17+
cur.children.set(char, new Tree())
18+
}
19+
cur = cur.children.get(char)!
20+
}
21+
cur.end = word
22+
}
23+
24+
static deleteWord(node: Tree, word: string): void {
25+
const stack: [Tree, string][] = []
26+
let cur = node
27+
for (const char of word) {
28+
const next = cur.children.get(char)
29+
if (!next) return
30+
stack.push([cur, char])
31+
cur = next
32+
}
33+
cur.end = null
34+
for (let i = stack.length - 1; i >= 0; i--) {
35+
const [parent, char] = stack[i]
36+
const child = parent.children.get(char)!
37+
if (child.children.size === 0 && child.end === null) {
38+
parent.children.delete(char)
39+
} else {
40+
break
41+
}
42+
}
43+
}
44+
45+
getChild(char: string): Tree | null {
46+
return this.children.get(char) || null
47+
}
48+
49+
len(): number {
50+
return this.children.size
51+
}
52+
}
53+
54+
function findWords(board: string[][], words: string[]): string[] {
55+
if (board.length === 0 || board[0].length === 0) {
56+
return []
57+
}
58+
const root = new Tree()
59+
for (const word of words) {
60+
Tree.addWord(root, word)
61+
}
62+
const collected: string[] = []
63+
for (let i = 0; i < board.length; i++) {
64+
for (let j = 0; j < board[0].length; j++) {
65+
dfs(board, i, j, root, collected, root)
66+
}
67+
}
68+
return collected
69+
}
70+
71+
function dfs(board: string[][], i: number, j: number, cur: Tree, collected: string[], root: Tree): void {
72+
const c = board[i][j]
73+
if (c === '-') {
74+
return
75+
}
76+
const next = cur.getChild(c)
77+
if (!next) {
78+
return
79+
}
80+
if (next.end !== null) {
81+
collected.push(next.end)
82+
const word = next.end
83+
next.end = null
84+
if (next.len() === 0) {
85+
Tree.deleteWord(root, word)
86+
}
87+
}
88+
board[i][j] = '-'
89+
if (i > 0) {
90+
dfs(board, i - 1, j, next, collected, root)
91+
}
92+
if (i + 1 < board.length) {
93+
dfs(board, i + 1, j, next, collected, root)
94+
}
95+
if (j > 0) {
96+
dfs(board, i, j - 1, next, collected, root)
97+
}
98+
if (j + 1 < board[0].length) {
99+
dfs(board, i, j + 1, next, collected, root)
100+
}
101+
board[i][j] = c
102+
}
103+
104+
export { findWords }

0 commit comments

Comments
 (0)