Skip to content

Commit 1898369

Browse files
committed
Improved tasks 3408, 3419, 3433, 3434, 3435, 3472
1 parent 674770b commit 1898369

File tree

6 files changed

+24
-30
lines changed
  • src/main/kotlin/g3401_3500
    • s3408_design_task_manager
    • s3419_minimize_the_maximum_edge_weight_of_graph
    • s3433_count_mentions_per_user
    • s3434_maximum_frequency_after_subarray_operation
    • s3435_frequencies_of_shortest_supersequences
    • s3472_longest_palindromic_subsequence_after_at_most_k_operations

6 files changed

+24
-30
lines changed

src/main/kotlin/g3401_3500/s3408_design_task_manager/readme.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,14 @@ TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103,
5656
import java.util.TreeSet
5757

5858
class TaskManager(tasks: List<List<Int>>) {
59-
private val tasks: TreeSet<IntArray?>
60-
private val taskMap: MutableMap<Int?, IntArray>
59+
private val tasks: TreeSet<IntArray> = TreeSet<IntArray>(
60+
Comparator { a: IntArray, b: IntArray ->
61+
if (b[2] == a[2]) b[1] - a[1] else b[2] - a[2]
62+
},
63+
)
64+
private val taskMap: MutableMap<Int, IntArray> = HashMap<Int, IntArray>()
6165

6266
init {
63-
this.tasks =
64-
TreeSet<IntArray?>(
65-
Comparator { a: IntArray?, b: IntArray? ->
66-
if (b!![2] == a!![2]) b[1] - a[1] else b[2] - a[2]
67-
},
68-
)
69-
this.taskMap = HashMap<Int?, IntArray>()
7067
for (task in tasks) {
7168
val t = intArrayOf(task[0], task[1], task[2])
7269
this.tasks.add(t)

src/main/kotlin/g3401_3500/s3419_minimize_the_maximum_edge_weight_of_graph/readme.md

+5-8
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,24 @@ import kotlin.math.max
7676
@Suppress("unused")
7777
class Solution {
7878
fun minMaxWeight(n: Int, edges: Array<IntArray>, threshold: Int): Int {
79-
val reversedG: Array<MutableList<IntArray>?> = arrayOfNulls<MutableList<IntArray>?>(n)
80-
for (i in 0..<n) {
81-
reversedG[i] = ArrayList<IntArray>()
82-
}
79+
val reversedG: Array<MutableList<IntArray>> = Array<MutableList<IntArray>>(n) { ArrayList<IntArray>() }
8380
for (i in edges) {
8481
val a = i[0]
8582
val b = i[1]
8683
val w = i[2]
87-
reversedG[b]!!.add(intArrayOf(a, w))
84+
reversedG[b].add(intArrayOf(a, w))
8885
}
8986
val distance = IntArray(n)
9087
distance.fill(Int.Companion.MAX_VALUE)
9188
distance[0] = 0
92-
if (reversedG[0]!!.isEmpty()) {
89+
if (reversedG[0].isEmpty()) {
9390
return -1
9491
}
95-
val que: Queue<Int?> = LinkedList<Int?>()
92+
val que: Queue<Int> = LinkedList<Int>()
9693
que.add(0)
9794
while (que.isNotEmpty()) {
9895
val cur: Int = que.poll()!!
99-
for (next in reversedG[cur]!!) {
96+
for (next in reversedG[cur]) {
10097
val node = next[0]
10198
val w = next[1]
10299
val nextdis = max(w, distance[cur])

src/main/kotlin/g3401_3500/s3433_count_mentions_per_user/readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ At timestamp 12, `"HERE"` is mentioned. Because `id0` is still offline, they wil
8787
class Solution {
8888
fun countMentions(numberOfUsers: Int, events: List<List<String>>): IntArray {
8989
val ans = IntArray(numberOfUsers)
90-
val l: MutableList<Int?> = ArrayList<Int?>()
90+
val l: MutableList<Int> = ArrayList<Int>()
9191
var c = 0
9292
for (i in events.indices) {
9393
val s = events[i][0]
@@ -112,7 +112,7 @@ class Solution {
112112
val id = events[i][2].toInt()
113113
val a = events[i][1].toInt() + 60
114114
for (j in l.indices) {
115-
if (l[j]!! >= a - 60 && l[j]!! < a) {
115+
if (l[j] >= a - 60 && l[j] < a) {
116116
ans[id]--
117117
}
118118
}

src/main/kotlin/g3401_3500/s3434_maximum_frequency_after_subarray_operation/readme.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ import kotlin.math.max
5151

5252
class Solution {
5353
fun maxFrequency(nums: IntArray, k: Int): Int {
54-
val count: MutableMap<Int?, Int?> = HashMap<Int?, Int?>()
54+
val count: MutableMap<Int, Int> = HashMap<Int, Int>()
5555
for (a in nums) {
56-
count.put(a, count.getOrDefault(a, 0)!! + 1)
56+
count.put(a, count.getOrDefault(a, 0) + 1)
5757
}
5858
var res = 0
5959
for (b in count.keys) {
60-
res = max(res, kadane(nums, k, b!!))
60+
res = max(res, kadane(nums, k, b))
6161
}
62-
return count.getOrDefault(k, 0)!! + res
62+
return count.getOrDefault(k, 0) + res
6363
}
6464

6565
private fun kadane(nums: IntArray, k: Int, b: Int): Int {

src/main/kotlin/g3401_3500/s3435_frequencies_of_shortest_supersequences/readme.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Solution {
6565
private val charToIdx = IntArray(26)
6666
private val used = BooleanArray(26)
6767

68-
fun supersequences(words: Array<String>): MutableList<MutableList<Int?>?> {
68+
fun supersequences(words: Array<String>): List<List<Int>> {
6969
charToIdx.fill(-1)
7070
for (w in words) {
7171
used[w[0].code - 'a'.code] = true
@@ -91,7 +91,7 @@ class Solution {
9191
}
9292
// Try all supersets of forcedMask; keep those that kill all cycles
9393
var best = 9999
94-
val goodSets: MutableList<Int?> = ArrayList<Int?>()
94+
val goodSets: MutableList<Int> = ArrayList<Int>()
9595
for (s in 0..<(1 shl m)) {
9696
if ((s and forcedMask) != forcedMask) {
9797
continue
@@ -106,16 +106,16 @@ class Solution {
106106
}
107107
}
108108
// Build distinct freq arrays from these sets
109-
val seen: MutableSet<String?> = HashSet<String?>()
110-
val ans: MutableList<MutableList<Int?>?> = ArrayList<MutableList<Int?>?>()
109+
val seen: MutableSet<String> = HashSet<String>()
110+
val ans: MutableList<MutableList<Int>> = ArrayList<MutableList<Int>>()
111111
for (s in goodSets) {
112112
val freq = IntArray(26)
113113
for (i in 0..<m) {
114-
freq[idxToChar[i].code - 'a'.code] = if ((s!! and (1 shl i)) != 0) 2 else 1
114+
freq[idxToChar[i].code - 'a'.code] = if ((s and (1 shl i)) != 0) 2 else 1
115115
}
116116
val key = freq.contentToString()
117117
if (seen.add(key)) {
118-
val tmp: MutableList<Int?> = ArrayList<Int?>()
118+
val tmp: MutableList<Int> = ArrayList<Int>()
119119
for (f in freq) {
120120
tmp.add(f)
121121
}

src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Solution {
5757
val arr = Array<IntArray>(26) { IntArray(26) }
5858
for (i in 0..25) {
5959
for (j in 0..25) {
60-
arr[i][j] = min(abs(i - j), (26 - abs(i - j)))
60+
arr[i][j] = min(abs(i - j), 26 - abs(i - j))
6161
}
6262
}
6363
val dp = Array<Array<IntArray>>(n) { Array<IntArray>(n) { IntArray(k + 1) } }

0 commit comments

Comments
 (0)