Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/11/19 20:26), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1131/1554 (看更多)
2461. Maximum Sum of Distinct Subarrays With Length K ## 思路 Sliding window 用HashSet記錄window內出現過的數字 如果有重複或是window太長就移動left ## CODE ```python class Solution: def maximumSubarraySum(self, nums: List[int], k: int) -> int: n = len(nums) seen = set() res = left = curr = 0 for right in range(n): while nums[right] in seen or right - left + 1 > k: seen.remove(nums[left]) curr -= nums[left] left += 1 curr += nums[right] seen.add(nums[right]) if right - left + 1 == k: res = max(res, curr) return res ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.250 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1732019173.A.F9E.html
文章代碼(AID): #1dF8Fb-U (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dF8Fb-U (Marginalman)