Categorygithub.com/szhou12/leetcode-goleetcode2461-Maximum-Sum-of-Distinct-Subarrays-With-Length-K
package
0.0.0-20241220224003-b7cf03a90b2b
Repository: https://github.com/szhou12/leetcode-go.git
Documentation: pkg.go.dev

# README

2461. Maximum Sum of Distinct Subarrays With Length K

Solution idea

Sliding Window

  • 一看题目是要求定长为k的subarray,用模版Flex解题比较方便

  • 更新窗口内数据:

    1. 更新 sum
    2. count的变动依据如下:(镜面对称)
window[rightElement]++
if window[rightElement] == 1 {
    count--
}
if window[leftElement] == 1 {
    count--
}
window[leftElement]--
  • 更新result:
    1. 时机: 每次窗口收缩前
    2. 条件: count == k

Time complexity = $O(n)$

Resource

wisdompeak/LeetCode