Leetcode 3321. Find X-Sum of All K-Long Subarrays II

  • [Leetcode 3321. Find X-Sum of All K-Long Subarrays II](#Leetcode 3321. Find X-Sum of All K-Long Subarrays II)
    • [1. 解题思路](#1. 解题思路)
    • [2. 代码实现](#2. 代码实现)

1. 解题思路

这一题同样虽然是一道hard的题目,但也是比较常规的,就是通过一个滑动窗口不断地维护当前长度为k的滑动窗口内所有数字的出现次数,进而维护一个按照出现次数和大小从大到小排列的数组,最后使用这个数组维护top x的频次的数字的总和即可。

2. 代码实现

给出python代码实现如下:

python 复制代码
class Solution:
    def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
        n = len(nums)
        cnt = Counter(nums[:k])
        q = sorted([(-v, -k) for k, v in cnt.items()])
        s = sum(it[0] * it[1] for it in q[:x])

        ans = [s]
        for i in range(k, n):
            key, val = nums[i-k], cnt[nums[i-k]]
            idx = bisect.bisect_left(q, (-val, -key))
            q.pop(idx)
            if idx < x:
                s -= val * key
                if x <= len(q):
                    s += q[x-1][0] * q[x-1][1]

            cnt[key] -= 1
            if cnt[key] > 0:
                bisect.insort(q, (-val+1, -key))
                nidx = bisect.bisect_left(q, (-val+1, -key))
                if nidx < x:
                    s += key * (val-1)
                    if x < len(q):
                        s -= q[x][0] * q[x][1]

            key, val = nums[i], cnt[nums[i]]
            cnt[key] += 1
            if val == 0:
                bisect.insort(q, (-val-1, -key))
                idx = bisect.bisect_left(q, (-val-1, -key))
                if idx < x:
                    s += (val+1) * key
                    if x < len(q):
                        s -= q[x][0] * q[x][1]
            else:
                idx = bisect.bisect_left(q, (-val, -key))
                q.pop(idx)
                if idx < x:
                    s -= val * key
                    if x <= len(q):
                        s += q[x-1][0] * q[x-1][1]

                bisect.insort(q, (-val-1, -key))
                idx = bisect.bisect_left(q, (-val-1, -key))
                if idx < x:
                    s += (val+1) * key
                    if x < len(q):
                        s -= q[x][0] * q[x][1]

            ans.append(s)
        return ans

提交代码评测得到:耗时3055ms,占用内存42.3MB。

相关推荐
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
元亓亓亓2 天前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展
伟大的车尔尼2 天前
滑动窗口题目:删除子数组的最大得分
滑动窗口
仙俊红2 天前
LeetCode每日一题,20250914
算法·leetcode·职场和发展
_不会dp不改名_2 天前
leetcode_21 合并两个有序链表
算法·leetcode·链表
吃着火锅x唱着歌2 天前
LeetCode 3302.字典序最小的合法序列
leetcode
睡不醒的kun2 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌2 天前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
爱编程的化学家2 天前
代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度
数据结构·c++·算法·leetcode·二叉树·代码随想录
吃着火锅x唱着歌2 天前
LeetCode 1446.连续字符
算法·leetcode·职场和发展