{"id":310,"date":"2025-06-28T15:59:00","date_gmt":"2025-06-28T07:59:00","guid":{"rendered":"http:\/\/getzs.online\/?p=310"},"modified":"2025-06-30T22:40:05","modified_gmt":"2025-06-30T14:40:05","slug":"122-%e4%b9%b0%e5%8d%96%e8%82%a1%e7%a5%a8%e7%9a%84%e6%9c%80%e4%bd%b3%e6%97%b6%e6%9c%ba-ii","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/06\/28\/122-%e4%b9%b0%e5%8d%96%e8%82%a1%e7%a5%a8%e7%9a%84%e6%9c%80%e4%bd%b3%e6%97%b6%e6%9c%ba-ii\/","title":{"rendered":"122. \u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a II"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/best-time-to-buy-and-sell-stock-ii\/\">122. \u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a II<\/a><\/p>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>prices<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>prices[i]<\/code>&nbsp;\u8868\u793a\u67d0\u652f\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\u4ee5\u51b3\u5b9a\u662f\u5426\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\u5148\u8d2d\u4e70\uff0c\u7136\u540e\u5728&nbsp;<strong>\u540c\u4e00\u5929<\/strong>&nbsp;\u51fa\u552e\u3002<\/p>\n\n\n\n<p>\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\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>prices = [7,1,5,3,6,4]\n<strong>\u8f93\u51fa\uff1a<\/strong>7\n<strong>\u89e3\u91ca\uff1a<\/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\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\n\u6700\u5927\u603b\u5229\u6da6\u4e3a 4 + 3 = 7 \u3002<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>prices = [1,2,3,4,5]\n<strong>\u8f93\u51fa\uff1a<\/strong>4\n<strong>\u89e3\u91ca\uff1a<\/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\u6700\u5927\u603b\u5229\u6da6\u4e3a 4 \u3002<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>prices = [7,6,4,3,1]\n<strong>\u8f93\u51fa\uff1a<\/strong>0\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b, \u4ea4\u6613\u65e0\u6cd5\u83b7\u5f97\u6b63\u5229\u6da6\uff0c\u6240\u4ee5\u4e0d\u53c2\u4e0e\u4ea4\u6613\u53ef\u4ee5\u83b7\u5f97\u6700\u5927\u5229\u6da6\uff0c\u6700\u5927\u5229\u6da6\u4e3a 0\u3002<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= prices.length &lt;= 3 * 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>0 &lt;= prices[i] &lt;= 10<sup>4<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public int maxProfit(int&#91;] prices) {\n        return method1(prices, prices.length);\n    }\n\n    \/**\n     * \u8d2a\u5fc3\n     * 0 ms\n     * \u590d\u6742\u5ea6O(n)\n     *\/\n    public int method1(int&#91;] prices, int n) {\n        \/*\n            \u7c7b\u4f3c\u4e8e\u722c\u5c71 \u4f8b\u5982 &#91;1, 2, 3, 4]\n            \u6709\u76c8\u5229\u5219\u4e70\u5356\uff0c\u4e70\u51651 \u5356\u51fa 2\uff0c \u4e70\u51652 \u5356\u51fa3\uff0c\u6bcf\u5929\u7684\u5229\u6da6\u5c31\u662f1 + 1 + 1\uff0c \u7b49\u540c\u4e8e\u4e70\u51651\u5356\u51fa4\n         *\/\n        int mn = prices&#91;0], mx = 0;\n        int sum = 0;\n        for (int i = 1; i &lt; n; i++) {\n            mn = Math.min(mn, prices&#91;i]);\n            mx = Math.max(mx, prices&#91;i] - mn);\n            if (mx > 0) {\n                sum += mx;\n                mn = prices&#91;i];\n                mx = 0;\n            }\n        }\n        return sum;\n    }\n\n    public int method2(int&#91;] prices, int n) {\n        \/\/ \u7b80\u5316\u5199\u6cd5\n        int sum = 0, mn = Integer.MAX_VALUE;\n        for (int price : prices) {\n            if (price > mn) {\n                sum += price - mn;\n            }\n            mn = price;\n        }\n        return sum;\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>122. \u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a II \u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;prices&nbsp;\uff0c\u5176\u4e2d&nbsp;pr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,11],"tags":[],"class_list":["post-310","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=310"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":311,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/310\/revisions\/311"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}