Skip to content

Commit 0624c1a

Browse files
authored
Added tasks 169-200
1 parent 7bc560e commit 0624c1a

File tree

13 files changed

+348
-0
lines changed

13 files changed

+348
-0
lines changed

README.md

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

4040
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
4141
|-|-|-|-|-|-
42+
| 0189 |[Rotate Array](src/main/ts/g0101_0200/s0189_rotate_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 75 | 97.25
4243

4344
#### Day 3 Two Pointers
4445

@@ -96,6 +97,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
9697
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9798
|-|-|-|-|-|-
9899
| 0070 |[Climbing Stairs](src/main/ts/g0001_0100/s0070_climbing_stairs/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 44 | 94.58
100+
| 0198 |[House Robber](src/main/ts/g0101_0200/s0198_house_robber/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 51 | 77.29
99101

100102
#### Day 13 Bit Manipulation
101103

@@ -146,6 +148,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
146148

147149
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
148150
|-|-|-|-|-|-
151+
| 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) | 61 | 96.71
149152

150153
#### Day 7 Breadth First Search Depth First Search
151154

@@ -425,6 +428,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
425428

426429
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
427430
|-|-|-|-|-|-
431+
| 0198 |[House Robber](src/main/ts/g0101_0200/s0198_house_robber/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 51 | 77.29
428432

429433
#### Day 4
430434

@@ -711,6 +715,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
711715

712716
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
713717
|-|-|-|-|-|-
718+
| 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) | 61 | 96.71
714719

715720
#### Day 2 Matrix Related Problems
716721

@@ -881,6 +886,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
881886

882887
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
883888
|-|-|-|-|-|-
889+
| 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) | 61 | 96.71
884890

885891
#### Day 10 Dynamic Programming
886892

@@ -987,6 +993,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
987993

988994
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
989995
|-|-|-|-|-|-
996+
| 0198 |[House Robber](src/main/ts/g0101_0200/s0198_house_robber/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 51 | 77.29
990997
| 0322 |[Coin Change](src/main/ts/g0301_0400/s0322_coin_change/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m*n)_Space_O(amount) | 85 | 86.14
991998

992999
#### Day 13 Dynamic Programming
@@ -1073,6 +1080,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10731080
| 0121 |[Best Time to Buy and Sell Stock](src/main/ts/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 56 | 99.56
10741081
| 0283 |[Move Zeroes](src/main/ts/g0201_0300/s0283_move_zeroes/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 71 | 91.63
10751082
| 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) | 54 | 91.72
1083+
| 0189 |[Rotate Array](src/main/ts/g0101_0200/s0189_rotate_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 75 | 97.25
10761084
| 0055 |[Jump Game](src/main/ts/g0001_0100/s0055_jump_game/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 58 | 91.95
10771085
| 0075 |[Sort Colors](src/main/ts/g0001_0100/s0075_sort_colors/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 43 | 97.41
10781086
| 0238 |[Product of Array Except Self](src/main/ts/g0201_0300/s0238_product_of_array_except_self/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 89 | 64.48
@@ -1091,6 +1099,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10911099
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10921100
|-|-|-|-|-|-
10931101
| 0053 |[Maximum Subarray](src/main/ts/g0001_0100/s0053_maximum_subarray/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 66 | 90.36
1102+
| 0169 |[Majority Element](src/main/ts/g0101_0200/s0169_majority_element/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 50 | 96.16
10941103

10951104
#### Udemy Sorting Algorithms
10961105

@@ -1145,13 +1154,15 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11451154

11461155
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11471156
|-|-|-|-|-|-
1157+
| 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) | 61 | 96.71
11481158

11491159
#### Udemy Dynamic Programming
11501160

11511161
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11521162
|-|-|-|-|-|-
11531163
| 0139 |[Word Break](src/main/ts/g0101_0200/s0139_word_break/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max*N)_Space_O(M+N+max) | 56 | 88.44
11541164
| 0152 |[Maximum Product Subarray](src/main/ts/g0101_0200/s0152_maximum_product_subarray/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 53 | 87.50
1165+
| 0198 |[House Robber](src/main/ts/g0101_0200/s0198_house_robber/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 51 | 77.29
11551166
| 0070 |[Climbing Stairs](src/main/ts/g0001_0100/s0070_climbing_stairs/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 44 | 94.58
11561167
| 0064 |[Minimum Path Sum](src/main/ts/g0001_0100/s0064_minimum_path_sum/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m*n)_Space_O(m*n) | 48 | 99.67
11571168
| 0300 |[Longest Increasing Subsequence](src/main/ts/g0201_0300/s0300_longest_increasing_subsequence/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n*log_n)_Space_O(n) | 57 | 93.75
@@ -1274,6 +1285,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12741285
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12751286
|-|-|-|-|-|-
12761287
| 0136 |[Single Number](src/main/ts/g0101_0200/s0136_single_number/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 56 | 85.48
1288+
| 0169 |[Majority Element](src/main/ts/g0101_0200/s0169_majority_element/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 50 | 96.16
12771289
| 0015 |[3Sum](src/main/ts/g0001_0100/s0015_3sum/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n*log(n))_Space_O(n^2) | 148 | 92.62
12781290

12791291
#### Day 2 Array
@@ -1430,6 +1442,10 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
14301442
| 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, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 168 | 80.99
14311443
| 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) | 68 | 70.14
14321444
| 0206 |[Reverse Linked List](src/main/ts/g0201_0300/s0206_reverse_linked_list/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 51 | 92.87
1445+
| 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, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_1_Matrix_Related_Problems, Level_1_Day_9_Graph/BFS/DFS, Udemy_Graph, Big_O_Time_O(M*N)_Space_O(M*N) | 61 | 96.71
1446+
| 0198 |[House Robber](src/main/ts/g0101_0200/s0198_house_robber/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 51 | 77.29
1447+
| 0189 |[Rotate Array](src/main/ts/g0101_0200/s0189_rotate_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 75 | 97.25
1448+
| 0169 |[Majority Element](src/main/ts/g0101_0200/s0169_majority_element/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Data_Structure_II_Day_1_Array, Udemy_Famous_Algorithm, Big_O_Time_O(n)_Space_O(1) | 50 | 96.16
14331449
| 0160 |[Intersection of Two Linked Lists](src/main/ts/g0101_0200/s0160_intersection_of_two_linked_lists/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Udemy_Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 67 | 93.58
14341450
| 0155 |[Min Stack](src/main/ts/g0101_0200/s0155_min_stack/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Data_Structure_II_Day_14_Stack_Queue, Programming_Skills_II_Day_18, Level_2_Day_16_Design, Udemy_Design, Big_O_Time_O(1)_Space_O(N) | 84 | 92.72
14351451
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/ts/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/solution.ts)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_I_Day_12, Udemy_Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 42 | 98.87
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
169\. Majority Element
2+
3+
Easy
4+
5+
Given an array `nums` of size `n`, return _the majority element_.
6+
7+
The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [3,2,3]
12+
13+
**Output:** 3
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [2,2,1,1,1,2,2]
18+
19+
**Output:** 2
20+
21+
**Constraints:**
22+
23+
* `n == nums.length`
24+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
25+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
26+
27+
**Follow-up:** Could you solve the problem in linear time and in `O(1)` space?
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting #Counting
2+
// #Divide_and_Conquer #Data_Structure_II_Day_1_Array #Udemy_Famous_Algorithm
3+
// #Big_O_Time_O(n)_Space_O(1) #2023_10_09_Time_50_ms_(96.16%)_Space_46.1_MB_(46.90%)
4+
5+
function majorityElement(arr: number[]): number {
6+
let count = 1
7+
let majority = arr[0]
8+
// For Potential Majority Element
9+
for (let i = 1; i < arr.length; i++) {
10+
if (arr[i] === majority) {
11+
count++
12+
} else if (count > 1) {
13+
count--
14+
} else {
15+
majority = arr[i]
16+
}
17+
}
18+
// For Confirmation
19+
count = 0
20+
for (let j of arr) {
21+
if (j === majority) {
22+
count++
23+
}
24+
}
25+
if (count >= Math.floor(arr.length / 2) + 1) {
26+
return majority
27+
}
28+
return -1
29+
}
30+
31+
export { majorityElement }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
189\. Rotate Array
2+
3+
Medium
4+
5+
Given an array, rotate the array to the right by `k` steps, where `k` is non-negative.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,2,3,4,5,6,7], k = 3
10+
11+
**Output:** [5,6,7,1,2,3,4]
12+
13+
**Explanation:**
14+
15+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
16+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
17+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [-1,-100,3,99], k = 2
22+
23+
**Output:** [3,99,-1,-100]
24+
25+
**Explanation:**
26+
27+
rotate 1 steps to the right: [99,-1,-100,3]
28+
rotate 2 steps to the right: [3,99,-1,-100]
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
33+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
34+
* <code>0 <= k <= 10<sup>5</sup></code>
35+
36+
**Follow up:**
37+
38+
* Try to come up with as many solutions as you can. There are at least **three** different ways to solve this problem.
39+
* Could you do it in-place with `O(1)` extra space?
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Two_Pointers
2+
// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
3+
// #2023_10_09_Time_75_ms_(97.25%)_Space_52_MB_(63.34%)
4+
5+
/*
6+
Do not return anything, modify nums in-place instead.
7+
*/
8+
function reverse(nums: number[], l: number, r: number): void {
9+
while (l <= r) {
10+
const temp = nums[l]
11+
nums[l] = nums[r]
12+
nums[r] = temp
13+
l++
14+
r--
15+
}
16+
}
17+
18+
function rotate(nums: number[], k: number): void {
19+
const n = nums.length
20+
const t = n - (k % n)
21+
reverse(nums, 0, t - 1)
22+
reverse(nums, t, n - 1)
23+
reverse(nums, 0, n - 1)
24+
}
25+
26+
export { rotate }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
198\. House Robber
2+
3+
Medium
4+
5+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.
6+
7+
Given an integer array `nums` representing the amount of money of each house, return _the maximum amount of money you can rob tonight **without alerting the police**_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3,1]
12+
13+
**Output:** 4
14+
15+
**Explanation:**
16+
17+
Rob house 1 (money = 1) and then rob house 3 (money = 3).
18+
Total amount you can rob = 1 + 3 = 4.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [2,7,9,3,1]
23+
24+
**Output:** 12
25+
26+
**Explanation:**
27+
28+
Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
29+
Total amount you can rob = 2 + 9 + 1 = 12.
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 100`
34+
* `0 <= nums[i] <= 400`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
2+
// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_3
3+
// #Level_2_Day_12_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n)
4+
// #2023_10_09_Time_51_ms_(77.29%)_Space_42.7_MB_(62.58%)
5+
6+
function rob(nums: number[]): number {
7+
const n = nums.length
8+
if (n === 0) {
9+
return 0
10+
}
11+
if (n === 1) {
12+
return nums[0]
13+
}
14+
if (n === 2) {
15+
return Math.max(nums[0], nums[1])
16+
}
17+
const profit: number[] = new Array(n)
18+
profit[0] = nums[0]
19+
profit[1] = Math.max(nums[0], nums[1])
20+
for (let i = 2; i < n; i++) {
21+
profit[i] = Math.max(profit[i - 1], nums[i] + profit[i - 2])
22+
}
23+
return profit[n - 1]
24+
}
25+
26+
export { rob }
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
200\. Number of Islands
2+
3+
Medium
4+
5+
Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
6+
7+
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
8+
9+
**Example 1:**
10+
11+
**Input:**
12+
13+
grid = [
14+
["1","1","1","1","0"],
15+
["1","1","0","1","0"],
16+
["1","1","0","0","0"],
17+
["0","0","0","0","0"]
18+
]
19+
20+
**Output:** 1
21+
22+
**Example 2:**
23+
24+
**Input:**
25+
26+
grid = [
27+
["1","1","0","0","0"],
28+
["1","1","0","0","0"],
29+
["0","0","1","0","0"],
30+
["0","0","0","1","1"]
31+
]
32+
33+
**Output:** 3
34+
35+
**Constraints:**
36+
37+
* `m == grid.length`
38+
* `n == grid[i].length`
39+
* `1 <= m, n <= 300`
40+
* `grid[i][j]` is `'0'` or `'1'`.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Depth_First_Search
2+
// #Breadth_First_Search #Matrix #Union_Find
3+
// #Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search
4+
// #Graph_Theory_I_Day_1_Matrix_Related_Problems #Level_1_Day_9_Graph/BFS/DFS #Udemy_Graph
5+
// #Big_O_Time_O(M*N)_Space_O(M*N) #2023_10_09_Time_61_ms_(96.71%)_Space_44.5_MB_(99.30%)
6+
7+
function numIslands(grid: string[][]): number {
8+
let islands = 0
9+
if (grid !== null && grid.length !== 0 && grid[0].length !== 0) {
10+
for (let i = 0; i < grid.length; i++) {
11+
for (let j = 0; j < grid[0].length; j++) {
12+
if (grid[i][j] === '1') {
13+
dfs(grid, i, j)
14+
islands++
15+
}
16+
}
17+
}
18+
}
19+
return islands
20+
}
21+
22+
function dfs(grid: string[][], x: number, y: number): void {
23+
if (x < 0 || grid.length <= x || y < 0 || grid[0].length <= y || grid[x][y] !== '1') {
24+
return
25+
}
26+
grid[x][y] = 'x'
27+
dfs(grid, x + 1, y)
28+
dfs(grid, x - 1, y)
29+
dfs(grid, x, y + 1)
30+
dfs(grid, x, y - 1)
31+
}
32+
33+
export { numIslands }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// tslint:disable:no-magic-numbers
2+
import { majorityElement } from 'src/main/ts/g0101_0200/s0169_majority_element/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('majorityElement', () => {
6+
const nums1: number[] = [3, 2, 3]
7+
expect(majorityElement(nums1)).toEqual(3)
8+
})
9+
10+
test('majorityElement2', () => {
11+
const nums2: number[] = [2, 2, 1, 1, 1, 2, 2]
12+
expect(majorityElement(nums2)).toEqual(2)
13+
})

0 commit comments

Comments
 (0)