leetcode - 2461. Maximum Sum of Distinct Subarrays With Length K

Description

You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions:

复制代码
The length of the subarray is k, and
All the elements of the subarray are distinct.
Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return 0.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

复制代码
Input: nums = [1,5,4,2,9,9,9], k = 3
Output: 15
Explanation: The subarrays of nums with length 3 are:
- [1,5,4] which meets the requirements and has a sum of 10.
- [5,4,2] which meets the requirements and has a sum of 11.
- [4,2,9] which meets the requirements and has a sum of 15.
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions

Example 2:

复制代码
Input: nums = [4,4,4], k = 3
Output: 0
Explanation: The subarrays of nums with length 3 are:
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
We return 0 because no subarrays meet the conditions.

Constraints:

复制代码
1 <= k <= nums.length <= 10^5
1 <= nums[i] <= 10^5

Solution

Sliding window (TLE)

Use a sliding window to store all the elements we have visited. When the current element is in the sliding window, pop from left until it doesn't exist.

Time complexity: o ( n 2 ) o(n^2) o(n2)

Space complexity: o ( n ) o(n) o(n)

Sliding window + set

Previous solution will exceed the time limit because list's in has o ( n ) o(n) o(n) complexity. To mitigate this, we could use an additional set to store all the elements.

Time complexity: o ( n ) o(n) o(n)

Space complexity: o ( n ) o(n) o(n)

Code

Sliding window (TLE)

python3 复制代码
class Solution:
    def maximumSubarraySum(self, nums: List[int], k: int) -> int:
        sliding_window = collections.deque([])
        cur_sum = 0
        max_sum = 0
        for each_num in nums:
            while each_num in sliding_window or len(sliding_window) == k:
                pop_ele = sliding_window.popleft()
                cur_sum -= pop_ele
            cur_sum += each_num
            sliding_window.append(each_num)
            if len(sliding_window) == k:
                max_sum = max(max_sum, cur_sum)
        return max_sum

Sliding window + set

python3 复制代码
class Solution:
    def maximumSubarraySum(self, nums: List[int], k: int) -> int:
        sliding_window = collections.deque([])
        window_ele = set()
        cur_sum = 0
        max_sum = 0
        for each_num in nums:
            while each_num in window_ele or len(sliding_window) == k:
                pop_ele = sliding_window.popleft()
                window_ele.remove(pop_ele)
                cur_sum -= pop_ele
            cur_sum += each_num
            sliding_window.append(each_num)
            window_ele.add(each_num)
            if len(sliding_window) == k:
                max_sum = max(max_sum, cur_sum)
        return max_sum
相关推荐
@我漫长的孤独流浪10 分钟前
程序综合实践第十二周-二叉树
算法·深度优先·图论
啊阿狸不会拉杆10 分钟前
《数字图像处理》第 3 章 - 灰度变换与空间滤波
图像处理·人工智能·算法·计算机视觉·数字图像处理
执笔论英雄11 分钟前
【RL 】Ray 支持RDMA
算法
Keep_Trying_Go12 分钟前
统一的人群计数训练框架(PyTorch)——基于主流的密度图模型训练框架
人工智能·pytorch·python·深度学习·算法·机器学习·人群计数
(●—●)橘子……13 分钟前
记力扣557.反转字符串中的单词 练习理解
算法·leetcode·职场和发展
啊阿狸不会拉杆20 分钟前
《数字图像处理 》 第 1 章-绪论
图像处理·python·opencv·算法·数字图像处理
智驱力人工智能26 分钟前
加油站静电夹检测 视觉分析技术的安全赋能与实践 静电夹检测 加油站静电夹状态监测 静电接地报警器检测
人工智能·深度学习·算法·安全·yolo·边缘计算
断剑zou天涯33 分钟前
【算法笔记】线段树SegmentTree
数据结构·笔记·算法
ULTRA??1 小时前
各种排序算法时间复杂度分析和实现和优势
c++·python·算法·排序算法
sprintzer1 小时前
12.06-12.15力扣分治法刷题
算法·leetcode