第 237 场周赛题解
2025/8/31大约 1 分钟
第 237 场周赛题解
Q1 1832. 判断句子是否为全字母句
直接用$Set$来处理,简单~
class Solution1832 {
fun checkIfPangram(sentence: String): Boolean {
return sentence.toCharArray().toSet().size == 26
}
}
Q2 1833. 雪糕的最大数量
简单的贪心问题,也就是个$Easy$。
class Solution5735 {
fun maxIceCream(costs: IntArray, coins: Int): Int {
var cur = coins
var ans = 0
costs.sort()
for (i in costs.indices) {
if (cur >= costs[i]) {
ans++
cur -= costs[i]
} else {
return ans
}
}
return ans
}
}
Q3 1834. 单线程 CPU
选取下一个任务时,要选取当前已在等待中且执行事件最短的。直接用两个队列,每次执行完成当前任务时,将等待队列中符合要求的任务导入到执行队列。需要注意的是,若这里没有任何符合当前时间点要求的,则直接选取等待队列中的头。
class Solution1834 {
fun getOrder(tasks: Array<IntArray>): IntArray {
val queue = PriorityQueue<Triple<Long, Long, Int>>(compareBy { it.first })
for (i in tasks.indices) {
queue.offer(Triple(tasks[i][0].toLong(), tasks[i][1].toLong(), i))
}
var cur: Long = queue.peek().first
val ans = arrayListOf<Int>()
val curQueue = PriorityQueue<Triple<Long, Long, Int>>(compareBy({ it.second }, { it.third }))
while (queue.isNotEmpty() || curQueue.isNotEmpty()) {
while (queue.isNotEmpty() && queue.peek().first <= cur) {
curQueue.offer(queue.poll())
}
if (curQueue.isNotEmpty()) {
val item = curQueue.poll()
ans.add(item.third)
cur += item.second
} else {
cur = queue.peek().first
}
}
return ans.toIntArray()
}
}
Q4 1835. 所有数对按位与结果的异或和
公式:$(a&b)\oplus(a&c) = a&(b\oplus c)$
最终两个数组的计算可转化为:$(a1 \oplus a2 \oplus a3...)& (b1 \oplus b2 \oplus b3...)$
可以直接将两个数组的异或计算出来,取一个$&$即可。
class Solution1835 {
fun getXORSum(arr1: IntArray, arr2: IntArray): Int {
val a = arr1.fold(0) { a, b -> a xor b }
val b = arr2.fold(0) { a, b -> a xor b }
return a and b
}
}