{"id":7823,"date":"2020-12-20T01:14:16","date_gmt":"2020-12-20T09:14:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7823"},"modified":"2020-12-20T01:14:27","modified_gmt":"2020-12-20T09:14:27","slug":"leetcode-1695-maximum-erasure-value","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1695-maximum-erasure-value\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1695. Maximum Erasure Value"},"content":{"rendered":"\n<p>You are given an array of positive integers&nbsp;<code>nums<\/code>&nbsp;and want to erase a subarray containing&nbsp;<strong>unique elements<\/strong>. The&nbsp;<strong>score<\/strong>&nbsp;you get by erasing the subarray is equal to the&nbsp;<strong>sum<\/strong>&nbsp;of its elements.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum score<\/strong>&nbsp;you can get by erasing&nbsp;<strong>exactly one<\/strong>&nbsp;subarray.<\/em><\/p>\n\n\n\n<p>An array&nbsp;<code>b<\/code>&nbsp;is called to be a&nbsp;subarray&nbsp;of&nbsp;<code>a<\/code>&nbsp;if it forms a contiguous subsequence of&nbsp;<code>a<\/code>, that is, if it is equal to&nbsp;<code>a[l],a[l+1],...,a[r]<\/code>&nbsp;for some&nbsp;<code>(l,r)<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [4,2,4,5,6]\n<strong>Output:<\/strong> 17\n<strong>Explanation:<\/strong> The optimal subarray here is [2,4,5,6].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [5,2,1,2,5,2,1,2,5]\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong> The optimal subarray here is [5,2,1] or [1,2,5].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding window + Hashset<\/strong><\/h2>\n\n\n\n<p>Maintain a window that has no duplicate elements.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maximumUniqueSubarray(vector<int>& nums) {\n    const int n = nums.size();\n    unordered_set<int> t;\n    int ans = 0;\n    for (int l = 0, r = 0, s = 0; r < n; ++r) {\n      while (t.count(nums[r]) &#038;&#038; l < r) {\n        s -= nums[l];\n        t.erase(nums[l++]);\n      }      \n      t.insert(nums[r]);\n      ans = max(ans, s += nums[r]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of positive integers&nbsp;nums&nbsp;and want to erase a subarray containing&nbsp;unique elements. The&nbsp;score&nbsp;you get by erasing the subarray is equal to the&nbsp;sum&nbsp;of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[299,177,215],"class_list":["post-7823","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-hashset","tag-medium","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7823"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7823\/revisions"}],"predecessor-version":[{"id":7825,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7823\/revisions\/7825"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}