第 252 场周赛题解
2025/10/30小于 1 分钟
第 252 场周赛题解
Q1 1952. 三除数
正常模拟即可。
class Solution5830 {
    fun isThree(n: Int): Boolean {
        var tmp = 0
        for (i in 1..n) {
            if (n % i == 0) {
                tmp++
                if (tmp > 3) return false
            }
        }
        return tmp == 3
    }
}Q2 1953. 你可以工作的最大周数
只有最大值超过总数的一半,才无法满额工作,仅能工作$max * 2 + 1$天。
class Solution5831 {
    fun numberOfWeeks(milestones: IntArray): Long {
        var sum = 0L
        var max = 0L
        milestones.forEach {
            sum += it
            max = maxOf(max, it.toLong())
        }
        return if (sum - max >= max) {
            sum
        } else {
            (sum - max) * 2 + 1
        }
    }
}Q3 1954. 收集足够苹果的最小花园周长
无聊的数学计算。
class Solution5187 {
    fun minimumPerimeter(neededApples: Long): Long {
        var n = 0L
        var count = 0L
        while (count < neededApples) {
            n += 1
            count += 12 * n * n
        }
        return 8 * n
    }
}Q4 1955. 统计特殊子序列的数目
遍历,分别当前以$0、1、2$结尾的序列数。
本周是无聊的一周,数学题没啥意思。
class Solution5833 {
    fun countSpecialSubsequences(nums: IntArray): Int {
        val mod = 1000000007L
        var a = 0L
        var b = 0L
        var c = 0L
        for (x in nums) {
            when (x) {
                0 -> a = (a * 2 + 1) % mod
                1 -> b = (b * 2 + a) % mod
                2 -> c = (c * 2 + b) % mod
            }
        }
        return c.toInt()
    }
}