{"id":5467,"date":"2023-11-20T12:19:54","date_gmt":"2023-11-20T04:19:54","guid":{"rendered":"https:\/\/white-night.club\/?p=5467"},"modified":"2023-11-20T13:24:00","modified_gmt":"2023-11-20T05:24:00","slug":"ad159","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/11\/20\/ad159\/","title":{"rendered":"121.\u00a0\u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4\u00a0<code>prices<\/code>\u00a0\uff0c\u5b83\u7684\u7b2c\u00a0<code>i<\/code>\u00a0\u4e2a\u5143\u7d20\u00a0<code>prices[i]<\/code>\u00a0\u8868\u793a\u4e00\u652f\u7ed9\u5b9a\u80a1\u7968\u7b2c\u00a0<code>i<\/code>\u00a0\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><strong>\u8f93\u5165\uff1a<\/strong>[7,1,5,3,6,4] <strong>\u8f93\u51fa\uff1a<\/strong>5 <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 \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<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><strong>\u8f93\u5165\uff1a<\/strong>prices = [7,6,4,3,1] <strong>\u8f93\u51fa\uff1a<\/strong>0 <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<\/p>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul>\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<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color has-link-color wp-elements-64b8fbd24c8a045babc5564a5f0141ad\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u52a8\u6001\u89c4\u5212<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u6c42\u7684\u662f\u6700\u5927\u5229\u6da6\u3002\u7406\u60f3\u60c5\u51b5\u5c31\u662f\u627e\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002\u5176\u5b9e\u6709\u70b9\u201c\u9884\u77e5\u672a\u6765\u201d\u7684\u610f\u601d\uff0c\u4e3e\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><em>1 2 3 5 4<\/em><\/strong><\/p>\n\n\n\n<p>\u5f88\u660e\u663e\uff0c\u57281\u4e70\u5165\u57285\u5356\u51fa\u5c31\u80fd\u62ff\u5230\u6700\u5927\u5229\u6da64\u3002\u90a3\u5982\u679c\u662f\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5462\uff1f<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><em>5 2 6 1 3 7<\/em><\/strong><\/p>\n\n\n\n<p>\u5148\u6bd4\u8f832\u548c6\uff0c\u6700\u5927\u5229\u6da6\u4e3a4\u3002\u6b64\u65f6\u6211\u4eec\u53d1\u73b0\u8fd8\u6709\u4e2a\u6bd4min=2\u66f4\u5c0f\u76841\uff0c\u90a3\u4e48\u66f4\u65b0min=1\uff0c\u518d\u5f80\u540e\u627e\u770b\u80fd\u4e0d\u80fd\u627e\u5230\u66f4\u5927\u7684\u5229\u6da6\u30023\u4e0d\u884c\uff0c\u5229\u6da6\u4e3a2\u30027\u53ef\u4ee5\uff0c\u5229\u6da6\u4e3a6\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2a\u6570\u7ec4\u5b58\u50a8price[i]-min\u7684\u7ed3\u679c\u3002\u5982\u679c\u78b0\u5230\u6bd4min\u66f4\u5c0f\u7684\u503c\uff0c\u5c31\u66f4\u65b0min\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import (\r\n    \"math\"\r\n    \"sort\"\r\n)\r\nfunc maxProfit(prices &#91;]int) int {\r\n     min:=math.MaxInt32\r\n     profit:=make(&#91;]int,len(prices))\r\n     for i:=0;i&lt;len(prices);i++{\r\n        if prices&#91;i]&lt;min{\r\n            min=prices&#91;i]\r\n        }\r\n        profit&#91;i]=prices&#91;i]-min\r\n     }\r\n     sort.Ints(profit)\r\n     return profit&#91;len(prices)-1]\r\n}<\/code><\/pre>\n\n\n\n<p>\u5b9e\u9645\u4e0a\u8fd8\u53ef\u4ee5\u4f18\u5316\u4e0b\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u8981\u8fd4\u56de\u6700\u5927\u7684\u90a3\u6b21\u4ea4\u6613\uff0c\u5176\u4ed6\u7684\u65e0\u6240\u8c13\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import \"math\"\r\nfunc maxProfit(prices &#91;]int) int {\r\n     min:=math.MaxInt32\r\n     result:=0\r\n     for i:=0;i&lt;len(prices);i++{\r\n         if prices&#91;i]&lt;min{\r\n             min=prices&#91;i]\r\n         }else if prices&#91;i]-min>result{\r\n             result=prices&#91;i]-min\r\n         }\r\n     }\r\n     return result\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\u52a8\u6001\u89c4\u5212<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/5467"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=5467"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/5467\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=5467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=5467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=5467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}