{"id":2591,"date":"2022-02-04T13:14:34","date_gmt":"2022-02-04T05:14:34","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2591"},"modified":"2022-02-04T13:14:34","modified_gmt":"2022-02-04T05:14:34","slug":"leetcodeday122","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/02\/04\/leetcodeday122\/","title":{"rendered":"leetcodeday122&#8211;\u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a II"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4&nbsp;<code>prices<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>prices[i]<\/code>&nbsp;\u8868\u793a\u80a1\u7968\u7b2c&nbsp;<code>i<\/code>&nbsp;\u5929\u7684\u4ef7\u683c\u3002<\/p>\n\n\n\n<p>\u5728\u6bcf\u4e00\u5929\uff0c\u4f60\u53ef\u80fd\u4f1a\u51b3\u5b9a\u8d2d\u4e70\u548c\/\u6216\u51fa\u552e\u80a1\u7968\u3002\u4f60\u5728\u4efb\u4f55\u65f6\u5019&nbsp;<strong>\u6700\u591a<\/strong>&nbsp;\u53ea\u80fd\u6301\u6709&nbsp;<strong>\u4e00\u80a1<\/strong>&nbsp;\u80a1\u7968\u3002\u4f60\u4e5f\u53ef\u4ee5\u8d2d\u4e70\u5b83\uff0c\u7136\u540e\u5728&nbsp;<strong>\u540c\u4e00\u5929<\/strong>&nbsp;\u51fa\u552e\u3002<br>\u8fd4\u56de&nbsp;<em>\u4f60\u80fd\u83b7\u5f97\u7684&nbsp;<strong>\u6700\u5927<\/strong>&nbsp;\u5229\u6da6<\/em>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong> prices = &#91;7,1,5,3,6,4]\n<strong>\u8f93\u51fa:<\/strong> 7\n<strong>\u89e3\u91ca:<\/strong> \u5728\u7b2c 2 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 1\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 3 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 5\uff09\u7684\u65f6\u5019\u5356\u51fa, \u8fd9\u7b14\u4ea4\u6613\u6240\u80fd\u83b7\u5f97\u5229\u6da6 = 5-1 = 4 \u3002\n&nbsp;    \u968f\u540e\uff0c\u5728\u7b2c 4 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 3\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 5 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 6\uff09\u7684\u65f6\u5019\u5356\u51fa, \u8fd9\u7b14\u4ea4\u6613\u6240\u80fd\u83b7\u5f97\u5229\u6da6 = 6-3 = 3 \u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong> prices = &#91;1,2,3,4,5]\n<strong>\u8f93\u51fa:<\/strong> 4\n<strong>\u89e3\u91ca:<\/strong> \u5728\u7b2c 1 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 1\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 5 \u5929 \uff08\u80a1\u7968\u4ef7\u683c = 5\uff09\u7684\u65f6\u5019\u5356\u51fa, \u8fd9\u7b14\u4ea4\u6613\u6240\u80fd\u83b7\u5f97\u5229\u6da6 = 5-1 = 4 \u3002\n&nbsp;    \u6ce8\u610f\u4f60\u4e0d\u80fd\u5728\u7b2c 1 \u5929\u548c\u7b2c 2 \u5929\u63a5\u8fde\u8d2d\u4e70\u80a1\u7968\uff0c\u4e4b\u540e\u518d\u5c06\u5b83\u4eec\u5356\u51fa\u3002\u56e0\u4e3a\u8fd9\u6837\u5c5e\u4e8e\u540c\u65f6\u53c2\u4e0e\u4e86\u591a\u7b14\u4ea4\u6613\uff0c\u4f60\u5fc5\u987b\u5728\u518d\u6b21\u8d2d\u4e70\u524d\u51fa\u552e\u6389\u4e4b\u524d\u7684\u80a1\u7968\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong> prices = &#91;7,6,4,3,1]\n<strong>\u8f93\u51fa:<\/strong> 0\n<strong>\u89e3\u91ca:<\/strong> \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b, \u6ca1\u6709\u4ea4\u6613\u5b8c\u6210, \u6240\u4ee5\u6700\u5927\u5229\u6da6\u4e3a 0\u3002<\/code><\/pre>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u6cd5\u4e00\uff1a\u8d2a\u5fc3\u7b97\u6cd5\uff1a\u53ea\u8981\u5f53\u524d\u80a1\u7968\u8d5a\u4e86\uff0c\u5c31\u5356\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"1024\" height=\"815\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-30-1024x815.png\" alt=\"\" class=\"wp-image-2594\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-30-1024x815.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-30-300x239.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-30-768x611.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-30.png 1096w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=122 lang=python3\r\n#\r\n# &#91;122] \u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a II\r\n# \u52a8\u6001\u89c4\u5212\r\n# @lc code=start\r\nclass Solution:\r\n    def maxProfit(self, prices: List&#91;int]) -> int:\r\n        ans=0\r\n        for i in range(len(prices)-1):\r\n            if prices&#91;i] &lt;prices&#91;i+1]:\r\n                ans=ans+(prices&#91;i+1]-prices&#91;i])\r\n        return ans\r\n# @lc code=end\r\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"845\" height=\"209\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-29.png\" alt=\"\" class=\"wp-image-2592\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-29.png 845w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-29-300x74.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-29-768x190.png 768w\" sizes=\"(max-width: 845px) 100vw, 845px\" \/><\/figure>\n\n\n\n<p>\u6cd5\u4e8c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"1024\" height=\"636\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-31-1024x636.png\" alt=\"\" class=\"wp-image-2595\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-31-1024x636.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-31-300x186.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-31-768x477.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-31.png 1140w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\r\npublic:\r\n    int maxProfit(vector&lt;int>&amp; prices) {\r\n        int n = prices.size();\r\n        int dp&#91;n]&#91;2];\r\n        dp&#91;0]&#91;0] = 0, dp&#91;0]&#91;1] = -prices&#91;0];\r\n        for (int i = 1; i &lt; n; ++i) {\r\n            dp&#91;i]&#91;0] = max(dp&#91;i - 1]&#91;0], dp&#91;i - 1]&#91;1] + prices&#91;i]);\r\n            dp&#91;i]&#91;1] = max(dp&#91;i - 1]&#91;1], dp&#91;i - 1]&#91;0] - prices&#91;i]);\r\n        }\r\n        return dp&#91;n - 1]&#91;0];\r\n    }\r\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4&nbsp;prices&nbsp;\uff0c\u5176\u4e2d&nbsp;prices[i]&nbsp;\u8868\u793a\u80a1\u7968\u7b2c&#038;n &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/02\/04\/leetcodeday122\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday122&#8211;\u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a II<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2591"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2591"}],"version-history":[{"count":3,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2591\/revisions"}],"predecessor-version":[{"id":2597,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2591\/revisions\/2597"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2591"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}