{"id":6668,"date":"2020-04-26T00:58:26","date_gmt":"2020-04-26T07:58:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6668"},"modified":"2020-04-26T16:40:41","modified_gmt":"2020-04-26T23:40:41","slug":"leetcode-1425-constrained-subset-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1425-constrained-subset-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1425. Constrained Subset Sum"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1425. Constrained Subset Sum - \u5237\u9898\u627e\u5de5\u4f5c EP321\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/B5fa989qz4U?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given an integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>, return the maximum sum of a&nbsp;<strong>non-empty<\/strong>&nbsp;subset of that array such that for every&nbsp;two&nbsp;<strong>consecutive<\/strong>&nbsp;integers in the subset,&nbsp;<code>nums[i]<\/code>&nbsp;and&nbsp;<code>nums[j]<\/code>, where&nbsp;<code>i &lt; j<\/code>, the condition&nbsp;<code>j - i &lt;= k<\/code>&nbsp;is satisfied.<\/p>\n\n\n\n<p>A&nbsp;<em>subset<\/em>&nbsp;of an array is&nbsp;obtained by deleting some number of elements (can be&nbsp;zero) from the array, leaving the remaining elements in their original order.<\/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 = [10,2,-10,5,20], k = 2\n<strong>Output:<\/strong> 37\n<strong>Explanation:<\/strong> The subset is [10, 2, 5, 20].\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 = [-1,-2,-3], k = 1\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> The subset must be non-empty, so we choose the largest number.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [10,-2,-10,-5,20], k = 2\n<strong>Output:<\/strong> 23\n<strong>Explanation:<\/strong> The subset is [10, -2, -5, 20].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= nums.length &lt;= 10^5<\/code><\/li><li><code>-10^4&nbsp;&lt;= nums[i] &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP \/ Sliding window \/ monotonic queue<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/04\/1425-ep231-1.png\" alt=\"\" class=\"wp-image-6673\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/04\/1425-ep231-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/04\/1425-ep231-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/04\/1425-ep231-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp[i] := max sum of a subset that include nums[i]<br>dp[i] := max(dp[i-1], dp[i-2], &#8230;, dp[i-k-1], 0) + nums[i]<\/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 constrainedSubsetSum(vector<int>& nums, int k) {    \n    const int n = nums.size();\n    vector<int> dp(n);\n    multiset<int> s{INT_MIN};\n    int ans = INT_MIN;\n    for (int i = 0; i < nums.size(); ++i) {\n      if (i > k) s.erase(s.equal_range(dp[i - k - 1]).first);\n      dp[i] = max(*rbegin(s), 0) + nums[i];\n      s.insert(dp[i]);\n      ans = max(ans, dp[i]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Use a monotonic queue to track the maximum of a sliding window dp[i-k-1] ~ dp[i-1].<\/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 184 ms\nclass Solution {\npublic:\n  int constrainedSubsetSum(vector<int>& nums, int k) {\n    const int n = nums.size();\n    vector<int> dp(n);\n    deque<int> q;\n    int ans = INT_MIN;\n    for (int i = 0; i < nums.size(); ++i) {\n      if (i > k && q.front() == i - k - 1)\n        q.pop_front();\n      dp[i] = (q.empty() ? 0 : max(dp[q.front()], 0))\n             + nums[i];\n      while (!q.empty() && dp[i] >= dp[q.back()])\n        q.pop_back();\n      q.push_back(i);\n      ans = max(ans, dp[i]);\n    }\n    return ans;\n  }\n};\n\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums&nbsp;and an integer&nbsp;k, return the maximum sum of a&nbsp;non-empty&nbsp;subset of that array such that for every&nbsp;two&nbsp;consecutive&nbsp;integers in the subset,&nbsp;nums[i]&nbsp;and&nbsp;nums[j], where&nbsp;i &lt; j,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,217,587,215],"class_list":["post-6668","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","tag-monotonic-queue","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6668","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=6668"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6668\/revisions"}],"predecessor-version":[{"id":6675,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6668\/revisions\/6675"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}