Skip to content

Commit 543a173

Browse files
authored
Added tasks 1901, 1903, 1904, 1905, 1906, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1920, 1921, 1922, 1923, 1925, 1926, 1927, 1928, 1929, 1930, 1931, 1932, 1935, 1936, 1937, 1938, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948
1 parent 9f1c86b commit 543a173

File tree

112 files changed

+4415
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+4415
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
11271127
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11281128
|-|-|-|-|-|-
11291129
| 1802 |[Maximum Value at a Given Index in a Bounded Array](src/main/kotlin/g1801_1900/s1802_maximum_value_at_a_given_index_in_a_bounded_array/Solution.kt)| Medium | Greedy, Binary_Search | 118 | 100.00
1130+
| 1901 |[Find a Peak Element II](src/main/kotlin/g1901_2000/s1901_find_a_peak_element_ii/Solution.kt)| Medium | Array, Binary_Search, Matrix, Divide_and_Conquer | 726 | 100.00
11301131

11311132
#### Day 18
11321133

@@ -1560,6 +1561,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
15601561
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15611562
|-|-|-|-|-|-
15621563
| 1020 |[Number of Enclaves](src.save/main/kotlin/g1001_1100/s1020_number_of_enclaves/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 369 | 76.26
1564+
| 1905 |[Count Sub Islands](src/main/kotlin/g1901_2000/s1905_count_sub_islands/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 866 | 100.00
15631565

15641566
#### Day 4 Matrix Related Problems
15651567

@@ -1580,6 +1582,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
15801582
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15811583
|-|-|-|-|-|-
15821584
| 0934 |[Shortest Bridge](src.save/main/kotlin/g0901_1000/s0934_shortest_bridge/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 301 | 80.95
1585+
| 1926 |[Nearest Exit from Entrance in Maze](src/main/kotlin/g1901_2000/s1926_nearest_exit_from_entrance_in_maze/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix | 351 | 94.44
15831586

15841587
#### Day 7 Standard Traversal
15851588

@@ -1834,6 +1837,43 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
18341837

18351838
| # | Title | Difficulty | Tag | Time, ms | Time, %
18361839
|------|----------------|-------------|-------------|----------|---------
1840+
| 1948 |[Delete Duplicate Folders in System](src/main/kotlin/g1901_2000/s1948_delete_duplicate_folders_in_system/Solution.kt)| Hard | Array, String, Hash_Table, Trie, Hash_Function | 1420 | 100.00
1841+
| 1947 |[Maximum Compatibility Score Sum](src/main/kotlin/g1901_2000/s1947_maximum_compatibility_score_sum/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 179 | 100.00
1842+
| 1946 |[Largest Number After Mutating Substring](src/main/kotlin/g1901_2000/s1946_largest_number_after_mutating_substring/Solution.kt)| Medium | Array, String, Greedy | 346 | 100.00
1843+
| 1945 |[Sum of Digits of String After Convert](src/main/kotlin/g1901_2000/s1945_sum_of_digits_of_string_after_convert/Solution.kt)| Easy | String, Simulation | 146 | 75.00
1844+
| 1944 |[Number of Visible People in a Queue](src/main/kotlin/g1901_2000/s1944_number_of_visible_people_in_a_queue/Solution.kt)| Hard | Array, Stack, Monotonic_Stack | 605 | 90.00
1845+
| 1943 |[Describe the Painting](src/main/kotlin/g1901_2000/s1943_describe_the_painting/Solution.kt)| Medium | Array, Prefix_Sum | 951 | 100.00
1846+
| 1942 |[The Number of the Smallest Unoccupied Chair](src/main/kotlin/g1901_2000/s1942_the_number_of_the_smallest_unoccupied_chair/Solution.kt)| Medium | Array, Heap_Priority_Queue, Ordered_Set | 549 | 100.00
1847+
| 1941 |[Check if All Characters Have Equal Number of Occurrences](src/main/kotlin/g1901_2000/s1941_check_if_all_characters_have_equal_number_of_occurrences/Solution.kt)| Easy | String, Hash_Table, Counting | 167 | 78.57
1848+
| 1938 |[Maximum Genetic Difference Query](src/main/kotlin/g1901_2000/s1938_maximum_genetic_difference_query/Solution.kt)| Hard | Array, Bit_Manipulation, Trie | 855 | 100.00
1849+
| 1937 |[Maximum Number of Points with Cost](src/main/kotlin/g1901_2000/s1937_maximum_number_of_points_with_cost/Solution.kt)| Medium | Array, Dynamic_Programming | 886 | 100.00
1850+
| 1936 |[Add Minimum Number of Rungs](src/main/kotlin/g1901_2000/s1936_add_minimum_number_of_rungs/Solution.kt)| Medium | Array, Greedy | 405 | 100.00
1851+
| 1935 |[Maximum Number of Words You Can Type](src/main/kotlin/g1901_2000/s1935_maximum_number_of_words_you_can_type/Solution.kt)| Easy | String, Hash_Table | 178 | 37.50
1852+
| 1932 |[Merge BSTs to Create Single BST](src/main/kotlin/g1901_2000/s1932_merge_bsts_to_create_single_bst/Solution.kt)| Hard | Hash_Table, Depth_First_Search, Tree, Binary_Search, Binary_Tree | 1146 | 100.00
1853+
| 1931 |[Painting a Grid With Three Different Colors](src/main/kotlin/g1901_2000/s1931_painting_a_grid_with_three_different_colors/Solution.kt)| Hard | Dynamic_Programming | 135 | 100.00
1854+
| 1930 |[Unique Length-3 Palindromic Subsequences](src/main/kotlin/g1901_2000/s1930_unique_length_3_palindromic_subsequences/Solution.kt)| Medium | String, Hash_Table, Prefix_Sum | 273 | 100.00
1855+
| 1929 |[Concatenation of Array](src/main/kotlin/g1901_2000/s1929_concatenation_of_array/Solution.kt)| Easy | Array | 220 | 94.47
1856+
| 1928 |[Minimum Cost to Reach Destination in Time](src/main/kotlin/g1901_2000/s1928_minimum_cost_to_reach_destination_in_time/Solution.kt)| Hard | Dynamic_Programming, Graph | 414 | 100.00
1857+
| 1927 |[Sum Game](src/main/kotlin/g1901_2000/s1927_sum_game/Solution.kt)| Medium | Math, Greedy, Game_Theory | 211 | 100.00
1858+
| 1926 |[Nearest Exit from Entrance in Maze](src/main/kotlin/g1901_2000/s1926_nearest_exit_from_entrance_in_maze/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Graph_Theory_I_Day_6_Matrix_Related_Problems | 351 | 94.44
1859+
| 1925 |[Count Square Sum Triples](src/main/kotlin/g1901_2000/s1925_count_square_sum_triples/Solution.kt)| Easy | Math, Enumeration | 161 | 40.00
1860+
| 1923 |[Longest Common Subpath](src/main/kotlin/g1901_2000/s1923_longest_common_subpath/Solution.kt)| Hard | Array, Binary_Search, Hash_Function, Rolling_Hash, Suffix_Array | 1142 | 100.00
1861+
| 1922 |[Count Good Numbers](src/main/kotlin/g1901_2000/s1922_count_good_numbers/Solution.kt)| Medium | Math, Recursion | 135 | 100.00
1862+
| 1921 |[Eliminate Maximum Number of Monsters](src/main/kotlin/g1901_2000/s1921_eliminate_maximum_number_of_monsters/Solution.kt)| Medium | Array, Sorting, Greedy | 507 | 100.00
1863+
| 1920 |[Build Array from Permutation](src/main/kotlin/g1901_2000/s1920_build_array_from_permutation/Solution.kt)| Easy | Array, Simulation | 252 | 81.25
1864+
| 1916 |[Count Ways to Build Rooms in an Ant Colony](src/main/kotlin/g1901_2000/s1916_count_ways_to_build_rooms_in_an_ant_colony/Solution.kt)| Hard | Dynamic_Programming, Math, Tree, Graph, Topological_Sort, Combinatorics | 2564 | 100.00
1865+
| 1915 |[Number of Wonderful Substrings](src/main/kotlin/g1901_2000/s1915_number_of_wonderful_substrings/Solution.kt)| Medium | String, Hash_Table, Bit_Manipulation, Prefix_Sum | 267 | 100.00
1866+
| 1914 |[Cyclically Rotating a Grid](src/main/kotlin/g1901_2000/s1914_cyclically_rotating_a_grid/Solution.kt)| Medium | Array, Matrix, Simulation | 282 | 100.00
1867+
| 1913 |[Maximum Product Difference Between Two Pairs](src/main/kotlin/g1901_2000/s1913_maximum_product_difference_between_two_pairs/Solution.kt)| Easy | Array, Sorting | 261 | 100.00
1868+
| 1912 |[Design Movie Rental System](src/main/kotlin/g1901_2000/s1912_design_movie_rental_system/MovieRentingSystem.kt)| Hard | Array, Hash_Table, Design, Heap_Priority_Queue, Ordered_Set | 3005 | 100.00
1869+
| 1911 |[Maximum Alternating Subsequence Sum](src/main/kotlin/g1901_2000/s1911_maximum_alternating_subsequence_sum/Solution.kt)| Medium | Array, Dynamic_Programming | 531 | 100.00
1870+
| 1910 |[Remove All Occurrences of a Substring](src/main/kotlin/g1901_2000/s1910_remove_all_occurrences_of_a_substring/Solution.kt)| Medium | String | 177 | 100.00
1871+
| 1909 |[Remove One Element to Make the Array Strictly Increasing](src/main/kotlin/g1901_2000/s1909_remove_one_element_to_make_the_array_strictly_increasing/Solution.kt)| Easy | Array | 176 | 50.00
1872+
| 1906 |[Minimum Absolute Difference Queries](src/main/kotlin/g1901_2000/s1906_minimum_absolute_difference_queries/Solution.kt)| Medium | Array, Hash_Table | 1069 | 50.00
1873+
| 1905 |[Count Sub Islands](src/main/kotlin/g1901_2000/s1905_count_sub_islands/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Graph_Theory_I_Day_3_Matrix_Related_Problems | 866 | 100.00
1874+
| 1904 |[The Number of Full Rounds You Have Played](src/main/kotlin/g1901_2000/s1904_the_number_of_full_rounds_you_have_played/Solution.kt)| Medium | String, Math | 149 | 100.00
1875+
| 1903 |[Largest Odd Number in String](src/main/kotlin/g1901_2000/s1903_largest_odd_number_in_string/Solution.kt)| Easy | String, Math, Greedy | 256 | 75.00
1876+
| 1901 |[Find a Peak Element II](src/main/kotlin/g1901_2000/s1901_find_a_peak_element_ii/Solution.kt)| Medium | Array, Binary_Search, Matrix, Divide_and_Conquer, Binary_Search_II_Day_17 | 726 | 100.00
18371877
| 1824 |[Minimum Sideway Jumps](src/main/kotlin/g1801_1900/s1824_minimum_sideway_jumps/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy | 726 | 100.00
18381878
| 1823 |[Find the Winner of the Circular Game](src/main/kotlin/g1801_1900/s1823_find_the_winner_of_the_circular_game/Solution.kt)| Medium | Array, Math, Simulation, Recursion, Queue, Data_Structure_II_Day_14_Stack_Queue | 119 | 87.50
18391879
| 1822 |[Sign of the Product of an Array](src/main/kotlin/g1801_1900/s1822_sign_of_the_product_of_an_array/Solution.kt)| Easy | Array, Math, Programming_Skills_I_Day_4_Loop | 170 | 92.51
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g1901_2000.s1901_find_a_peak_element_ii
2+
3+
// #Medium #Array #Binary_Search #Matrix #Divide_and_Conquer #Binary_Search_II_Day_17
4+
// #2023_06_19_Time_726_ms_(100.00%)_Space_92.7_MB_(100.00%)
5+
6+
class Solution {
7+
fun findPeakGrid(mat: Array<IntArray>): IntArray {
8+
val n = mat.size
9+
val m = mat[0].size
10+
var l = 0
11+
var r = m - 1
12+
var mid: Int
13+
while (l <= r) {
14+
mid = (l + r) / 2
15+
var mx = mat[0][mid]
16+
var mxi = 0
17+
for (i in 1 until n) {
18+
if (mx < mat[i][mid]) {
19+
mx = mat[i][mid]
20+
mxi = i
21+
}
22+
}
23+
val lv = if (mid > l) mat[mxi][mid - 1] else -1
24+
val rv = if (mid < r) mat[mxi][mid + 1] else -1
25+
if (mx > lv && mx > rv) {
26+
return intArrayOf(mxi, mid)
27+
} else if (mx > lv) {
28+
l = mid + 1
29+
} else {
30+
r = mid - 1
31+
}
32+
}
33+
return intArrayOf(-1, -1)
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1901\. Find a Peak Element II
2+
3+
Medium
4+
5+
A **peak** element in a 2D grid is an element that is **strictly greater** than all of its **adjacent** neighbors to the left, right, top, and bottom.
6+
7+
Given a **0-indexed** `m x n` matrix `mat` where **no two adjacent cells are equal**, find **any** peak element `mat[i][j]` and return _the length 2 array_ `[i,j]`.
8+
9+
You may assume that the entire matrix is surrounded by an **outer perimeter** with the value `-1` in each cell.
10+
11+
You must write an algorithm that runs in `O(m log(n))` or `O(n log(m))` time.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/06/08/1.png)
16+
17+
**Input:** mat = [[1,4],[3,2]]
18+
19+
**Output:** [0,1]
20+
21+
**Explanation:** Both 3 and 4 are peak elements so [1,0] and [0,1] are both acceptable answers.
22+
23+
**Example 2:**
24+
25+
**![](https://assets.leetcode.com/uploads/2021/06/07/3.png)**
26+
27+
**Input:** mat = [[10,20,15],[21,30,14],[7,16,32]]
28+
29+
**Output:** [1,1]
30+
31+
**Explanation:** Both 30 and 32 are peak elements so [1,1] and [2,2] are both acceptable answers.
32+
33+
**Constraints:**
34+
35+
* `m == mat.length`
36+
* `n == mat[i].length`
37+
* `1 <= m, n <= 500`
38+
* <code>1 <= mat[i][j] <= 10<sup>5</sup></code>
39+
* No two adjacent cells are equal.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g1901_2000.s1903_largest_odd_number_in_string
2+
3+
// #Easy #String #Math #Greedy #2023_06_19_Time_256_ms_(75.00%)_Space_39.4_MB_(87.50%)
4+
5+
class Solution {
6+
fun largestOddNumber(num: String): String {
7+
for (i in num.length - 1 downTo 0) {
8+
if (("" + num[i]).toInt() % 2 == 1) {
9+
return num.substring(0, i + 1)
10+
}
11+
}
12+
return ""
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1903\. Largest Odd Number in String
2+
3+
Easy
4+
5+
You are given a string `num`, representing a large integer. Return _the **largest-valued odd** integer (as a string) that is a **non-empty substring** of_ `num`_, or an empty string_ `""` _if no odd integer exists_.
6+
7+
A **substring** is a contiguous sequence of characters within a string.
8+
9+
**Example 1:**
10+
11+
**Input:** num = "52"
12+
13+
**Output:** "5"
14+
15+
**Explanation:** The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.
16+
17+
**Example 2:**
18+
19+
**Input:** num = "4206"
20+
21+
**Output:** ""
22+
23+
**Explanation:** There are no odd numbers in "4206".
24+
25+
**Example 3:**
26+
27+
**Input:** num = "35427"
28+
29+
**Output:** "35427"
30+
31+
**Explanation:** "35427" is already an odd number.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= num.length <= 10<sup>5</sup></code>
36+
* `num` only consists of digits and does not contain any leading zeros.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g1901_2000.s1904_the_number_of_full_rounds_you_have_played
2+
3+
// #Medium #String #Math #2023_06_19_Time_149_ms_(100.00%)_Space_33.4_MB_(100.00%)
4+
5+
class Solution {
6+
fun numberOfRounds(loginTime: String, logoutTime: String): Int {
7+
var loginSerializeTime = serializeTime(loginTime)
8+
var logoutSerializeTime = serializeTime(logoutTime)
9+
if (logoutSerializeTime - 14 < loginSerializeTime &&
10+
logoutSerializeTime > loginSerializeTime
11+
) {
12+
return 0
13+
}
14+
loginSerializeTime = maskSerializeTime(loginSerializeTime, 14)
15+
logoutSerializeTime = maskSerializeTime(logoutSerializeTime, 0)
16+
if (loginSerializeTime == logoutSerializeTime) {
17+
return 0
18+
}
19+
return if (loginSerializeTime > logoutSerializeTime + 14) {
20+
(
21+
calculateFullRounds(loginSerializeTime, MID_NIGHT_END) +
22+
calculateFullRounds(MID_NIGHT_START, logoutSerializeTime)
23+
)
24+
} else calculateFullRounds(loginSerializeTime, logoutSerializeTime)
25+
}
26+
27+
private fun maskSerializeTime(serializeTime: Int, mask: Int): Int {
28+
return (serializeTime + mask) / ROUND_INTERVAL * ROUND_INTERVAL
29+
}
30+
31+
private fun serializeTime(time: String): Int {
32+
return time.substring(0, 2).toInt() * 60 + time.substring(3, 5).toInt()
33+
}
34+
35+
private fun calculateFullRounds(login: Int, logout: Int): Int {
36+
return (logout - login) / ROUND_INTERVAL
37+
}
38+
39+
companion object {
40+
private const val MID_NIGHT_END = 1440
41+
private const val MID_NIGHT_START = 0
42+
private const val ROUND_INTERVAL = 15
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
1904\. The Number of Full Rounds You Have Played
2+
3+
Medium
4+
5+
You are participating in an online chess tournament. There is a chess round that starts every `15` minutes. The first round of the day starts at `00:00`, and after every `15` minutes, a new round starts.
6+
7+
* For example, the second round starts at `00:15`, the fourth round starts at `00:45`, and the seventh round starts at `01:30`.
8+
9+
You are given two strings `loginTime` and `logoutTime` where:
10+
11+
* `loginTime` is the time you will login to the game, and
12+
* `logoutTime` is the time you will logout from the game.
13+
14+
If `logoutTime` is **earlier** than `loginTime`, this means you have played from `loginTime` to midnight and from midnight to `logoutTime`.
15+
16+
Return _the number of full chess rounds you have played in the tournament_.
17+
18+
**Note:** All the given times follow the 24-hour clock. That means the first round of the day starts at `00:00` and the last round of the day starts at `23:45`.
19+
20+
**Example 1:**
21+
22+
**Input:** loginTime = "09:31", logoutTime = "10:14"
23+
24+
**Output:** 1
25+
26+
**Explanation:**
27+
28+
You played one full round from 09:45 to 10:00. You did not play the full round from 09:30 to 09:45 because you logged in at 09:31 after it began.
29+
30+
You did not play the full round from 10:00 to 10:15 because you logged out at 10:14 before it ended.
31+
32+
**Example 2:**
33+
34+
**Input:** loginTime = "21:30", logoutTime = "03:00"
35+
36+
**Output:** 22
37+
38+
**Explanation:** You played 10 full rounds from 21:30 to 00:00 and 12 full rounds from 00:00 to 03:00. 10 + 12 = 22.
39+
40+
**Constraints:**
41+
42+
* `loginTime` and `logoutTime` are in the format `hh:mm`.
43+
* `00 <= hh <= 23`
44+
* `00 <= mm <= 59`
45+
* `loginTime` and `logoutTime` are not equal.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g1901_2000.s1905_count_sub_islands
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
4+
// #Graph_Theory_I_Day_3_Matrix_Related_Problems
5+
// #2023_06_19_Time_866_ms_(100.00%)_Space_81.8_MB_(100.00%)
6+
7+
class Solution {
8+
private var ans = 0
9+
fun countSubIslands(grid1: Array<IntArray>, grid2: Array<IntArray>): Int {
10+
var count = 0
11+
for (i in grid2.indices) {
12+
for (j in grid2[0].indices) {
13+
if (grid2[i][j] == 1) {
14+
ans = 1
15+
dfs(grid1, grid2, i, j)
16+
count += ans
17+
}
18+
}
19+
}
20+
return count
21+
}
22+
23+
private fun dfs(grid1: Array<IntArray>, grid2: Array<IntArray>, i: Int, j: Int) {
24+
if (i < 0 || j < 0 || i >= grid1.size || j >= grid1[0].size || grid2[i][j] == 0) {
25+
return
26+
}
27+
if (grid1[i][j] == 0) {
28+
ans = 0
29+
}
30+
grid2[i][j] = 0
31+
dfs(grid1, grid2, i - 1, j)
32+
dfs(grid1, grid2, i + 1, j)
33+
dfs(grid1, grid2, i, j + 1)
34+
dfs(grid1, grid2, i, j - 1)
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1905\. Count Sub Islands
2+
3+
Medium
4+
5+
You are given two `m x n` binary matrices `grid1` and `grid2` containing only `0`'s (representing water) and `1`'s (representing land). An **island** is a group of `1`'s connected **4-directionally** (horizontal or vertical). Any cells outside of the grid are considered water cells.
6+
7+
An island in `grid2` is considered a **sub-island** if there is an island in `grid1` that contains **all** the cells that make up **this** island in `grid2`.
8+
9+
Return the _**number** of islands in_ `grid2` _that are considered **sub-islands**_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/06/10/test1.png)
14+
15+
**Input:** grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
16+
17+
**Output:** 3
18+
19+
**Explanation:** In the picture above, the grid on the left is grid1 and the grid on the right is grid2. The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/06/03/testcasex2.png)
24+
25+
**Input:** grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
26+
27+
**Output:** 2
28+
29+
**Explanation:** In the picture above, the grid on the left is grid1 and the grid on the right is grid2. The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
30+
31+
**Constraints:**
32+
33+
* `m == grid1.length == grid2.length`
34+
* `n == grid1[i].length == grid2[i].length`
35+
* `1 <= m, n <= 500`
36+
* `grid1[i][j]` and `grid2[i][j]` are either `0` or `1`.

0 commit comments

Comments
 (0)