{"id":206,"date":"2017-09-10T11:22:10","date_gmt":"2017-09-10T18:22:10","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=206"},"modified":"2021-01-06T20:23:26","modified_gmt":"2021-01-07T04:23:26","slug":"leetcode-300-longest-increasing-subsequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-300-longest-increasing-subsequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 300. Longest Increasing Subsequence"},"content":{"rendered":"\n<figure class=\"wp-block-embed alignleft is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 300 Longest Increasing Subsequence O(nlogn) - \u5237\u9898\u627e\u5de5\u4f5c EP378\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/l2rCz7skAlk?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<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 300. Longest Increasing Subsequence (Dynamic Programming O(n^2)) -  \u5237\u9898\u627e\u5de5\u4f5c EP48\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/7DKFpWnaxLI?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><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given an unsorted array of integers, find the length of longest increasing subsequence.<\/p>\n<p>For example,<br \/>Given\u00a0<code>[10, 9, 2, 5, 3, 7, 101, 18]<\/code>,<br \/>The longest increasing subsequence is\u00a0<code>[2, 3, 7, 101]<\/code>, therefore the length is\u00a0<code>4<\/code>. Note that there may be more than one LIS combination, it is only necessary for you to return the length.<\/p>\n<p>Your algorithm should run in O(<i>n<sup>2<\/sup><\/i>) complexity.<\/p>\n<p><b>Follow up:<\/b>\u00a0Could you improve it to O(<i>n<\/i>\u00a0log\u00a0<i>n<\/i>) time complexity?<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Dynamic Programming<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-211\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-210\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-3-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-213 size-full\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-3-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-3-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-3-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-3-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/300-ep48-3-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Time Complexity:<\/strong><\/p>\n<p>O(n^2)<\/p>\n<p><strong>Solution 1:<\/strong><\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\n\/\/ Running time: 29 ms\nclass Solution {\npublic:\n    int lengthOfLIS(vector&lt;int&gt;&amp; nums) {\n        if (nums.empty()) return 0;\n        int n = nums.size();\n        auto f = vector&lt;int&gt;(n, 1);\n        for (int i = 1; i &lt; n; ++i)\n            for (int j = 0; j &lt; i; ++j)\n                if (nums[i] &gt; nums[j])\n                    f[i] = max(f[i], f[j] + 1);\n        return *max_element(f.begin(), f.end());\n    }\n};<\/pre>\n<pre class=\"lang:default decode:true\"><\/pre>\n<p><strong>Solution 2: DP + Binary Search \/ Patience Sort<\/strong><\/p>\n<p>dp[i] := smallest tailing number of a increasing subsequence of length i + 1.<\/p>\n<p>dp is an increasing array, we can use binary search to find the index to insert\/update the array.<\/p>\n<p>ans = len(dp)<\/p>\n<p>Time complexity: O(nlogn)<br \/>Space complexity: O(n)<\/p>\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:  \n  int lengthOfLIS(vector<int>& nums) {\n    const int n = nums.size();\n    if (n == 0) return 0;    \n    vector<int> dp;\n    for (int i = 0; i < n; ++i) {\n      auto it = lower_bound(begin(dp), end(dp), nums[i]);\n      if (it == end(dp))\n        dp.push_back(nums[i]);\n      else\n        *it = nums[i];\n    }\n    return dp.size();\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def lengthOfLIS(self, nums: List[int]) -> int:\n    d = []\n    for x in nums:\n      i = bisect_left(d, x)\n      if i == len(d): \n        d.append(x)\n      else:\n        d[i] = x\n    return len(d)\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><strong>Related Problems:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-674-longest-continuous-increasing-subsequence\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 674. Longest Continuous Increasing Subsequence<\/a><\/li><li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/zoj\/leetcode-longest-consecutive-sequence\/\">[LeetCode] Longest Consecutive Sequence<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given\u00a0[10, 9, 2, 5, 3, 7, 101, 18],The longest increasing&#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,69,177],"class_list":["post-206","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-lis","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/206","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=206"}],"version-history":[{"count":11,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":7929,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/206\/revisions\/7929"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}