Skip to content

Commit 24d632c

Browse files
committed
Added tasks 130, 133
1 parent 950ed7f commit 24d632c

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@
990990
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
991991
|-|-|-|-|-|-
992992
| 0200 |[Number of Islands](src/main/ts/g0101_0200/s0200_number_of_islands)| 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
993+
| 0133 |[Clone Graph](src/main/ts/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 48 | 82.94
993994

994995
#### Udemy Dynamic Programming
995996

@@ -1180,6 +1181,8 @@
11801181
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11811182
|-|-|-|-|-|-
11821183
| 0200 |[Number of Islands](src/main/ts/g0101_0200/s0200_number_of_islands)| 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
1184+
| 0130 |[Surrounded Regions](src/main/ts/g0101_0200/s0130_surrounded_regions)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 1 | 99.18
1185+
| 0133 |[Clone Graph](src/main/ts/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 48 | 82.94
11831186
| 0399 |[Evaluate Division](src/main/ts/g0301_0400/s0399_evaluate_division)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path | 0 | 100.00
11841187
| 0207 |[Course Schedule](src/main/ts/g0201_0300/s0207_course_schedule)| 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
11851188
| 0210 |[Course Schedule II](src/main/ts/g0201_0300/s0210_course_schedule_ii)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 2 | 99.76
@@ -1663,6 +1666,7 @@
16631666

16641667
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16651668
|-|-|-|-|-|-
1669+
| 0130 |[Surrounded Regions](src/main/ts/g0101_0200/s0130_surrounded_regions)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 1 | 99.18
16661670

16671671
#### Day 9 Recursion Backtracking
16681672

@@ -1837,7 +1841,9 @@
18371841
| 0136 |[Single Number](src/main/ts/g0101_0200/s0136_single_number)| 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
18381842
| 0135 |[Candy](src/main/ts/g0101_0200/s0135_candy)| Hard | Array, Greedy, Top_Interview_150_Array/String | 2 | 96.15
18391843
| 0134 |[Gas Station](src/main/ts/g0101_0200/s0134_gas_station)| Medium | Top_Interview_Questions, Array, Greedy, Top_Interview_150_Array/String | 0 | 100.00
1844+
| 0133 |[Clone Graph](src/main/ts/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph, Udemy_Graph, Top_Interview_150_Graph_General | 48 | 82.94
18401845
| 0131 |[Palindrome Partitioning](src/main/ts/g0101_0200/s0131_palindrome_partitioning)| 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
1846+
| 0130 |[Surrounded Regions](src/main/ts/g0101_0200/s0130_surrounded_regions)| 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
18411847
| 0129 |[Sum Root to Leaf Numbers](src/main/ts/g0101_0200/s0129_sum_root_to_leaf_numbers)| Medium | Depth_First_Search, Tree, Binary_Tree, Top_Interview_150_Binary_Tree_General | 0 | 100.00
18421848
| 0128 |[Longest Consecutive Sequence](src/main/ts/g0101_0200/s0128_longest_consecutive_sequence)| 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
18431849
| 0127 |[Word Ladder](src/main/ts/g0101_0200/s0127_word_ladder)| 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,86 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 130\. Surrounded Regions
5+
6+
Medium
7+
8+
Given an `m x n` matrix `board` containing `'X'` and `'O'`, _capture all regions that are 4-directionally surrounded by_ `'X'`.
9+
10+
A region is **captured** by flipping all `'O'`s into `'X'`s in that surrounded region.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg)
15+
16+
**Input:** board = \[\["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
17+
18+
**Output:** [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
19+
20+
**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.
21+
22+
**Example 2:**
23+
24+
**Input:** board = \[\["X"]]
25+
26+
**Output:** [["X"]]
27+
28+
**Constraints:**
29+
30+
* `m == board.length`
31+
* `n == board[i].length`
32+
* `1 <= m, n <= 200`
33+
* `board[i][j]` is `'X'` or `'O'`.
34+
35+
## Solution
36+
37+
```typescript
38+
/**
39+
Do not return anything, modify board in-place instead.
40+
*/
41+
function solve(board: string[][]): void {
42+
if (board.length === 0) {
43+
return
44+
}
45+
const rows = board.length
46+
const cols = board[0].length
47+
const dfs = (board: string[][], row: number, col: number): void => {
48+
if (row < 0 || row >= rows || col < 0 || col >= cols || board[row][col] !== 'O') {
49+
return
50+
}
51+
board[row][col] = '#'
52+
dfs(board, row + 1, col)
53+
dfs(board, row - 1, col)
54+
dfs(board, row, col + 1)
55+
dfs(board, row, col - 1)
56+
}
57+
for (let i = 0; i < cols; i++) {
58+
if (board[0][i] === 'O') {
59+
dfs(board, 0, i)
60+
}
61+
if (board[rows - 1][i] === 'O') {
62+
dfs(board, rows - 1, i)
63+
}
64+
}
65+
for (let i = 0; i < rows; i++) {
66+
if (board[i][0] === 'O') {
67+
dfs(board, i, 0)
68+
}
69+
if (board[i][cols - 1] === 'O') {
70+
dfs(board, i, cols - 1)
71+
}
72+
}
73+
for (let i = 0; i < rows; i++) {
74+
for (let j = 0; j < cols; j++) {
75+
if (board[i][j] === 'O') {
76+
board[i][j] = 'X'
77+
}
78+
if (board[i][j] === '#') {
79+
board[i][j] = 'O'
80+
}
81+
}
82+
}
83+
}
84+
85+
export { solve }
86+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 133\. Clone Graph
5+
6+
Medium
7+
8+
Given a reference of a node in a **[connected](https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph)** undirected graph.
9+
10+
Return a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) (clone) of the graph.
11+
12+
Each node in the graph contains a value (`int`) and a list (`List[Node]`) of its neighbors.
13+
14+
class Node { public int val; public List<Node> neighbors; }
15+
16+
**Test case format:**
17+
18+
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.
19+
20+
**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.
21+
22+
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.
23+
24+
**Example 1:**
25+
26+
![](https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png)
27+
28+
**Input:** adjList = \[\[2,4],[1,3],[2,4],[1,3]]
29+
30+
**Output:** [[2,4],[1,3],[2,4],[1,3]]
31+
32+
**Explanation:**
33+
34+
There are 4 nodes in the graph.
35+
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
36+
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
37+
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
38+
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
39+
40+
**Example 2:**
41+
42+
![](https://assets.leetcode.com/uploads/2020/01/07/graph.png)
43+
44+
**Input:** adjList = \[\[]]
45+
46+
**Output:** [[]]
47+
48+
**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.
49+
50+
**Example 3:**
51+
52+
**Input:** adjList = []
53+
54+
**Output:** []
55+
56+
**Explanation:** This an empty graph, it does not have any nodes.
57+
58+
**Example 4:**
59+
60+
![](https://assets.leetcode.com/uploads/2020/01/07/graph-1.png)
61+
62+
**Input:** adjList = \[\[2],[1]]
63+
64+
**Output:** [[2],[1]]
65+
66+
**Constraints:**
67+
68+
* The number of nodes in the graph is in the range `[0, 100]`.
69+
* `1 <= Node.val <= 100`
70+
* `Node.val` is unique for each node.
71+
* There are no repeated edges and no self-loops in the graph.
72+
* The Graph is connected and all nodes can be visited starting from the given node.
73+
74+
## Solution
75+
76+
```typescript
77+
class Node {
78+
val: number
79+
neighbors: Node[]
80+
81+
constructor(val: number = 0, neighbors: Node[] = []) {
82+
this.val = val
83+
this.neighbors = neighbors
84+
}
85+
86+
toString(): string {
87+
const result: string[] = []
88+
for (const node of this.neighbors) {
89+
if (node.neighbors.length === 0) {
90+
result.push(node.val.toString())
91+
} else {
92+
const result2: string[] = []
93+
for (const nodeItem of node.neighbors) {
94+
result2.push(nodeItem.val.toString())
95+
}
96+
result.push(`[${result2.join(',')}]`)
97+
}
98+
}
99+
return `[${result.join(',')}]`
100+
}
101+
}
102+
103+
function cloneGraph(node: Node | null): Node | null {
104+
const processedNodes = new Map<Node, Node>()
105+
return cloneGraphHelper(node, processedNodes)
106+
}
107+
108+
function cloneGraphHelper(node: Node | null, processedNodes: Map<Node, Node>): Node | null {
109+
if (node === null) {
110+
return null
111+
}
112+
if (processedNodes.has(node)) {
113+
return processedNodes.get(node)!
114+
}
115+
const newNode = new Node(node.val)
116+
processedNodes.set(node, newNode)
117+
for (const neighbor of node.neighbors) {
118+
const clonedNeighbor = cloneGraphHelper(neighbor, processedNodes)
119+
if (clonedNeighbor !== null) {
120+
newNode.neighbors.push(clonedNeighbor)
121+
}
122+
}
123+
return newNode
124+
}
125+
126+
export { Node, cloneGraph }
127+
```

0 commit comments

Comments
 (0)