Skip to content

Commit f3f00a5

Browse files
authored
Added tasks 928, 929, 930, 931
1 parent df5882d commit f3f00a5

File tree

13 files changed

+437
-0
lines changed

13 files changed

+437
-0
lines changed

README.md

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

13661366
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13671367
|-|-|-|-|-|-
1368+
| 0931 |[Minimum Falling Path Sum](src/main/kotlin/g0901_1000/s0931_minimum_falling_path_sum/Solution.kt)| Medium | Array, Dynamic_Programming, Matrix | 201 | 84.21
13681369
| 0120 |[Triangle](src/main/kotlin/g0101_0200/s0120_triangle/Solution.kt)| Medium | Array, Dynamic_Programming | 194 | 97.87
13691370

13701371
#### Day 14
@@ -1730,6 +1731,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17301731
|------|----------------|-------------|-------------|----------|---------
17311732
| 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
17321733
| 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
1734+
| 0931 |[Minimum Falling Path Sum](src/main/kotlin/g0901_1000/s0931_minimum_falling_path_sum/Solution.kt)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_13 | 201 | 84.21
1735+
| 0930 |[Binary Subarrays With Sum](src/main/kotlin/g0901_1000/s0930_binary_subarrays_with_sum/Solution.kt)| Medium | Array, Hash_Table, Prefix_Sum, Sliding_Window | 250 | 93.75
1736+
| 0929 |[Unique Email Addresses](src/main/kotlin/g0901_1000/s0929_unique_email_addresses/Solution.kt)| Easy | Array, String, Hash_Table | 207 | 89.29
1737+
| 0928 |[Minimize Malware Spread II](src/main/kotlin/g0901_1000/s0928_minimize_malware_spread_ii/Solution.kt)| Hard | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 716 | 100.00
17331738
| 0927 |[Three Equal Parts](src/main/kotlin/g0901_1000/s0927_three_equal_parts/Solution.kt)| Hard | Array, Math | 303 | 100.00
17341739
| 0926 |[Flip String to Monotone Increasing](src/main/kotlin/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.kt)| Medium | String, Dynamic_Programming | 199 | 100.00
17351740
| 0925 |[Long Pressed Name](src/main/kotlin/g0901_1000/s0925_long_pressed_name/Solution.kt)| Easy | String, Two_Pointers | 126 | 92.31
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g0901_1000.s0928_minimize_malware_spread_ii
2+
3+
// #Hard #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
4+
// #2023_04_26_Time_716_ms_(100.00%)_Space_63.5_MB_(100.00%)
5+
6+
import java.util.LinkedList
7+
import java.util.Queue
8+
9+
class Solution {
10+
private val adj: MutableMap<Int, ArrayList<Int>> = HashMap()
11+
private var visited: MutableSet<Int>? = null
12+
private var count = 0
13+
14+
private fun bfs(ind: Int, initial: IntArray) {
15+
val q: Queue<Int> = LinkedList()
16+
for (i in initial.indices) {
17+
if (i != ind) {
18+
q.add(initial[i])
19+
visited!!.add(initial[i])
20+
}
21+
}
22+
while (!q.isEmpty()) {
23+
val curr = q.poll()
24+
if (curr != initial[ind]) {
25+
count++
26+
}
27+
val children = adj[curr]
28+
if (children != null) {
29+
for (child in children) {
30+
if (!visited!!.contains(child)) {
31+
q.add(child)
32+
visited!!.add(child)
33+
}
34+
}
35+
}
36+
}
37+
}
38+
39+
fun minMalwareSpread(graph: Array<IntArray>, initial: IntArray): Int {
40+
val n = graph.size
41+
for (i in 0 until n) {
42+
adj.putIfAbsent(i, ArrayList())
43+
for (j in 0 until n) {
44+
if (graph[i][j] == 1) {
45+
adj[i]!!.add(j)
46+
}
47+
}
48+
}
49+
var min = n + 1
50+
initial.sort()
51+
var node = initial[0]
52+
for (i in initial.indices) {
53+
visited = HashSet()
54+
val children = adj[initial[i]]!!
55+
adj.remove(initial[i])
56+
bfs(i, initial)
57+
if (count < min) {
58+
min = count
59+
node = initial[i]
60+
}
61+
count = 0
62+
adj[initial[i]] = children
63+
}
64+
return node
65+
}
66+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
928\. Minimize Malware Spread II
2+
3+
Hard
4+
5+
You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the <code>i<sup>th</sup></code> node is directly connected to the <code>j<sup>th</sup></code> node if `graph[i][j] == 1`.
6+
7+
Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.
8+
9+
Suppose `M(initial)` is the final number of nodes infected with malware in the entire network after the spread of malware stops.
10+
11+
We will remove **exactly one node** from `initial`, **completely removing it and any connections from this node to any other node**.
12+
13+
Return the node that, if removed, would minimize `M(initial)`. If multiple nodes could be removed to minimize `M(initial)`, return such a node with **the smallest index**.
14+
15+
**Example 1:**
16+
17+
**Input:** graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
18+
19+
**Output:** 0
20+
21+
**Example 2:**
22+
23+
**Input:** graph = [[1,1,0],[1,1,1],[0,1,1]], initial = [0,1]
24+
25+
**Output:** 1
26+
27+
**Example 3:**
28+
29+
**Input:** graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]
30+
31+
**Output:** 1
32+
33+
**Constraints:**
34+
35+
* `n == graph.length`
36+
* `n == graph[i].length`
37+
* `2 <= n <= 300`
38+
* `graph[i][j]` is `0` or `1`.
39+
* `graph[i][j] == graph[j][i]`
40+
* `graph[i][i] == 1`
41+
* `1 <= initial.length < n`
42+
* `0 <= initial[i] <= n - 1`
43+
* All the integers in `initial` are **unique**.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0901_1000.s0929_unique_email_addresses
2+
3+
// #Easy #Array #String #Hash_Table #2023_04_26_Time_207_ms_(89.29%)_Space_37.1_MB_(82.14%)
4+
5+
class Solution {
6+
fun numUniqueEmails(emails: Array<String>): Int {
7+
val set: MutableSet<String> = HashSet()
8+
for (s in emails) {
9+
val sb = StringBuilder()
10+
var i = 0
11+
while (i < s.length) {
12+
val c = s[i]
13+
if (c == '+' || c == '@') {
14+
sb.append('@')
15+
i = s.indexOf("@") + 1
16+
sb.append(s.substring(i))
17+
break
18+
} else if (c != '.') {
19+
sb.append(c)
20+
}
21+
i++
22+
}
23+
set.add(sb.toString())
24+
}
25+
return set.size
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
929\. Unique Email Addresses
2+
3+
Easy
4+
5+
Every **valid email** consists of a **local name** and a **domain name**, separated by the `'@'` sign. Besides lowercase letters, the email may contain one or more `'.'` or `'+'`.
6+
7+
* For example, in `"[email protected]"`, `"alice"` is the **local name**, and `"leetcode.com"` is the **domain name**.
8+
9+
If you add periods `'.'` between some characters in the **local name** part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule **does not apply** to **domain names**.
10+
11+
* For example, `"[email protected]"` and `"[email protected]"` forward to the same email address.
12+
13+
If you add a plus `'+'` in the **local name**, everything after the first plus sign **will be ignored**. This allows certain emails to be filtered. Note that this rule **does not apply** to **domain names**.
14+
15+
* For example, `"[email protected]"` will be forwarded to `"[email protected]"`.
16+
17+
It is possible to use both of these rules at the same time.
18+
19+
Given an array of strings `emails` where we send one email to each `emails[i]`, return _the number of different addresses that actually receive mails_.
20+
21+
**Example 1:**
22+
23+
24+
25+
**Output:** 2
26+
27+
**Explanation:** "[email protected]" and "[email protected]" actually receive mails.
28+
29+
**Example 2:**
30+
31+
32+
33+
**Output:** 3
34+
35+
**Constraints:**
36+
37+
* `1 <= emails.length <= 100`
38+
* `1 <= emails[i].length <= 100`
39+
* `emails[i]` consist of lowercase English letters, `'+'`, `'.'` and `'@'`.
40+
* Each `emails[i]` contains exactly one `'@'` character.
41+
* All local and domain names are non-empty.
42+
* Local names do not start with a `'+'` character.
43+
* Domain names end with the `".com"` suffix.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0901_1000.s0930_binary_subarrays_with_sum
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #Sliding_Window
4+
// #2023_04_26_Time_250_ms_(93.75%)_Space_39.6_MB_(87.50%)
5+
6+
class Solution {
7+
fun numSubarraysWithSum(nums: IntArray, goal: Int): Int {
8+
return atmostK(nums, goal) - atmostK(nums, goal - 1)
9+
}
10+
11+
fun atmostK(arr: IntArray, k: Int): Int {
12+
var cnt = 0
13+
var i = 0
14+
var j = 0
15+
var sum = 0
16+
while (j < arr.size) {
17+
sum += arr[j]
18+
if (sum > k) {
19+
while (i <= j && sum > k) {
20+
sum -= arr[i]
21+
i++
22+
}
23+
}
24+
cnt += j - i + 1
25+
j++
26+
}
27+
return cnt
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
930\. Binary Subarrays With Sum
2+
3+
Medium
4+
5+
Given a binary array `nums` and an integer `goal`, return _the number of non-empty **subarrays** with a sum_ `goal`.
6+
7+
A **subarray** is a contiguous part of the array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,0,1,0,1], goal = 2
12+
13+
**Output:** 4
14+
15+
**Explanation:** The 4 subarrays are bolded and underlined below:
16+
17+
[<ins>**1,0,1**</ins>,0,1]
18+
19+
[<ins>**1,0,1,0**</ins>,1]
20+
21+
[1,<ins>**0,1,0,1**</ins>]
22+
23+
[1,0,<ins>**1,0,1**</ins>]
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [0,0,0,0,0], goal = 0
28+
29+
**Output:** 15
30+
31+
**Constraints:**
32+
33+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
34+
* `nums[i]` is either `0` or `1`.
35+
* `0 <= goal <= nums.length`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0901_1000.s0931_minimum_falling_path_sum
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix #Dynamic_Programming_I_Day_13
4+
// #2023_04_26_Time_201_ms_(84.21%)_Space_36.7_MB_(94.74%)
5+
6+
class Solution {
7+
fun minFallingPathSum(matrix: Array<IntArray>): Int {
8+
val l = matrix[0].size
9+
var arr = matrix[0]
10+
for (i in 1 until matrix.size) {
11+
val cur = IntArray(l)
12+
for (j in 0 until l) {
13+
var left = Int.MAX_VALUE
14+
var right = Int.MAX_VALUE
15+
val curCell = arr[j]
16+
if (j > 0) {
17+
left = arr[j - 1]
18+
}
19+
if (j < l - 1) {
20+
right = arr[j + 1]
21+
}
22+
cur[j] = matrix[i][j] + left.coerceAtMost(right.coerceAtMost(curCell))
23+
}
24+
arr = cur
25+
}
26+
var min = Int.MAX_VALUE
27+
for (i in 0 until l) {
28+
if (arr[i] < min) {
29+
min = arr[i]
30+
}
31+
}
32+
return min
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
931\. Minimum Falling Path Sum
2+
3+
Medium
4+
5+
Given an `n x n` array of integers `matrix`, return _the **minimum sum** of any **falling path** through_ `matrix`.
6+
7+
A **falling path** starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position `(row, col)` will be `(row + 1, col - 1)`, `(row + 1, col)`, or `(row + 1, col + 1)`.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/11/03/failing1-grid.jpg)
12+
13+
**Input:** matrix = [[2,1,3],[6,5,4],[7,8,9]]
14+
15+
**Output:** 13
16+
17+
**Explanation:** There are two falling paths with a minimum sum as shown.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/11/03/failing2-grid.jpg)
22+
23+
**Input:** matrix = [[-19,57],[-40,-5]]
24+
25+
**Output:** -59
26+
27+
**Explanation:** The falling path with a minimum sum is shown.
28+
29+
**Constraints:**
30+
31+
* `n == matrix.length == matrix[i].length`
32+
* `1 <= n <= 100`
33+
* `-100 <= matrix[i][j] <= 100`
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0901_1000.s0928_minimize_malware_spread_ii
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 minMalwareSpread() {
10+
assertThat(
11+
Solution()
12+
.minMalwareSpread(
13+
arrayOf(intArrayOf(1, 1, 0), intArrayOf(1, 1, 0), intArrayOf(0, 0, 1)),
14+
intArrayOf(0, 1)
15+
),
16+
equalTo(0)
17+
)
18+
}
19+
20+
@Test
21+
fun minMalwareSpread2() {
22+
assertThat(
23+
Solution()
24+
.minMalwareSpread(
25+
arrayOf(intArrayOf(1, 1, 0), intArrayOf(1, 1, 1), intArrayOf(0, 1, 1)),
26+
intArrayOf(0, 1)
27+
),
28+
equalTo(1)
29+
)
30+
}
31+
32+
@Test
33+
fun minMalwareSpread3() {
34+
assertThat(
35+
Solution()
36+
.minMalwareSpread(
37+
arrayOf(
38+
intArrayOf(1, 1, 0, 0),
39+
intArrayOf(1, 1, 1, 0),
40+
intArrayOf(0, 1, 1, 1),
41+
intArrayOf(0, 0, 1, 1)
42+
),
43+
intArrayOf(0, 1)
44+
),
45+
equalTo(1)
46+
)
47+
}
48+
}

0 commit comments

Comments
 (0)