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解题比较方便
-
更新窗口内数据:
- 更新 sum
- count的变动依据如下:(镜面对称)
window[rightElement]++
if window[rightElement] == 1 {
count--
}
if window[leftElement] == 1 {
count--
}
window[leftElement]--
- 更新result:
- 时机: 每次窗口收缩前
- 条件:
count == k
Time complexity = $O(n)$