{"id":6999,"date":"2020-06-28T01:25:06","date_gmt":"2020-06-28T08:25:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6999"},"modified":"2020-06-28T01:35:40","modified_gmt":"2020-06-28T08:35:40","slug":"leetcode-1498-number-of-subsequences-that-satisfy-the-given-sum-condition","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-1498-number-of-subsequences-that-satisfy-the-given-sum-condition\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1498. Number of Subsequences That Satisfy the Given Sum Condition"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>target<\/code>.<\/p>\n\n\n\n<p>Return the number of&nbsp;<strong>non-empty<\/strong>&nbsp;subsequences of&nbsp;<code>nums<\/code>&nbsp;such that the sum of the minimum and maximum element on it is less or equal than&nbsp;<code>target<\/code>.<\/p>\n\n\n\n<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;10^9 + 7.<\/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 = [3,5,6,7], target = 9\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>There are 4 subsequences that satisfy the condition.\n[3] -&gt; Min value + max value &lt;= target (3 + 3 &lt;= 9)\n[3,5] -&gt; (3 + 5 &lt;= 9)\n[3,5,6] -&gt; (3 + 6 &lt;= 9)\n[3,6] -&gt; (3 + 6 &lt;= 9)\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 = [3,3,6,8], target = 10\n<strong>Output:<\/strong> 6\n<strong>Explanation: <\/strong>There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).\n[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]<\/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 = [2,3,3,4,6,7], target = 12\n<strong>Output:<\/strong> 61\n<strong>Explanation: <\/strong>There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]).\nNumber of valid subsequences (63 - 2 = 61).\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [5,2,4,1,7,6,8], target = 16\n<strong>Output:<\/strong> 127\n<strong>Explanation: <\/strong>All non-empty subset satisfy the condition (2^7 - 1) = 127<\/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^5<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10^6<\/code><\/li><li><code>1 &lt;= target &lt;= 10^6<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Two Pointers<\/strong><\/h2>\n\n\n\n<p>Since order of the elements in the subsequence doesn&#8217;t matter, we can sort the input array.<br>Very similar to two sum, we use two pointers (i, j) to maintain a window, s.t. nums[i] +nums[j] &lt;= target.<br>Then fix nums[i], any subset of (nums[i+1~j]) gives us a valid subsequence, thus we have 2^(j-(i+1)+1) = 2^(j-i) valid subsequence for window (i, j).<\/p>\n\n\n\n<p>Time complexity: O(nlogn) \/\/ Sort<br>Space complexity: O(n) \/\/ need to precompute 2^n % kMod.<\/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 numSubseq(vector<int>& nums, int target) {\n    constexpr int kMod = 1e9 + 7;\n    const int n = nums.size();\n    vector<int> p(n + 1, 1);\n    for (int i = 1; i <= n; ++i) \n      p[i] = (p[i - 1] << 1) % kMod;\n    sort(begin(nums), end(nums));\n    int ans = 0;\n    for (int i = 0, j = n - 1; i <= j; ++i) {\n      while (i <= j &#038;&#038; nums[i] + nums[j] > target) --j;\n      if (i > j) continue;\n      \/\/ In subarray nums[i~j]:\n      \/\/ min = nums[i], max = nums[j]\n      \/\/ nums[i] + nums[j] <= target\n      \/\/ {nums[i], (j - i - 1 + 1 values)}\n      \/\/ Any subset of the right part gives a valid subsequence \n      \/\/ in the original array. And There are 2^(j - i) ones.\n      ans = (ans + p[j - i]) % kMod;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;nums&nbsp;and an integer&nbsp;target. Return the number of&nbsp;non-empty&nbsp;subsequences of&nbsp;nums&nbsp;such that the sum of the minimum and maximum element on it is less&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[177,175,208],"class_list":["post-6999","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-medium","tag-two-pointers","tag-two-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6999","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=6999"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6999\/revisions"}],"predecessor-version":[{"id":7003,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6999\/revisions\/7003"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}