Skip to content

Commit 7f37c9e

Browse files
authored
Added tasks 130, 133
1 parent fb741da commit 7f37c9e

File tree

7 files changed

+257
-0
lines changed

7 files changed

+257
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
996996
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
997997
|-|-|-|-|-|-
998998
| 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
999+
| 0133 |[Clone Graph](src/main/ts/g0101_0200/s0133_clone_graph/solution.ts)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 48 | 82.94
9991000

10001001
#### Udemy Dynamic Programming
10011002

@@ -1186,6 +1187,8 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11861187
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11871188
|-|-|-|-|-|-
11881189
| 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
1190+
| 0130 |[Surrounded Regions](src/main/ts/g0101_0200/s0130_surrounded_regions/solution.ts)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 1 | 99.18
1191+
| 0133 |[Clone Graph](src/main/ts/g0101_0200/s0133_clone_graph/solution.ts)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 48 | 82.94
11891192
| 0399 |[Evaluate Division](src/main/ts/g0301_0400/s0399_evaluate_division/solution.ts)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path | 0 | 100.00
11901193
| 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
11911194
| 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
@@ -1669,6 +1672,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
16691672

16701673
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16711674
|-|-|-|-|-|-
1675+
| 0130 |[Surrounded Regions](src/main/ts/g0101_0200/s0130_surrounded_regions/solution.ts)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 1 | 99.18
16721676

16731677
#### Day 9 Recursion Backtracking
16741678

@@ -1843,7 +1847,9 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
18431847
| 0136 |[Single Number](src/main/ts/g0101_0200/s0136_single_number/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Top_Interview_150_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 1 | 78.27
18441848
| 0135 |[Candy](src/main/ts/g0101_0200/s0135_candy/solution.ts)| Hard | Array, Greedy, Top_Interview_150_Array/String | 2 | 96.15
18451849
| 0134 |[Gas Station](src/main/ts/g0101_0200/s0134_gas_station/solution.ts)| Medium | Top_Interview_Questions, Array, Greedy, Top_Interview_150_Array/String | 0 | 100.00
1850+
| 0133 |[Clone Graph](src/main/ts/g0101_0200/s0133_clone_graph/solution.ts)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph, Udemy_Graph, Top_Interview_150_Graph_General | 48 | 82.94
18461851
| 0131 |[Palindrome Partitioning](src/main/ts/g0101_0200/s0131_palindrome_partitioning/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 13 | 94.96
1852+
| 0130 |[Surrounded Regions](src/main/ts/g0101_0200/s0130_surrounded_regions/solution.ts)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_8_Breadth_First_Search_Depth_First_Search, Top_Interview_150_Graph_General | 1 | 99.18
18471853
| 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, Top_Interview_150_Binary_Tree_General | 0 | 100.00
18481854
| 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, Top_Interview_150_Hashmap, Big_O_Time_O(N_log_N)_Space_O(1) | 34 | 90.07
18491855
| 0127 |[Word Ladder](src/main/ts/g0101_0200/s0127_word_ladder/solution.ts)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search, Graph_Theory_I_Day_12_Breadth_First_Search, Top_Interview_150_Graph_BFS | 41 | 95.63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
130\. Surrounded Regions
2+
3+
Medium
4+
5+
Given an `m x n` matrix `board` containing `'X'` and `'O'`, _capture all regions that are 4-directionally surrounded by_ `'X'`.
6+
7+
A region is **captured** by flipping all `'O'`s into `'X'`s in that surrounded region.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg)
12+
13+
**Input:** board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
14+
15+
**Output:** [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
16+
17+
**Explanation:** Surrounded regions should not be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
18+
19+
**Example 2:**
20+
21+
**Input:** board = [["X"]]
22+
23+
**Output:** [["X"]]
24+
25+
**Constraints:**
26+
27+
* `m == board.length`
28+
* `n == board[i].length`
29+
* `1 <= m, n <= 200`
30+
* `board[i][j]` is `'X'` or `'O'`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// #Medium #Top_Interview_Questions #Array #Depth_First_Search #Breadth_First_Search #Matrix
2+
// #Union_Find #Algorithm_II_Day_8_Breadth_First_Search_Depth_First_Search
3+
// #Top_Interview_150_Graph_General #2025_04_16_Time_1_ms_(99.18%)_Space_59.64_MB_(82.97%)
4+
5+
/**
6+
Do not return anything, modify board in-place instead.
7+
*/
8+
function solve(board: string[][]): void {
9+
if (board.length === 0) {
10+
return
11+
}
12+
const rows = board.length
13+
const cols = board[0].length
14+
const dfs = (board: string[][], row: number, col: number): void => {
15+
if (row < 0 || row >= rows || col < 0 || col >= cols || board[row][col] !== 'O') {
16+
return
17+
}
18+
board[row][col] = '#'
19+
dfs(board, row + 1, col)
20+
dfs(board, row - 1, col)
21+
dfs(board, row, col + 1)
22+
dfs(board, row, col - 1)
23+
}
24+
for (let i = 0; i < cols; i++) {
25+
if (board[0][i] === 'O') {
26+
dfs(board, 0, i)
27+
}
28+
if (board[rows - 1][i] === 'O') {
29+
dfs(board, rows - 1, i)
30+
}
31+
}
32+
for (let i = 0; i < rows; i++) {
33+
if (board[i][0] === 'O') {
34+
dfs(board, i, 0)
35+
}
36+
if (board[i][cols - 1] === 'O') {
37+
dfs(board, i, cols - 1)
38+
}
39+
}
40+
for (let i = 0; i < rows; i++) {
41+
for (let j = 0; j < cols; j++) {
42+
if (board[i][j] === 'O') {
43+
board[i][j] = 'X'
44+
}
45+
if (board[i][j] === '#') {
46+
board[i][j] = 'O'
47+
}
48+
}
49+
}
50+
}
51+
52+
export { solve }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
133\. Clone Graph
2+
3+
Medium
4+
5+
Given a reference of a node in a **[connected](https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph)** undirected graph.
6+
7+
Return a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) (clone) of the graph.
8+
9+
Each node in the graph contains a value (`int`) and a list (`List[Node]`) of its neighbors.
10+
11+
class Node { public int val; public List<Node> neighbors; }
12+
13+
**Test case format:**
14+
15+
For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with `val == 1`, the second node with `val == 2`, and so on. The graph is represented in the test case using an adjacency list.
16+
17+
**An adjacency list** is a collection of unordered **lists** used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
18+
19+
The given node will always be the first node with `val = 1`. You must return the **copy of the given node** as a reference to the cloned graph.
20+
21+
**Example 1:**
22+
23+
![](https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png)
24+
25+
**Input:** adjList = [[2,4],[1,3],[2,4],[1,3]]
26+
27+
**Output:** [[2,4],[1,3],[2,4],[1,3]]
28+
29+
**Explanation:**
30+
31+
There are 4 nodes in the graph.
32+
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
33+
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
34+
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
35+
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
36+
37+
**Example 2:**
38+
39+
![](https://assets.leetcode.com/uploads/2020/01/07/graph.png)
40+
41+
**Input:** adjList = [[]]
42+
43+
**Output:** [[]]
44+
45+
**Explanation:** Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
46+
47+
**Example 3:**
48+
49+
**Input:** adjList = []
50+
51+
**Output:** []
52+
53+
**Explanation:** This an empty graph, it does not have any nodes.
54+
55+
**Example 4:**
56+
57+
![](https://assets.leetcode.com/uploads/2020/01/07/graph-1.png)
58+
59+
**Input:** adjList = [[2],[1]]
60+
61+
**Output:** [[2],[1]]
62+
63+
**Constraints:**
64+
65+
* The number of nodes in the graph is in the range `[0, 100]`.
66+
* `1 <= Node.val <= 100`
67+
* `Node.val` is unique for each node.
68+
* There are no repeated edges and no self-loops in the graph.
69+
* The Graph is connected and all nodes can be visited starting from the given node.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Udemy_Graph
2+
// #Top_Interview_150_Graph_General #2025_04_16_Time_48_ms_(82.94%)_Space_58.20_MB_(70.18%)
3+
4+
class Node {
5+
val: number
6+
neighbors: Node[]
7+
8+
constructor(val: number = 0, neighbors: Node[] = []) {
9+
this.val = val
10+
this.neighbors = neighbors
11+
}
12+
13+
toString(): string {
14+
const result: string[] = []
15+
for (const node of this.neighbors) {
16+
if (node.neighbors.length === 0) {
17+
result.push(node.val.toString())
18+
} else {
19+
const result2: string[] = []
20+
for (const nodeItem of node.neighbors) {
21+
result2.push(nodeItem.val.toString())
22+
}
23+
result.push(`[${result2.join(',')}]`)
24+
}
25+
}
26+
return `[${result.join(',')}]`
27+
}
28+
}
29+
30+
function cloneGraph(node: Node | null): Node | null {
31+
const processedNodes = new Map<Node, Node>()
32+
return cloneGraphHelper(node, processedNodes)
33+
}
34+
35+
function cloneGraphHelper(node: Node | null, processedNodes: Map<Node, Node>): Node | null {
36+
if (node === null) {
37+
return null
38+
}
39+
if (processedNodes.has(node)) {
40+
return processedNodes.get(node)!
41+
}
42+
const newNode = new Node(node.val)
43+
processedNodes.set(node, newNode)
44+
for (const neighbor of node.neighbors) {
45+
const clonedNeighbor = cloneGraphHelper(neighbor, processedNodes)
46+
if (clonedNeighbor !== null) {
47+
newNode.neighbors.push(clonedNeighbor)
48+
}
49+
}
50+
return newNode
51+
}
52+
53+
export { Node, cloneGraph }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// tslint:disable:no-magic-numbers
2+
import { solve } from 'src/main/ts/g0101_0200/s0130_surrounded_regions/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('solve', () => {
6+
const board: string[][] = [
7+
['X', 'X', 'X', 'X'],
8+
['X', 'O', 'O', 'X'],
9+
['X', 'X', 'O', 'X'],
10+
['X', 'O', 'X', 'X'],
11+
]
12+
solve(board)
13+
expect(board).toEqual([
14+
['X', 'X', 'X', 'X'],
15+
['X', 'X', 'X', 'X'],
16+
['X', 'X', 'X', 'X'],
17+
['X', 'O', 'X', 'X'],
18+
])
19+
})
20+
21+
test('solve', () => {
22+
const board: string[][] = [['X']]
23+
solve(board)
24+
expect(board).toEqual([['X']])
25+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// tslint:disable:no-magic-numbers
2+
import { Node, cloneGraph } from 'src/main/ts/g0101_0200/s0133_clone_graph/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('cloneGraph', () => {
6+
const node1 = new Node(1)
7+
const node2 = new Node(2)
8+
const node3 = new Node(3)
9+
const node4 = new Node(4)
10+
node1.neighbors = [node2, node4]
11+
node2.neighbors = [node1, node3]
12+
node3.neighbors = [node2, node4]
13+
node4.neighbors = [node1, node3]
14+
const clonedNode1 = cloneGraph(node1)
15+
expect(clonedNode1?.toString()).toEqual('[[1,3],[1,3]]')
16+
})
17+
18+
test('cloneGraph2', () => {
19+
const node1 = new Node(1)
20+
const clonedNode1 = cloneGraph(node1)
21+
expect(clonedNode1?.toString()).toEqual('[]')
22+
})

0 commit comments

Comments
 (0)