力扣第 465 场周赛

本文最后更新于 3 分钟前,文中所描述的信息可能已发生改变。

3668. 重排完成顺序 - 力扣(LeetCode)

题目类型

#哈希

解题思路

哈希表,直接遍历即可

示例代码

python
class Solution:
    def recoverOrder(self, order: List[int], friends: List[int]) -> List[int]:
        f = set(friends)
        return [x for x in order if x in f]

3669. K 因数分解 - 力扣(LeetCode)

题目类型

#回溯

解题思路

回溯,用大到小查找因子,可以预处理全部因子

示例代码

python
class Solution:
    def minDifference(self, n: int, k: int) -> List[int]:
        ans = []
        path = []
        mx = inf

        def dfs(x):
            if len(path) >= k:
                nonlocal mx, ans
                d = path[0] - path[-1]
                if x == 1 and d < mx:
                    mx = d
                    ans = path[:]
                return
            for y in range(1, x + 1):
                if x % y == 0:
                    if path and y > path[-1]:
                        break
                    path.append(y)
                    dfs(x // y)
                    path.pop()
            return

        dfs(n)
        return ans

3670. 没有公共位的整数最大乘积 - 力扣(LeetCode)

题目类型

#SOSDP #位运算

解题思路

参考:【模板】高维前缀和(SOS DP)Python/Java/C++/Go

示例代码

python
class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        mx_bit = max(nums).bit_length()
        msk = 1 << mx_bit
        f = [0] * msk
        for x in nums:
            f[x] = x

        for i in range(mx_bit):
            bit = 1 << i
            s = 0
            while s < msk:
                s |= bit
                v = f[s ^ bit]
                if v > f[s]:
                    f[s] = v
                s += 1

        return max(x * f[(msk - 1) ^ x] for x in nums)

3671. 子序列美丽值求和 - 力扣(LeetCode)

题目类型

#树状数组 #倍数容斥 #数学

解题思路

参考:这道题很神秘,以后再来解决吧!

示例代码

python
TODO
力扣第 164 场双周赛
力扣第 464 场周赛