Skip to content

Commit 25aa049

Browse files
authored
Improved tasks 3408, 3419, 3433, 3434, 3435, 3472
1 parent 57e77f4 commit 25aa049

File tree

6 files changed

+24
-30
lines changed
  • src/main/kotlin/g3401_3500

6 files changed

+24
-30
lines changed

src/main/kotlin/g3401_3500/s3408_design_task_manager/TaskManager.kt

+6-9
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ package g3401_3500.s3408_design_task_manager
66
import java.util.TreeSet
77

88
class TaskManager(tasks: List<List<Int>>) {
9-
private val tasks: TreeSet<IntArray?>
10-
private val taskMap: MutableMap<Int?, IntArray>
9+
private val tasks: TreeSet<IntArray> = TreeSet<IntArray>(
10+
Comparator { a: IntArray, b: IntArray ->
11+
if (b[2] == a[2]) b[1] - a[1] else b[2] - a[2]
12+
},
13+
)
14+
private val taskMap: MutableMap<Int, IntArray> = HashMap<Int, IntArray>()
1115

1216
init {
13-
this.tasks =
14-
TreeSet<IntArray?>(
15-
Comparator { a: IntArray?, b: IntArray? ->
16-
if (b!![2] == a!![2]) b[1] - a[1] else b[2] - a[2]
17-
},
18-
)
19-
this.taskMap = HashMap<Int?, IntArray>()
2017
for (task in tasks) {
2118
val t = intArrayOf(task[0], task[1], task[2])
2219
this.tasks.add(t)

src/main/kotlin/g3401_3500/s3419_minimize_the_maximum_edge_weight_of_graph/Solution.kt

+5-8
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,24 @@ import kotlin.math.max
1010
@Suppress("unused")
1111
class Solution {
1212
fun minMaxWeight(n: Int, edges: Array<IntArray>, threshold: Int): Int {
13-
val reversedG: Array<MutableList<IntArray>?> = arrayOfNulls<MutableList<IntArray>?>(n)
14-
for (i in 0..<n) {
15-
reversedG[i] = ArrayList<IntArray>()
16-
}
13+
val reversedG: Array<MutableList<IntArray>> = Array<MutableList<IntArray>>(n) { ArrayList<IntArray>() }
1714
for (i in edges) {
1815
val a = i[0]
1916
val b = i[1]
2017
val w = i[2]
21-
reversedG[b]!!.add(intArrayOf(a, w))
18+
reversedG[b].add(intArrayOf(a, w))
2219
}
2320
val distance = IntArray(n)
2421
distance.fill(Int.Companion.MAX_VALUE)
2522
distance[0] = 0
26-
if (reversedG[0]!!.isEmpty()) {
23+
if (reversedG[0].isEmpty()) {
2724
return -1
2825
}
29-
val que: Queue<Int?> = LinkedList<Int?>()
26+
val que: Queue<Int> = LinkedList<Int>()
3027
que.add(0)
3128
while (que.isNotEmpty()) {
3229
val cur: Int = que.poll()!!
33-
for (next in reversedG[cur]!!) {
30+
for (next in reversedG[cur]) {
3431
val node = next[0]
3532
val w = next[1]
3633
val nextdis = max(w, distance[cur])

src/main/kotlin/g3401_3500/s3433_count_mentions_per_user/Solution.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package g3401_3500.s3433_count_mentions_per_user
66
class Solution {
77
fun countMentions(numberOfUsers: Int, events: List<List<String>>): IntArray {
88
val ans = IntArray(numberOfUsers)
9-
val l: MutableList<Int?> = ArrayList<Int?>()
9+
val l: MutableList<Int> = ArrayList<Int>()
1010
var c = 0
1111
for (i in events.indices) {
1212
val s = events[i][0]
@@ -31,7 +31,7 @@ class Solution {
3131
val id = events[i][2].toInt()
3232
val a = events[i][1].toInt() + 60
3333
for (j in l.indices) {
34-
if (l[j]!! >= a - 60 && l[j]!! < a) {
34+
if (l[j] >= a - 60 && l[j] < a) {
3535
ans[id]--
3636
}
3737
}

src/main/kotlin/g3401_3500/s3434_maximum_frequency_after_subarray_operation/Solution.kt

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

88
class Solution {
99
fun maxFrequency(nums: IntArray, k: Int): Int {
10-
val count: MutableMap<Int?, Int?> = HashMap<Int?, Int?>()
10+
val count: MutableMap<Int, Int> = HashMap<Int, Int>()
1111
for (a in nums) {
12-
count.put(a, count.getOrDefault(a, 0)!! + 1)
12+
count.put(a, count.getOrDefault(a, 0) + 1)
1313
}
1414
var res = 0
1515
for (b in count.keys) {
16-
res = max(res, kadane(nums, k, b!!))
16+
res = max(res, kadane(nums, k, b))
1717
}
18-
return count.getOrDefault(k, 0)!! + res
18+
return count.getOrDefault(k, 0) + res
1919
}
2020

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

src/main/kotlin/g3401_3500/s3435_frequencies_of_shortest_supersequences/Solution.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Solution {
1111
private val charToIdx = IntArray(26)
1212
private val used = BooleanArray(26)
1313

14-
fun supersequences(words: Array<String>): MutableList<MutableList<Int?>?> {
14+
fun supersequences(words: Array<String>): List<List<Int>> {
1515
charToIdx.fill(-1)
1616
for (w in words) {
1717
used[w[0].code - 'a'.code] = true
@@ -37,7 +37,7 @@ class Solution {
3737
}
3838
// Try all supersets of forcedMask; keep those that kill all cycles
3939
var best = 9999
40-
val goodSets: MutableList<Int?> = ArrayList<Int?>()
40+
val goodSets: MutableList<Int> = ArrayList<Int>()
4141
for (s in 0..<(1 shl m)) {
4242
if ((s and forcedMask) != forcedMask) {
4343
continue
@@ -52,16 +52,16 @@ class Solution {
5252
}
5353
}
5454
// Build distinct freq arrays from these sets
55-
val seen: MutableSet<String?> = HashSet<String?>()
56-
val ans: MutableList<MutableList<Int?>?> = ArrayList<MutableList<Int?>?>()
55+
val seen: MutableSet<String> = HashSet<String>()
56+
val ans: MutableList<MutableList<Int>> = ArrayList<MutableList<Int>>()
5757
for (s in goodSets) {
5858
val freq = IntArray(26)
5959
for (i in 0..<m) {
60-
freq[idxToChar[i].code - 'a'.code] = if ((s!! and (1 shl i)) != 0) 2 else 1
60+
freq[idxToChar[i].code - 'a'.code] = if ((s and (1 shl i)) != 0) 2 else 1
6161
}
6262
val key = freq.contentToString()
6363
if (seen.add(key)) {
64-
val tmp: MutableList<Int?> = ArrayList<Int?>()
64+
val tmp: MutableList<Int> = ArrayList<Int>()
6565
for (f in freq) {
6666
tmp.add(f)
6767
}

src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Solution {
1212
val arr = Array<IntArray>(26) { IntArray(26) }
1313
for (i in 0..25) {
1414
for (j in 0..25) {
15-
arr[i][j] = min(abs(i - j), (26 - abs(i - j)))
15+
arr[i][j] = min(abs(i - j), 26 - abs(i - j))
1616
}
1717
}
1818
val dp = Array<Array<IntArray>>(n) { Array<IntArray>(n) { IntArray(k + 1) } }

0 commit comments

Comments
 (0)