{"id":285,"date":"2017-09-15T21:55:26","date_gmt":"2017-09-16T04:55:26","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=285"},"modified":"2018-07-10T18:28:34","modified_gmt":"2018-07-11T01:28:34","slug":"leetcode-673-number-of-longest-increasing-subsequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-673-number-of-longest-increasing-subsequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 673. Number of Longest Increasing Subsequence"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 673. Number of Longest Increasing Subsequence - \u5237\u9898\u627e\u5de5\u4f5c EP56\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/SFCiuIJu17Y?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><a href=\"https:\/\/leetcode.com\/problems\/number-of-longest-increasing-subsequence\">https:\/\/leetcode.com\/problems\/number-of-longest-increasing-subsequence<\/a><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given an unsorted array of integers, find the number of longest increasing subsequence.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: [1,3,5,4,7]\r\nOutput: 2\r\nExplanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: [2,2,2,2,2]\r\nOutput: 5\r\nExplanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.<\/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\/673-ep56.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-290\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-289\" style=\"font-size: 1rem;\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/673-ep56-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Solution1:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 82 ms\r\nclass Solution {\r\npublic:\r\n    int findNumberOfLIS(vector&lt;int&gt;&amp; nums) {\r\n        int n = nums.size();\r\n        if (n == 0) return 0;\r\n        \r\n        c_ = vector&lt;int&gt;(n, 0);\r\n        l_ = vector&lt;int&gt;(n, 0);\r\n        \r\n        \/\/ Find the length LIS.\r\n        int max_len = 0;\r\n        for (int i = 0; i &lt; n; ++i)\r\n            max_len = max(max_len, len(nums, i));\r\n        \r\n        \/\/ Checking all endings.\r\n        int ans = 0;\r\n        for (int i = 0; i &lt; n; ++i)\r\n            if (len(nums, i) == max_len) \r\n                ans += count(nums, i);\r\n        \r\n        return ans;\r\n    }\r\nprivate:\r\n    vector&lt;int&gt; c_; \/\/ c[i]: number of LIS ends with nums[i] \/ NLIS'\r\n    vector&lt;int&gt; l_; \/\/ l[i]: lengeh of LIS ends with nums[i] \/ LIS'\r\n    \r\n    \/\/ Number of LIS ends with nums[n]\r\n    int count(const vector&lt;int&gt;&amp; nums, int n) {\r\n        if (n == 0) return 1;\r\n        if (c_[n] &gt; 0) return c_[n];\r\n        \r\n        int total_count = 0;\r\n        int l = len(nums, n);\r\n        for (int i = 0; i &lt; n; ++i)\r\n            if (nums[n] &gt; nums[i] &amp;&amp; len(nums, i) == l-1)\r\n                total_count += count(nums, i);\r\n        \r\n        if (total_count == 0)\r\n            total_count = 1;\r\n        \r\n        return c_[n] = total_count;\r\n    }\r\n    \r\n    \/\/ Length of LIS ends with nums[n]\r\n    int len(const vector&lt;int&gt;&amp; nums, int n) {\r\n        if (n == 0) return 1;\r\n        if (l_[n] &gt; 0) return l_[n];\r\n        \r\n        int max_len = 1;\r\n        \r\n        \/\/ Check every subarray\r\n        for (int i = 0; i &lt; n; ++i)\r\n            if (nums[n] &gt; nums[i])\r\n                max_len = max(max_len, len(nums, i) + 1);\r\n        \r\n        return l_[n] = max_len;\r\n    }\r\n};<\/pre>\n<p><strong>Solution2:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 33 ms\r\nclass Solution {\r\npublic:\r\n    int findNumberOfLIS(vector&lt;int&gt;&amp; nums) {\r\n        int n = nums.size();\r\n        if (n == 0) return 0;\r\n        \r\n        vector&lt;int&gt; c(n, 1);\r\n        vector&lt;int&gt; l(n, 1);\r\n        \r\n        for (int i = 1; i &lt; n; ++i)\r\n            for(int j = 0; j &lt; i; ++j)\r\n                if (nums[i] &gt; nums[j]) {\r\n                    if (l[j] + 1 &gt; l[i]) {\r\n                        l[i] = l[j] + 1;\r\n                        c[i] = c[j];\r\n                    } else if (l[j] + 1 == l[i]) {\r\n                        c[i] += c[j];\r\n                    }\r\n                }\r\n        \r\n        int max_len = *max_element(l.begin(), l.end());\r\n        \r\n        int ans = 0;\r\n        for (int i = 0; i &lt; n; ++i)\r\n            if (l[i] == max_len)\r\n                ans += c[i];\r\n        \r\n        return ans;\r\n    }\r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-300-longest-increasing-subsequence\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 300. Longest Increasing Subsequence<\/a><\/li>\n<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>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/zoj\/leetcode-longest-consecutive-sequence\/\">[LeetCode] Longest Consecutive Sequence<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode.com\/problems\/number-of-longest-increasing-subsequence Problem: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest&#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":[8,18,69],"class_list":["post-285","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-counting","tag-dp","tag-lis","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/285","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=285"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/285\/revisions"}],"predecessor-version":[{"id":3056,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/285\/revisions\/3056"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}