Skip to content

Commit c4ae896

Browse files
authored
Added tasks 802, 803, 804, 805
1 parent 9040433 commit c4ae896

File tree

13 files changed

+469
-0
lines changed

13 files changed

+469
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
102102

103103
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
104104
|-|-|-|-|-|-
105+
| 0802 |[Find Eventual Safe States](src/main/kotlin/g0801_0900/s0802_find_eventual_safe_states/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 511 | 100.00
105106

106107
#### Day 10 Standard Traversal
107108

@@ -1710,6 +1711,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17101711
| 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
17111712
| 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
17121713
| 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
1714+
| 0805 |[Split Array With Same Average](src/main/kotlin/g0801_0900/s0805_split_array_with_same_average/Solution.kt)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask | 142 | 100.00
1715+
| 0804 |[Unique Morse Code Words](src/main/kotlin/g0801_0900/s0804_unique_morse_code_words/Solution.kt)| Easy | Array, String, Hash_Table | 158 | 80.00
1716+
| 0803 |[Bricks Falling When Hit](src/main/kotlin/g0801_0900/s0803_bricks_falling_when_hit/Solution.kt)| Hard | Array, Matrix, Union_Find | 742 | 100.00
1717+
| 0802 |[Find Eventual Safe States](src/main/kotlin/g0801_0900/s0802_find_eventual_safe_states/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Graph_Theory_I_Day_9_Standard_Traversal | 511 | 100.00
17131718
| 0801 |[Minimum Swaps To Make Sequences Increasing](src/main/kotlin/g0801_0900/s0801_minimum_swaps_to_make_sequences_increasing/Solution.kt)| Hard | Array, Dynamic_Programming | 617 | 83.33
17141719
| 0799 |[Champagne Tower](src/main/kotlin/g0701_0800/s0799_champagne_tower/Solution.kt)| Medium | Dynamic_Programming | 153 | 100.00
17151720
| 0798 |[Smallest Rotation with Highest Score](src/main/kotlin/g0701_0800/s0798_smallest_rotation_with_highest_score/Solution.kt)| Hard | Array, Prefix_Sum | 470 | 100.00
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0801_0900.s0802_find_eventual_safe_states
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Topological_Sort
4+
// #Graph_Theory_I_Day_9_Standard_Traversal
5+
// #2023_03_16_Time_511_ms_(100.00%)_Space_65.1_MB_(58.33%)
6+
7+
class Solution {
8+
fun eventualSafeNodes(graph: Array<IntArray>): List<Int> {
9+
val res: MutableList<Int> = ArrayList()
10+
val vis = IntArray(graph.size)
11+
for (i in graph.indices) {
12+
if (dfs(graph, i, vis)) {
13+
res.add(i)
14+
}
15+
}
16+
return res
17+
}
18+
19+
private fun dfs(graph: Array<IntArray>, src: Int, vis: IntArray): Boolean {
20+
if (vis[src] != 0) {
21+
return vis[src] == 2
22+
}
23+
vis[src] = 1
24+
for (x in graph[src]) {
25+
if (!dfs(graph, x, vis)) {
26+
return false
27+
}
28+
}
29+
vis[src] = 2
30+
return true
31+
}
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
802\. Find Eventual Safe States
2+
3+
Medium
4+
5+
There is a directed graph of `n` nodes with each node labeled from `0` to `n - 1`. The graph is represented by a **0-indexed** 2D integer array `graph` where `graph[i]` is an integer array of nodes adjacent to node `i`, meaning there is an edge from node `i` to each node in `graph[i]`.
6+
7+
A node is a **terminal node** if there are no outgoing edges. A node is a **safe node** if every possible path starting from that node leads to a **terminal node** (or another safe node).
8+
9+
Return _an array containing all the **safe nodes** of the graph_. The answer should be sorted in **ascending** order.
10+
11+
**Example 1:**
12+
13+
![Illustration of graph](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/17/picture1.png)
14+
15+
**Input:** graph = [[1,2],[2,3],[5],[0],[5],[],[]]
16+
17+
**Output:** [2,4,5,6]
18+
19+
**Explanation:** The given graph is shown above. Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them. Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.
20+
21+
**Example 2:**
22+
23+
**Input:** graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
24+
25+
**Output:** [4]
26+
27+
**Explanation:** Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
28+
29+
**Constraints:**
30+
31+
* `n == graph.length`
32+
* <code>1 <= n <= 10<sup>4</sup></code>
33+
* `0 <= graph[i].length <= n`
34+
* `0 <= graph[i][j] <= n - 1`
35+
* `graph[i]` is sorted in a strictly increasing order.
36+
* The graph may contain self-loops.
37+
* The number of edges in the graph will be in the range <code>[1, 4 * 10<sup>4</sup>]</code>.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g0801_0900.s0803_bricks_falling_when_hit
2+
3+
// #Hard #Array #Matrix #Union_Find #2023_03_16_Time_742_ms_(100.00%)_Space_53.4_MB_(100.00%)
4+
5+
class Solution {
6+
private val dirs = arrayOf(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, 1), intArrayOf(0, -1))
7+
fun hitBricks(grid: Array<IntArray>, hits: Array<IntArray>): IntArray {
8+
val cols = grid[0].size
9+
for (hit in hits) {
10+
val x = hit[0]
11+
val y = hit[1]
12+
grid[x][y]--
13+
}
14+
val row = 0
15+
for (col in 0 until cols) {
16+
dfs(row, col, grid)
17+
}
18+
val res = IntArray(hits.size)
19+
for (i in hits.indices.reversed()) {
20+
val x = hits[i][0]
21+
val y = hits[i][1]
22+
grid[x][y]++
23+
if (grid[x][y] == 1 && isConnectedToRoof(x, y, grid)) {
24+
res[i] = dfs(x, y, grid) - 1
25+
}
26+
}
27+
return res
28+
}
29+
30+
private fun dfs(i: Int, j: Int, grid: Array<IntArray>): Int {
31+
if (i < 0 || i >= grid.size || j < 0 || j >= grid[0].size || grid[i][j] != 1) {
32+
return 0
33+
}
34+
grid[i][j] = 2
35+
return (
36+
dfs(i + 1, j, grid) +
37+
dfs(i - 1, j, grid) +
38+
dfs(i, j + 1, grid) +
39+
dfs(i, j - 1, grid) +
40+
1
41+
)
42+
}
43+
44+
private fun isConnectedToRoof(i: Int, j: Int, grid: Array<IntArray>): Boolean {
45+
if (i == 0) {
46+
return true
47+
}
48+
for (d in dirs) {
49+
val x = i + d[0]
50+
val y = j + d[1]
51+
if (x >= 0 && x < grid.size && y >= 0 && y < grid[0].size && grid[x][y] == 2) {
52+
return true
53+
}
54+
}
55+
return false
56+
}
57+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
803\. Bricks Falling When Hit
2+
3+
Hard
4+
5+
You are given an `m x n` binary `grid`, where each `1` represents a brick and `0` represents an empty space. A brick is **stable** if:
6+
7+
* It is directly connected to the top of the grid, or
8+
* At least one other brick in its four adjacent cells is **stable**.
9+
10+
You are also given an array `hits`, which is a sequence of erasures we want to apply. Each time we want to erase the brick at the location <code>hits[i] = (row<sub>i</sub>, col<sub>i</sub>)</code>. The brick on that location (if it exists) will disappear. Some other bricks may no longer be stable because of that erasure and will **fall**. Once a brick falls, it is **immediately** erased from the `grid` (i.e., it does not land on other stable bricks).
11+
12+
Return _an array_ `result`_, where each_ `result[i]` _is the number of bricks that will **fall** after the_ <code>i<sup>th</sup></code> _erasure is applied._
13+
14+
**Note** that an erasure may refer to a location with no brick, and if it does, no bricks drop.
15+
16+
**Example 1:**
17+
18+
**Input:** grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,0]]
19+
20+
**Output:** [2]
21+
22+
**Explanation:** Starting with the grid:
23+
24+
[[1,0,0,0], [1,1,1,0]]
25+
26+
We erase the underlined brick at (1,0), resulting in the grid:
27+
28+
[[1,0,0,0], [0,1,1,0]]
29+
30+
The two underlined bricks are no longer stable as they are no longer connected to the top nor adjacent to another stable brick, so they will fall. The resulting grid is:
31+
32+
[[1,0,0,0], [0,0,0,0]]
33+
34+
Hence the result is [2].
35+
36+
**Example 2:**
37+
38+
**Input:** grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]
39+
40+
**Output:** [0,0]
41+
42+
**Explanation:** Starting with the grid:
43+
44+
[[1,0,0,0], [1,1,0,0]]
45+
46+
We erase the underlined brick at (1,1), resulting in the grid:
47+
48+
[[1,0,0,0], [1,0,0,0]]
49+
50+
All remaining bricks are still stable, so no bricks fall. The grid remains the same:
51+
52+
[[1,0,0,0], [1,0,0,0]]
53+
54+
Next, we erase the underlined brick at (1,0), resulting in the grid:
55+
56+
[[1,0,0,0], [0,0,0,0]]
57+
58+
Once again, all remaining bricks are still stable, so no bricks fall.
59+
60+
Hence the result is [0,0].
61+
62+
**Constraints:**
63+
64+
* `m == grid.length`
65+
* `n == grid[i].length`
66+
* `1 <= m, n <= 200`
67+
* `grid[i][j]` is `0` or `1`.
68+
* <code>1 <= hits.length <= 4 * 10<sup>4</sup></code>
69+
* `hits[i].length == 2`
70+
* <code>0 <= x<sub>i </sub><= m - 1</code>
71+
* <code>0 <= y<sub>i</sub> <= n - 1</code>
72+
* All <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are unique.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0801_0900.s0804_unique_morse_code_words
2+
3+
// #Easy #Array #String #Hash_Table #2023_03_16_Time_158_ms_(80.00%)_Space_34.9_MB_(65.71%)
4+
5+
class Solution {
6+
fun uniqueMorseRepresentations(words: Array<String>): Int {
7+
val morse = arrayOf(
8+
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
9+
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
10+
"-.--", "--.."
11+
)
12+
val set: MutableSet<String> = HashSet()
13+
for (word in words) {
14+
val temp = StringBuilder()
15+
for (c in word.toCharArray()) {
16+
temp.append(morse[c.code - 'a'.code])
17+
}
18+
set.add(temp.toString())
19+
}
20+
return set.size
21+
}
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
804\. Unique Morse Code Words
2+
3+
Easy
4+
5+
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
6+
7+
* `'a'` maps to `".-"`,
8+
* `'b'` maps to `"-..."`,
9+
* `'c'` maps to `"-.-."`, and so on.
10+
11+
For convenience, the full table for the `26` letters of the English alphabet is given below:
12+
13+
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
14+
15+
Given an array of strings `words` where each word can be written as a concatenation of the Morse code of each letter.
16+
17+
* For example, `"cab"` can be written as `"-.-..--..."`, which is the concatenation of `"-.-."`, `".-"`, and `"-..."`. We will call such a concatenation the **transformation** of a word.
18+
19+
Return _the number of different **transformations** among all words we have_.
20+
21+
**Example 1:**
22+
23+
**Input:** words = ["gin","zen","gig","msg"]
24+
25+
**Output:** 2
26+
27+
**Explanation:** The transformation of each word is:
28+
29+
"gin" -> "--...-."
30+
31+
"zen" -> "--...-."
32+
33+
"gig" -> "--...--."
34+
35+
"msg" -> "--...--."
36+
37+
There are 2 different transformations: "--...-." and "--...--.".
38+
39+
**Example 2:**
40+
41+
**Input:** words = ["a"]
42+
43+
**Output:** 1
44+
45+
**Constraints:**
46+
47+
* `1 <= words.length <= 100`
48+
* `1 <= words[i].length <= 12`
49+
* `words[i]` consists of lowercase English letters.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0801_0900.s0805_split_array_with_same_average
2+
3+
// #Hard #Array #Dynamic_Programming #Math #Bit_Manipulation #Bitmask
4+
// #2023_03_16_Time_142_ms_(100.00%)_Space_33.7_MB_(100.00%)
5+
6+
import java.util.Arrays
7+
8+
@Suppress("NAME_SHADOWING")
9+
class Solution {
10+
private lateinit var nums: IntArray
11+
private lateinit var sums: IntArray
12+
fun splitArraySameAverage(nums: IntArray): Boolean {
13+
val len = nums.size
14+
if (len == 1) {
15+
return false
16+
}
17+
Arrays.sort(nums)
18+
sums = IntArray(len + 1)
19+
for (i in 0 until len) {
20+
sums[i + 1] = sums[i] + nums[i]
21+
}
22+
val sum = sums[len]
23+
this.nums = nums
24+
var i = 1
25+
val stop = len / 2
26+
while (i <= stop) {
27+
if (sum * i % len == 0 && findSum(i, len, sum * i / len)) {
28+
return true
29+
}
30+
i++
31+
}
32+
return false
33+
}
34+
35+
private fun findSum(k: Int, pos: Int, target: Int): Boolean {
36+
var pos = pos
37+
if (k == 1) {
38+
while (true) {
39+
if (nums[--pos] <= target) {
40+
break
41+
}
42+
}
43+
return nums[pos] == target
44+
}
45+
var i = pos
46+
while (sums[i] - sums[i-- - k] >= target) {
47+
if (sums[k - 1] <= target - nums[i] && findSum(k - 1, i, target - nums[i])) {
48+
return true
49+
}
50+
}
51+
return false
52+
}
53+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
805\. Split Array With Same Average
2+
3+
Hard
4+
5+
You are given an integer array `nums`.
6+
7+
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
8+
9+
Return `true` if it is possible to achieve that and `false` otherwise.
10+
11+
**Note** that for an array `arr`, `average(arr)` is the sum of all the elements of `arr` over the length of `arr`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3,4,5,6,7,8]
16+
17+
**Output:** true
18+
19+
**Explanation:** We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have an average of 4.5.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [3,1]
24+
25+
**Output:** false
26+
27+
**Constraints:**
28+
29+
* `1 <= nums.length <= 30`
30+
* <code>0 <= nums[i] <= 10<sup>4</sup></code>

0 commit comments

Comments
 (0)