我正在解决Maximum Erasure Value - LeetCode,我的 solution 如下,但是出现了 Time Limit Exceeded ,我不太明白为什么,时间复杂度明明是 O(n)呀。🤐
class Solution:
def maximumUniqueSubarray(self, nums: List[int]) -> int:
# sliding window solution
res = 0
window, l, r = set(), 0, 0
while r < len(nums):
while r < len(nums) and nums[r] not in window:
window.add(nums[r])
r += 1
res = max(res, sum(window))
if r == len(nums):
return res
while nums[r] in window:
window.remove(nums[l])
l += 1
return res
不知道为什么 test case 的 nums 是空的。🤐
1
chitaotao 2023-02-01 00:07:52 +08:00 via Android
sum 需要用一个变量存储,r+1 时加 nums[r],l+1 时减 nums[l]
|