[
array
sliding window
]
BinarySearch 0930 Maximum Unique Sublist Sum
Problem statement
https://binarysearch.com/problems/Maximum-Unique-Sublist-Sum/
Solution
Equal to Leetcode 0003. Longest Substring Without Repeating Characters, but now we keep not length, but sum.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(self, nums):
window, sm = set(), 0
beg, end, ans, n = 0, 0, 0, len(nums)
while beg < n and end < n:
if nums[end] not in window:
if end + 1 < n: window.add(nums[end])
sm += nums[end]
end += 1
ans = max(ans, sm)
else:
window.remove(nums[beg])
sm -= nums[beg]
beg += 1
return ans