# 第 237 场周赛题解
# Q1 1832. 判断句子是否为全字母句 (opens new window)
直接用来处理,简单~
class Solution1832 {
fun checkIfPangram(sentence: String): Boolean {
return sentence.toCharArray().toSet().size == 26
}
}
1
2
3
4
5
2
3
4
5
# Q2 1833. 雪糕的最大数量 (opens new window)
简单的贪心问题,也就是个。
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
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Q3 1834. 单线程 CPU (opens new window)
选取下一个任务时,要选取当前已在等待中且执行事件最短的。直接用两个队列,每次执行完成当前任务时,将等待队列中符合要求的任务导入到执行队列。需要注意的是,若这里没有任何符合当前时间点要求的,则直接选取等待队列中的头。
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()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Q4 1835. 所有数对按位与结果的异或和 (opens new window)
公式:
最终两个数组的计算可转化为:
可以直接将两个数组的异或计算出来,取一个即可。
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
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7