{"id":308,"date":"2025-06-28T15:50:00","date_gmt":"2025-06-28T07:50:00","guid":{"rendered":"http:\/\/getzs.online\/?p=308"},"modified":"2025-06-30T22:38:39","modified_gmt":"2025-06-30T14:38:39","slug":"121-%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","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/06\/28\/121-%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\/","title":{"rendered":"121. \u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/best-time-to-buy-and-sell-stock\/\">121. \u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a<\/a><\/p>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4&nbsp;<code>prices<\/code>&nbsp;\uff0c\u5b83\u7684\u7b2c&nbsp;<code>i<\/code>&nbsp;\u4e2a\u5143\u7d20&nbsp;<code>prices[i]<\/code>&nbsp;\u8868\u793a\u4e00\u652f\u7ed9\u5b9a\u80a1\u7968\u7b2c&nbsp;<code>i<\/code>&nbsp;\u5929\u7684\u4ef7\u683c\u3002<\/p>\n\n\n\n<p>\u4f60\u53ea\u80fd\u9009\u62e9&nbsp;<strong>\u67d0\u4e00\u5929<\/strong>&nbsp;\u4e70\u5165\u8fd9\u53ea\u80a1\u7968\uff0c\u5e76\u9009\u62e9\u5728&nbsp;<strong>\u672a\u6765\u7684\u67d0\u4e00\u4e2a\u4e0d\u540c\u7684\u65e5\u5b50<\/strong>&nbsp;\u5356\u51fa\u8be5\u80a1\u7968\u3002\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u6765\u8ba1\u7b97\u4f60\u6240\u80fd\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u4f60\u53ef\u4ee5\u4ece\u8fd9\u7b14\u4ea4\u6613\u4e2d\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002\u5982\u679c\u4f60\u4e0d\u80fd\u83b7\u53d6\u4efb\u4f55\u5229\u6da6\uff0c\u8fd4\u56de&nbsp;<code>0<\/code>&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>[7,1,5,3,6,4]\n<strong>\u8f93\u51fa\uff1a<\/strong>5\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 5 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 6\uff09\u7684\u65f6\u5019\u5356\u51fa\uff0c\u6700\u5927\u5229\u6da6 = 6-1 = 5 \u3002\n     \u6ce8\u610f\u5229\u6da6\u4e0d\u80fd\u662f 7-1 = 6, \u56e0\u4e3a\u5356\u51fa\u4ef7\u683c\u9700\u8981\u5927\u4e8e\u4e70\u5165\u4ef7\u683c\uff1b\u540c\u65f6\uff0c\u4f60\u4e0d\u80fd\u5728\u4e70\u5165\u524d\u5356\u51fa\u80a1\u7968\u3002\n<\/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 = [7,6,4,3,1]\n<strong>\u8f93\u51fa\uff1a<\/strong>0\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b, \u6ca1\u6709\u4ea4\u6613\u5b8c\u6210, \u6240\u4ee5\u6700\u5927\u5229\u6da6\u4e3a 0\u3002\n<\/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;= 10<sup>5<\/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     * 1 ms\n     * \u590d\u6742\u5ea6O(n)\n     *\/\n    public int method1(int&#91;] prices, int n) {\n        int maxProfit = 0;\n        int mn = prices&#91;0];\n        for (int price : prices) {\n            \/\/ \u627e\u5230\u6700\u5c0f\u503c\n            mn = Math.min(mn, price);\n            \/\/ \u4e0e\u5f53\u524d\u503c\u6bd4\u8f83\uff0c\u6c42\u51fa\u6700\u5927\u5229\u6da6\n            maxProfit = Math.max(maxProfit, price - mn);\n        }\n        return maxProfit;\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>121. \u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a \u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4&nbsp;prices&nbsp;\uff0c\u5b83\u7684\u7b2c&nbsp;i&#038;nbsp [&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-308","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/308","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=308"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/308\/revisions\/309"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}