{"id":9817,"date":"2022-09-11T22:16:09","date_gmt":"2022-09-12T05:16:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9817"},"modified":"2022-09-11T22:17:21","modified_gmt":"2022-09-12T05:17:21","slug":"leetcode-2407-longest-increasing-subsequence-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2407-longest-increasing-subsequence-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2407.\u00a0Longest Increasing Subsequence II"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Find the longest subsequence of&nbsp;<code>nums<\/code>&nbsp;that meets the following requirements:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The subsequence is&nbsp;<strong>strictly increasing<\/strong>&nbsp;and<\/li><li>The difference between adjacent elements in the subsequence is&nbsp;<strong>at most<\/strong>&nbsp;<code>k<\/code>.<\/li><\/ul>\n\n\n\n<p>Return<em>&nbsp;the length of the&nbsp;<strong>longest<\/strong>&nbsp;<strong>subsequence<\/strong>&nbsp;that meets the requirements.<\/em><\/p>\n\n\n\n<p>A&nbsp;<strong>subsequence<\/strong>&nbsp;is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.<\/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,1,4,3,4,5,8,15], k = 3\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong>\nThe longest subsequence that meets the requirements is [1,3,4,5,8].\nThe subsequence has a length of 5, so we return 5.\nNote that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3.\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 = [7,4,5,1,8,12,4,7], k = 5\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>\nThe longest subsequence that meets the requirements is [4,5,8,12].\nThe subsequence has a length of 4, so we return 4.\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 = [1,5], k = 1\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nThe longest subsequence that meets the requirements is [1].\nThe subsequence has a length of 1, so we return 1.\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], k &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP + Segment Tree | Max range query<\/strong><\/h2>\n\n\n\n<p>Let dp[i] := length of LIS end with number i.<br>dp[i] = 1 + max(dp[i-k:i])<\/p>\n\n\n\n<p>Naive dp takes O(n*k) time which will cause TLE.<\/p>\n\n\n\n<p>We can use segment tree to speed up the max range query to log(m), where m is the max value of the array.<\/p>\n\n\n\n<p>Time complexity: O(n*logm)<br>Space complexity: O(m)<\/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 lengthOfLIS(vector<int>& nums, int k) {\n    const int n = *max_element(begin(nums), end(nums));\n    vector<int> dp(2 * (n + 1));\n    auto query = [&](int l, int r) -> int {\n      int ans = 0;\n      for (l += n, r += n; l < r; l >>= 1, r >>= 1) {\n        if (l & 1) ans = max(ans, dp[l++]);      \n        if (r & 1) ans = max(ans, dp[--r]);\n      }\n      return ans;\n    };\n    auto update = [&](int i, int val) -> void {\n      dp[i += n] = val;\n      while (i > 1) {\n        i >>= 1;\n        dp[i] = max(dp[i * 2], dp[i * 2 + 1]);\n      }\n    };        \n    int ans = 0;\n    for (int x : nums) {\n      int cur = 1 + query(max(1, x - k), x);\n      update(x, cur);\n      ans = max(ans, cur);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums&nbsp;and an integer&nbsp;k. Find the longest subsequence of&nbsp;nums&nbsp;that meets the following requirements: The subsequence is&nbsp;strictly increasing&nbsp;and The difference between adjacent&#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,98,458],"class_list":["post-9817","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","tag-range-query","tag-segment-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9817","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=9817"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9817\/revisions"}],"predecessor-version":[{"id":9820,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9817\/revisions\/9820"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}