diff --git a/src/main/kotlin/g1001_1100/s1044_longest_duplicate_substring/Solution.kt b/src/main/kotlin/g1001_1100/s1044_longest_duplicate_substring/Solution.kt index ed26160a2..52b05204f 100644 --- a/src/main/kotlin/g1001_1100/s1044_longest_duplicate_substring/Solution.kt +++ b/src/main/kotlin/g1001_1100/s1044_longest_duplicate_substring/Solution.kt @@ -6,7 +6,7 @@ package g1001_1100.s1044_longest_duplicate_substring class Solution { private lateinit var hsh: LongArray private lateinit var pw: LongArray - private val cnt: Array?> = arrayOfNulls(26) + private val cnt: Array> = Array(26) { ArrayList() } fun longestDupSubstring(s: String): String { val n = s.length @@ -20,17 +20,17 @@ class Solution { for (j in 1..n) { hsh[j] = (hsh[j - 1] * base + s[j - 1].code.toLong()) % MOD pw[j] = pw[j - 1] * base % MOD - cnt[s[j - 1].code - 'a'.code]!!.add(j - 1) + cnt[s[j - 1].code - 'a'.code].add(j - 1) } var ans = "" for (i in 0..25) { - if (cnt[i]!!.isEmpty()) { + if (cnt[i].isEmpty()) { continue } - val idx: MutableList? = cnt[i] - var set: MutableSet + val idx: MutableList = cnt[i] + var set: MutableSet var lo = 1 - var hi = n - idx!![0] + var hi = n - idx[0] while (lo <= hi) { val len = (lo + hi) / 2 set = HashSet() diff --git a/src/main/kotlin/g2501_2600/s2551_put_marbles_in_bags/Solution.kt b/src/main/kotlin/g2501_2600/s2551_put_marbles_in_bags/Solution.kt index 173ed8300..fae1a48d6 100644 --- a/src/main/kotlin/g2501_2600/s2551_put_marbles_in_bags/Solution.kt +++ b/src/main/kotlin/g2501_2600/s2551_put_marbles_in_bags/Solution.kt @@ -7,27 +7,27 @@ import java.util.PriorityQueue class Solution { fun putMarbles(weights: IntArray, k: Int): Long { - // Map, long[]> memo = new HashMap<>(); - // long[] res = dfs(weights, 0, k, memo); - // return res[1] - res[0]; - if (k == 1 || k == weights.size) return 0 + if (k == 1 || k == weights.size) { + return 0 + } val min = PriorityQueue() - val max = PriorityQueue { a: Long?, b: Long? -> - java.lang.Long.compare( - b!!, - a!!, - ) + val max = PriorityQueue { a: Long, b: Long -> + b.compareTo(a) } for (i in 0 until weights.size - 1) { val sum = weights[i].toLong() + weights[i + 1] min.offer(sum) max.offer(sum) - if (min.size == k) min.poll() - if (max.size == k) max.poll() + if (min.size == k) { + min.poll() + } + if (max.size == k) { + max.poll() + } } var res: Long = 0 while (max.isNotEmpty()) { - res += min.poll() - max.poll()!! + res += min.poll() - max.poll() } return res } diff --git a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt index 8beb4e525..62f7c6756 100644 --- a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt @@ -26,7 +26,7 @@ class Solution { private fun dfs(v: Int, parent: Int): LongArray { var sum: Long = 0 - val pq = PriorityQueue() + val pq = PriorityQueue() for (e in adj[v]) { val w = if (e[0] == v) e[1] else e[0] if (w == parent) {