{"id":9188,"date":"2021-12-20T22:08:17","date_gmt":"2021-12-21T06:08:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9188"},"modified":"2021-12-20T22:08:41","modified_gmt":"2021-12-21T06:08:41","slug":"leetcode-2110-number-of-smooth-descent-periods-of-a-stock","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2110-number-of-smooth-descent-periods-of-a-stock\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2110. Number of Smooth Descent Periods of a Stock"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>prices<\/code>&nbsp;representing the daily price history of a stock, where&nbsp;<code>prices[i]<\/code>&nbsp;is the stock price on the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;day.<\/p>\n\n\n\n<p>A&nbsp;<strong>smooth descent period<\/strong>&nbsp;of a stock consists of&nbsp;<strong>one or more contiguous<\/strong>&nbsp;days such that the price on each day is&nbsp;<strong>lower<\/strong>&nbsp;than the price on the&nbsp;<strong>preceding day<\/strong>&nbsp;by&nbsp;<strong>exactly<\/strong>&nbsp;<code>1<\/code>. The first day of the period is exempted from this rule.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of&nbsp;<strong>smooth descent periods<\/strong><\/em>.<\/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> prices = [3,2,1,4]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> There are 7 smooth descent periods:\n[3], [2], [1], [4], [3,2], [2,1], and [3,2,1]\nNote that a period with one day is a smooth descent period by the definition.\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> prices = [8,6,7,7]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> There are 4 smooth descent periods: [8], [6], [7], and [7]\nNote that [8,6] is not a smooth descent period as 8 - 6 \u2260 1.\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> prices = [1]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> There is 1 smooth descent period: [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;= prices.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= prices[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Same as longest decreasing subarray. <\/p>\n\n\n\n<p>dp[i] := length of longest smoothing subarray ends with nums[i].<\/p>\n\n\n\n<p>dp[i] = dp[i &#8211; 1] + 1 if nums[i] + 1 = nums[i &#8211; 1] else 1<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(1)<\/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  long long getDescentPeriods(vector<int>& prices) {\n    const int n = prices.size();\n    vector<long long> dp(n, 1);\n    long long ans = 1;\n    for (int i = 1; i < n; ++i) {\n      if (prices[i] - prices[i - 1] == -1)\n        dp[i] = dp[i - 1] + 1;    \n      ans += dp[i];\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;prices&nbsp;representing the daily price history of a stock, where&nbsp;prices[i]&nbsp;is the stock price on the&nbsp;ith&nbsp;day. A&nbsp;smooth descent period&nbsp;of a stock consists&#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,177,41],"class_list":["post-9188","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9188","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=9188"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9188\/revisions"}],"predecessor-version":[{"id":9190,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9188\/revisions\/9190"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}