Skip to content

Commit 8636dc7

Browse files
authored
Added tasks 838, 839, 840, 841
1 parent b9dd866 commit 8636dc7

File tree

13 files changed

+429
-0
lines changed

13 files changed

+429
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
9191
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9292
|-|-|-|-|-|-
9393
| 0797 |[All Paths From Source to Target](src/main/kotlin/g0701_0800/s0797_all_paths_from_source_to_target/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Backtracking | 232 | 100.00
94+
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 189 | 69.23
9495

9596
#### Day 8 Standard Traversal
9697

@@ -891,6 +892,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
891892

892893
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
893894
|-|-|-|-|-|-
895+
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 189 | 69.23
894896

895897
#### Day 20 Heap Priority Queue
896898

@@ -1713,6 +1715,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17131715
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17141716
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
17151717
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
1718+
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Data_Structure_II_Day_19_Graph, Graph_Theory_I_Day_7_Standard_Traversal | 189 | 69.23
1719+
| 0840 |[Magic Squares In Grid](src/main/kotlin/g0801_0900/s0840_magic_squares_in_grid/Solution.kt)| Medium | Array, Math, Matrix | 149 | 100.00
1720+
| 0839 |[Similar String Groups](src/main/kotlin/g0801_0900/s0839_similar_string_groups/Solution.kt)| Hard | Array, String, Depth_First_Search, Breadth_First_Search, Union_Find | 205 | 100.00
1721+
| 0838 |[Push Dominoes](src/main/kotlin/g0801_0900/s0838_push_dominoes/Solution.kt)| Medium | String, Dynamic_Programming, Two_Pointers | 270 | 100.00
17161722
| 0837 |[New 21 Game](src/main/kotlin/g0801_0900/s0837_new_21_game/Solution.kt)| Medium | Dynamic_Programming, Math, Sliding_Window, Probability_and_Statistics | 144 | 75.00
17171723
| 0836 |[Rectangle Overlap](src/main/kotlin/g0801_0900/s0836_rectangle_overlap/Solution.kt)| Easy | Math, Geometry | 121 | 100.00
17181724
| 0835 |[Image Overlap](src/main/kotlin/g0801_0900/s0835_image_overlap/Solution.kt)| Medium | Array, Matrix | 163 | 100.00
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g0801_0900.s0838_push_dominoes
2+
3+
// #Medium #String #Dynamic_Programming #Two_Pointers
4+
// #2023_03_28_Time_270_ms_(100.00%)_Space_39.5_MB_(100.00%)
5+
6+
class Solution {
7+
fun pushDominoes(dominoes: String): String {
8+
val ch = CharArray(dominoes.length + 2)
9+
ch[0] = 'L'
10+
ch[ch.size - 1] = 'R'
11+
for (k in 1 until ch.size - 1) {
12+
ch[k] = dominoes[k - 1]
13+
}
14+
var i = 0
15+
var j = 1
16+
while (j < ch.size) {
17+
while (ch[j] == '.') {
18+
j++
19+
}
20+
if (ch[i] == 'L' && ch[j] == 'L') {
21+
while (i != j) {
22+
ch[i] = 'L'
23+
i++
24+
}
25+
j++
26+
} else if (ch[i] == 'R' && ch[j] == 'R') {
27+
while (i != j) {
28+
ch[i] = 'R'
29+
i++
30+
}
31+
j++
32+
} else if (ch[i] == 'L' && ch[j] == 'R') {
33+
i = j
34+
j++
35+
} else if (ch[i] == 'R' && ch[j] == 'L') {
36+
var rTemp = i + 1
37+
var lTemp = j - 1
38+
while (rTemp < lTemp) {
39+
ch[rTemp] = 'R'
40+
ch[lTemp] = 'L'
41+
rTemp++
42+
lTemp--
43+
}
44+
i = j
45+
j++
46+
}
47+
}
48+
val sb = StringBuilder()
49+
for (k in 1 until ch.size - 1) {
50+
sb.append(ch[k])
51+
}
52+
return sb.toString()
53+
}
54+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
838\. Push Dominoes
2+
3+
Medium
4+
5+
There are `n` dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right.
6+
7+
After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
8+
9+
When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
10+
11+
For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.
12+
13+
You are given a string `dominoes` representing the initial state where:
14+
15+
* `dominoes[i] = 'L'`, if the <code>i<sup>th</sup></code> domino has been pushed to the left,
16+
* `dominoes[i] = 'R'`, if the <code>i<sup>th</sup></code> domino has been pushed to the right, and
17+
* `dominoes[i] = '.'`, if the <code>i<sup>th</sup></code> domino has not been pushed.
18+
19+
Return _a string representing the final state_.
20+
21+
**Example 1:**
22+
23+
**Input:** dominoes = "RR.L"
24+
25+
**Output:** "RR.L"
26+
27+
**Explanation:** The first domino expends no additional force on the second domino.
28+
29+
**Example 2:**
30+
31+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/05/18/domino.png)
32+
33+
**Input:** dominoes = ".L.R...LR..L.."
34+
35+
**Output:** "LL.RR.LLRRLL.."
36+
37+
**Constraints:**
38+
39+
* `n == dominoes.length`
40+
* <code>1 <= n <= 10<sup>5</sup></code>
41+
* `dominoes[i]` is either `'L'`, `'R'`, or `'.'`.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package g0801_0900.s0839_similar_string_groups
2+
3+
// #Hard #Array #String #Depth_First_Search #Breadth_First_Search #Union_Find
4+
// #2023_03_28_Time_205_ms_(100.00%)_Space_35.9_MB_(100.00%)
5+
6+
import java.util.LinkedList
7+
import java.util.Queue
8+
9+
class Solution {
10+
fun numSimilarGroups(strs: Array<String>): Int {
11+
val visited = BooleanArray(strs.size)
12+
var res = 0
13+
for (i in strs.indices) {
14+
if (!visited[i]) {
15+
res++
16+
bfs(i, visited, strs)
17+
}
18+
}
19+
return res
20+
}
21+
22+
private fun bfs(i: Int, visited: BooleanArray, strs: Array<String>) {
23+
val qu: Queue<String> = LinkedList()
24+
qu.add(strs[i])
25+
visited[i] = true
26+
while (!qu.isEmpty()) {
27+
val s = qu.poll()
28+
for (j in strs.indices) {
29+
if (visited[j]) {
30+
continue
31+
}
32+
if (isSimilar(s, strs[j])) {
33+
visited[j] = true
34+
qu.add(strs[j])
35+
}
36+
}
37+
}
38+
}
39+
40+
private fun isSimilar(s1: String, s2: String): Boolean {
41+
var c1: Char? = null
42+
var c2: Char? = null
43+
var mismatchCount = 0
44+
for (i in s1.indices) {
45+
if (s1[i] == s2[i]) {
46+
continue
47+
}
48+
mismatchCount++
49+
if (c1 == null) {
50+
c1 = s1[i]
51+
c2 = s2[i]
52+
} else if (s2[i] != c1 || s1[i] != c2) {
53+
return false
54+
} else if (mismatchCount > 2) {
55+
return false
56+
}
57+
}
58+
return true
59+
}
60+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
839\. Similar String Groups
2+
3+
Hard
4+
5+
Two strings `X` and `Y` are similar if we can swap two letters (in different positions) of `X`, so that it equals `Y`. Also two strings `X` and `Y` are similar if they are equal.
6+
7+
For example, `"tars"` and `"rats"` are similar (swapping at positions `0` and `2`), and `"rats"` and `"arts"` are similar, but `"star"` is not similar to `"tars"`, `"rats"`, or `"arts"`.
8+
9+
Together, these form two connected groups by similarity: `{"tars", "rats", "arts"}` and `{"star"}`. Notice that `"tars"` and `"arts"` are in the same group even though they are not similar. Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.
10+
11+
We are given a list `strs` of strings where every string in `strs` is an anagram of every other string in `strs`. How many groups are there?
12+
13+
**Example 1:**
14+
15+
**Input:** strs = ["tars","rats","arts","star"]
16+
17+
**Output:** 2
18+
19+
**Example 2:**
20+
21+
**Input:** strs = ["omv","ovm"]
22+
23+
**Output:** 1
24+
25+
**Constraints:**
26+
27+
* `1 <= strs.length <= 300`
28+
* `1 <= strs[i].length <= 300`
29+
* `strs[i]` consists of lowercase letters only.
30+
* All words in `strs` have the same length and are anagrams of each other.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0801_0900.s0840_magic_squares_in_grid
2+
3+
// #Medium #Array #Math #Matrix #2023_03_28_Time_149_ms_(100.00%)_Space_34.2_MB_(100.00%)
4+
5+
class Solution {
6+
fun numMagicSquaresInside(grid: Array<IntArray>): Int {
7+
val m = grid.size
8+
val n = grid[0].size
9+
var count = 0
10+
for (i in 0 until m - 2) {
11+
for (j in 0 until n - 2) {
12+
val set: MutableSet<Int> = HashSet()
13+
val sum = grid[i][j] + grid[i][j + 1] + grid[i][j + 2]
14+
if (sum == grid[i + 1][j] + grid[i + 1][j + 1] + grid[i + 1][j + 2] && sum == grid[i + 2][j] +
15+
grid[i + 2][j + 1] + grid[i + 2][j + 2] && sum == grid[i][j] + grid[i + 1][j] + grid[i + 2][j] &&
16+
sum == grid[i][j + 1] + grid[i + 1][j + 1] + grid[i + 2][j + 1] && sum == grid[i][j + 2] +
17+
grid[i + 1][j + 2] + grid[i + 2][j + 2] && sum == grid[i][j] + grid[i + 1][j + 1] +
18+
grid[i + 2][j + 2] && sum == grid[i][j + 2] + grid[i + 1][j + 1] + grid[i + 2][j] && set.add(
19+
grid[i][j]
20+
) &&
21+
isLegit(grid[i][j]) &&
22+
set.add(grid[i][j + 1]) &&
23+
isLegit(grid[i][j + 1]) &&
24+
set.add(grid[i][j + 2]) &&
25+
isLegit(grid[i][j + 2]) &&
26+
set.add(grid[i + 1][j]) &&
27+
isLegit(grid[i + 1][j]) &&
28+
set.add(grid[i + 1][j + 1]) &&
29+
isLegit(grid[i + 1][j + 1]) &&
30+
set.add(grid[i + 1][j + 2]) &&
31+
isLegit(grid[i + 1][j + 2]) &&
32+
set.add(grid[i + 2][j]) &&
33+
isLegit(grid[i + 2][j]) &&
34+
set.add(grid[i + 2][j + 1]) &&
35+
isLegit(grid[i + 2][j + 1]) &&
36+
set.add(grid[i + 2][j + 2]) &&
37+
isLegit(grid[i + 2][j + 2])
38+
) {
39+
count++
40+
}
41+
}
42+
}
43+
return count
44+
}
45+
46+
private fun isLegit(num: Int): Boolean {
47+
return num in 1..9
48+
}
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
840\. Magic Squares In Grid
2+
3+
Medium
4+
5+
A `3 x 3` magic square is a `3 x 3` grid filled with distinct numbers **from** `1` **to** `9` such that each row, column, and both diagonals all have the same sum.
6+
7+
Given a `row x col` `grid` of integers, how many `3 x 3` "magic square" subgrids are there? (Each subgrid is contiguous).
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/09/11/magic_main.jpg)
12+
13+
**Input:** grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
14+
15+
**Output:** 1
16+
17+
**Explanation:** The following subgrid is a 3 x 3 magic square: ![](https://assets.leetcode.com/uploads/2020/09/11/magic_valid.jpg) while this one is not: ![](https://assets.leetcode.com/uploads/2020/09/11/magic_invalid.jpg) In total, there is only one magic square inside the given grid.
18+
19+
**Example 2:**
20+
21+
**Input:** grid = [[8]]
22+
23+
**Output:** 0
24+
25+
**Constraints:**
26+
27+
* `row == grid.length`
28+
* `col == grid[i].length`
29+
* `1 <= row, col <= 10`
30+
* `0 <= grid[i][j] <= 15`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0801_0900.s0841_keys_and_rooms
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Data_Structure_II_Day_19_Graph
4+
// #Graph_Theory_I_Day_7_Standard_Traversal #2023_03_28_Time_189_ms_(69.23%)_Space_35.5_MB_(97.44%)
5+
6+
import java.util.TreeSet
7+
8+
class Solution {
9+
fun canVisitAllRooms(rooms: List<List<Int>?>): Boolean {
10+
val visited: MutableSet<Int> = HashSet()
11+
visited.add(0)
12+
val treeSet = TreeSet(rooms[0])
13+
while (!treeSet.isEmpty()) {
14+
val key = treeSet.pollFirst()
15+
if (!visited.add(key)) {
16+
continue
17+
}
18+
if (visited.size == rooms.size) {
19+
return true
20+
}
21+
treeSet.addAll(rooms[key]!!)
22+
}
23+
return visited.size == rooms.size
24+
}
25+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
841\. Keys and Rooms
2+
3+
Medium
4+
5+
There are `n` rooms labeled from `0` to `n - 1` and all the rooms are locked except for room `0`. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.
6+
7+
When you visit a room, you may find a set of **distinct keys** in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.
8+
9+
Given an array `rooms` where `rooms[i]` is the set of keys that you can obtain if you visited room `i`, return `true` _if you can visit **all** the rooms, or_ `false` _otherwise_.
10+
11+
**Example 1:**
12+
13+
**Input:** rooms = [[1],[2],[3],[]]
14+
15+
**Output:** true
16+
17+
**Explanation:**
18+
19+
We visit room 0 and pick up key 1.
20+
21+
We then visit room 1 and pick up key 2.
22+
23+
We then visit room 2 and pick up key 3.
24+
25+
We then visit room 3.
26+
27+
Since we were able to visit every room, we return true.
28+
29+
**Example 2:**
30+
31+
**Input:** rooms = [[1,3],[3,0,1],[2],[0]]
32+
33+
**Output:** false
34+
35+
**Explanation:** We can not enter room number 2 since the only key that unlocks it is in that room.
36+
37+
**Constraints:**
38+
39+
* `n == rooms.length`
40+
* `2 <= n <= 1000`
41+
* `0 <= rooms[i].length <= 1000`
42+
* `1 <= sum(rooms[i].length) <= 3000`
43+
* `0 <= rooms[i][j] < n`
44+
* All the values of `rooms[i]` are **unique**.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0801_0900.s0838_push_dominoes
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun pushDominoes() {
10+
assertThat(Solution().pushDominoes("RR.L"), equalTo("RR.L"))
11+
}
12+
13+
@Test
14+
fun pushDominoes2() {
15+
assertThat(Solution().pushDominoes(".L.R...LR..L.."), equalTo("LL.RR.LLRRLL.."))
16+
}
17+
}

0 commit comments

Comments
 (0)