Skip to content

Commit 127552a

Browse files
authored
Added tasks 924, 925, 926, 927
1 parent c3a0daf commit 127552a

File tree

13 files changed

+417
-0
lines changed

13 files changed

+417
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17301730
|------|----------------|-------------|-------------|----------|---------
17311731
| 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
17321732
| 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
1733+
| 0927 |[Three Equal Parts](src/main/kotlin/g0901_1000/s0927_three_equal_parts/Solution.kt)| Hard | Array, Math | 303 | 100.00
1734+
| 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
1735+
| 0925 |[Long Pressed Name](src/main/kotlin/g0901_1000/s0925_long_pressed_name/Solution.kt)| Easy | String, Two_Pointers | 138 | 76.19
1736+
| 0924 |[Minimize Malware Spread](src/main/kotlin/g0901_1000/s0924_minimize_malware_spread/Solution.kt)| Hard | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 791 | 100.00
17331737
| 0923 |[3Sum With Multiplicity](src/main/kotlin/g0901_1000/s0923_3sum_with_multiplicity/Solution.kt)| Medium | Array, Hash_Table, Sorting, Two_Pointers, Counting | 190 | 100.00
17341738
| 0922 |[Sort Array By Parity II](src/main/kotlin/g0901_1000/s0922_sort_array_by_parity_ii/Solution.kt)| Easy | Array, Sorting, Two_Pointers | 257 | 87.50
17351739
| 0921 |[Minimum Add to Make Parentheses Valid](src/main/kotlin/g0901_1000/s0921_minimum_add_to_make_parentheses_valid/Solution.kt)| Medium | String, Greedy, Stack | 131 | 92.59
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package g0901_1000.s0924_minimize_malware_spread
2+
3+
// #Hard #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
4+
// #2023_04_17_Time_791_ms_(100.00%)_Space_59.7_MB_(100.00%)
5+
6+
class Solution {
7+
private lateinit var size: IntArray
8+
private lateinit var par: IntArray
9+
fun minMalwareSpread(graph: Array<IntArray>, initial: IntArray): Int {
10+
size = IntArray(graph.size)
11+
par = IntArray(graph.size)
12+
for (i in graph.indices) {
13+
size[i] = 1
14+
par[i] = i
15+
}
16+
// create groups
17+
for (i in graph.indices) {
18+
for (j in graph[0].indices) {
19+
if (graph[i][j] == 1) {
20+
val p1 = find(i)
21+
val p2 = find(j)
22+
merge(p1, p2)
23+
}
24+
}
25+
}
26+
// no of infected in group
27+
val infected = IntArray(graph.size)
28+
for (e in initial) {
29+
val p = find(e)
30+
infected[p]++
31+
}
32+
var currSize = -1
33+
var ans = -1
34+
for (e in initial) {
35+
val p = find(e)
36+
if (infected[p] == 1 && size[p] >= currSize) {
37+
ans = if (size[p] > currSize) {
38+
e
39+
} else {
40+
ans.coerceAtMost(e)
41+
}
42+
currSize = size[p]
43+
}
44+
}
45+
// all groups have more than 1 infected node then return min value from initial
46+
if (ans == -1) {
47+
ans = initial[0]
48+
for (j in initial) {
49+
ans = ans.coerceAtMost(j)
50+
}
51+
}
52+
return ans
53+
}
54+
55+
private fun merge(p1: Int, p2: Int) {
56+
if (p1 != p2) {
57+
if (size[p1] > size[p2]) {
58+
par[p2] = p1
59+
size[p1] += size[p2]
60+
} else {
61+
par[p1] = p2
62+
size[p2] += size[p1]
63+
}
64+
}
65+
}
66+
67+
private fun find(u: Int): Int {
68+
if (par[u] == u) {
69+
return u
70+
}
71+
par[u] = find(par[u])
72+
return par[u]
73+
}
74+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
924\. Minimize Malware Spread
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. We will remove **exactly one node** from `initial`.
10+
11+
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**.
12+
13+
Note that if a node was removed from the `initial` list of infected nodes, it might still be infected later due to the malware spread.
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,0,0],[0,1,0],[0,0,1]], initial = [0,2]
24+
25+
**Output:** 0
26+
27+
**Example 3:**
28+
29+
**Input:** graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0901_1000.s0925_long_pressed_name
2+
3+
// #Easy #String #Two_Pointers #2023_04_24_Time_126_ms_(92.31%)_Space_33.6_MB_(84.62%)
4+
5+
class Solution {
6+
fun isLongPressedName(name: String, typed: String): Boolean {
7+
var right = 0
8+
for (left in typed.indices) {
9+
if (right < name.length && typed[left] == name[right]) {
10+
right++
11+
} else if (left == 0 || typed[left] != typed[left - 1]) {
12+
return false
13+
}
14+
}
15+
return right == name.length
16+
}
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
925\. Long Pressed Name
2+
3+
Easy
4+
5+
Your friend is typing his `name` into a keyboard. Sometimes, when typing a character `c`, the key might get _long pressed_, and the character will be typed 1 or more times.
6+
7+
You examine the `typed` characters of the keyboard. Return `True` if it is possible that it was your friends name, with some characters (possibly none) being long pressed.
8+
9+
**Example 1:**
10+
11+
**Input:** name = "alex", typed = "aaleex"
12+
13+
**Output:** true
14+
15+
**Explanation:** 'a' and 'e' in 'alex' were long pressed.
16+
17+
**Example 2:**
18+
19+
**Input:** name = "saeed", typed = "ssaaedd"
20+
21+
**Output:** false
22+
23+
**Explanation:** 'e' must have been pressed twice, but it was not in the typed output.
24+
25+
**Constraints:**
26+
27+
* `1 <= name.length, typed.length <= 1000`
28+
* `name` and `typed` consist of only lowercase English letters.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0901_1000.s0926_flip_string_to_monotone_increasing
2+
3+
// #Medium #String #Dynamic_Programming #2023_04_24_Time_199_ms_(100.00%)_Space_37.4_MB_(27.27%)
4+
5+
class Solution {
6+
fun minFlipsMonoIncr(s: String): Int {
7+
var ones = 0
8+
var flips = 0
9+
for (element in s) {
10+
if (element == '1') {
11+
ones++
12+
} else {
13+
flips = (flips + 1).coerceAtMost(ones)
14+
}
15+
}
16+
return flips
17+
}
18+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
926\. Flip String to Monotone Increasing
2+
3+
Medium
4+
5+
A binary string is monotone increasing if it consists of some number of `0`'s (possibly none), followed by some number of `1`'s (also possibly none).
6+
7+
You are given a binary string `s`. You can flip `s[i]` changing it from `0` to `1` or from `1` to `0`.
8+
9+
Return _the minimum number of flips to make_ `s` _monotone increasing_.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "00110"
14+
15+
**Output:** 1
16+
17+
**Explanation:** We flip the last digit to get 00111.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "010110"
22+
23+
**Output:** 2
24+
25+
**Explanation:** We flip to get 011111, or alternatively 000111.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "00011000"
30+
31+
**Output:** 2
32+
33+
**Explanation:** We flip to get 00000000.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= s.length <= 10<sup>5</sup></code>
38+
* `s[i]` is either `'0'` or `'1'`.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0901_1000.s0927_three_equal_parts
2+
3+
// #Hard #Array #Math #2023_04_24_Time_303_ms_(100.00%)_Space_41.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun threeEqualParts(arr: IntArray): IntArray {
7+
var ones = 0
8+
for (num in arr) {
9+
ones += num
10+
}
11+
if (ones == 0) {
12+
return intArrayOf(0, 2)
13+
} else if (ones % 3 != 0) {
14+
return intArrayOf(-1, -1)
15+
}
16+
ones /= 3
17+
var index1 = -1
18+
var index2 = -1
19+
var index3 = -1
20+
var totalOnes = 0
21+
for (i in arr.indices) {
22+
if (arr[i] == 0) {
23+
continue
24+
}
25+
totalOnes += arr[i]
26+
when (totalOnes) {
27+
1 -> index1 = i
28+
ones + 1 -> index2 = i
29+
2 * ones + 1 -> index3 = i
30+
}
31+
}
32+
while (index3 < arr.size) {
33+
if (arr[index1] == arr[index3] && arr[index2] == arr[index3]) {
34+
++index1
35+
++index2
36+
++index3
37+
} else {
38+
return intArrayOf(-1, -1)
39+
}
40+
}
41+
return intArrayOf(index1 - 1, index2)
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
927\. Three Equal Parts
2+
3+
Hard
4+
5+
You are given an array `arr` which consists of only zeros and ones, divide the array into **three non-empty parts** such that all of these parts represent the same binary value.
6+
7+
If it is possible, return any `[i, j]` with `i + 1 < j`, such that:
8+
9+
* `arr[0], arr[1], ..., arr[i]` is the first part,
10+
* `arr[i + 1], arr[i + 2], ..., arr[j - 1]` is the second part, and
11+
* `arr[j], arr[j + 1], ..., arr[arr.length - 1]` is the third part.
12+
* All three parts have equal binary values.
13+
14+
If it is not possible, return `[-1, -1]`.
15+
16+
Note that the entire part is used when considering what binary value it represents. For example, `[1,1,0]` represents `6` in decimal, not `3`. Also, leading zeros **are allowed**, so `[0,1,1]` and `[1,1]` represent the same value.
17+
18+
**Example 1:**
19+
20+
**Input:** arr = [1,0,1,0,1]
21+
22+
**Output:** [0,3]
23+
24+
**Example 2:**
25+
26+
**Input:** arr = [1,1,0,1,1]
27+
28+
**Output:** [-1,-1]
29+
30+
**Example 3:**
31+
32+
**Input:** arr = [1,1,0,0,1]
33+
34+
**Output:** [0,2]
35+
36+
**Constraints:**
37+
38+
* <code>3 <= arr.length <= 3 * 10<sup>4</sup></code>
39+
* `arr[i]` is `0` or `1`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0901_1000.s0924_minimize_malware_spread
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, 0, 0), intArrayOf(0, 1, 0), intArrayOf(0, 0, 1)),
26+
intArrayOf(0, 2)
27+
),
28+
equalTo(0)
29+
)
30+
}
31+
32+
@Test
33+
fun minMalwareSpread3() {
34+
assertThat(
35+
Solution()
36+
.minMalwareSpread(
37+
arrayOf(intArrayOf(1, 1, 1), intArrayOf(1, 1, 1), intArrayOf(1, 1, 1)),
38+
intArrayOf(1, 2)
39+
),
40+
equalTo(1)
41+
)
42+
}
43+
}

0 commit comments

Comments
 (0)