1695. Maximum Erasure Value 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718192021222324class Solution {public: int maximumUniqueSubarray(vector<int>& nums) { // slide window int ret = 0; unordered_map<int,int> window, need; int l = 0, r = 0,n = nums.size(), len = 0, window_total = 0; while(r<n){ int c = nums[r++]; if(window[c]==0) len++; window[c]++; window_total+=c; while(window[c]>1){ int d = nums[l++]; window[d]--; if(window[d] == 0) len--; window_total-=d; } ret = max(ret, window_total); } return ret; }}; analysis time complexity O(n) space complexity O(n)