{"id":40,"date":"2026-03-24T16:03:00","date_gmt":"2026-03-24T08:03:00","guid":{"rendered":"https:\/\/geocommunity.vip\/?p=40"},"modified":"2026-03-27T10:01:56","modified_gmt":"2026-03-27T02:01:56","slug":"%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92%ef%bc%9a%e4%b9%b0%e5%8d%96%e8%82%a1%e7%a5%a8%e7%9a%84%e6%9c%80%e4%bd%b3%e6%97%b6%e9%97%b4-%e7%8a%b6%e6%80%81%e6%9c%ba","status":"publish","type":"post","link":"https:\/\/geocommunity.vip\/index.php\/2026\/03\/24\/%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92%ef%bc%9a%e4%b9%b0%e5%8d%96%e8%82%a1%e7%a5%a8%e7%9a%84%e6%9c%80%e4%bd%b3%e6%97%b6%e9%97%b4-%e7%8a%b6%e6%80%81%e6%9c%ba\/","title":{"rendered":"\u52a8\u6001\u89c4\u5212\uff1a\u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u95f4\/\u72b6\u6001\u673a"},"content":{"rendered":"\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-351f99f14ed03886917dac9e1dded632 wp-block-paragraph\">\u52a8\u6001\u89c4\u5212\u7c7b\u95ee\u9898\uff0c\u53ea\u9700\u8981\u4f9d\u6b21\u786e\u5b9adp\u6570\u7ec4\u4ee5\u53cadp[i]\u7684\u542b\u4e49\u3001\u521d\u59cb\u72b6\u6001\u3001\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u7136\u540e\u8fdb\u884c\u52a8\u6001\u89c4\u5212\u5373\u53ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u3001\u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a\u542b\u51b7\u51bb\u671f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u9898\u76ee\u63cf\u8ff0\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>prices<\/code>\uff0c\u5176\u4e2d\u7b2c <code>prices[i]<\/code> \u8868\u793a\u7b2c i \u5929\u7684\u80a1\u7968\u4ef7\u683c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u8ba1\u7b97\u51fa\u6700\u5927\u5229\u6da6\u3002\u5728\u6ee1\u8db3\u4ee5\u4e0b\u7ea6\u675f\u6761\u4ef6\u4e0b\uff0c\u4f60\u53ef\u4ee5\u5c3d\u53ef\u80fd\u5730\u5b8c\u6210\u66f4\u591a\u7684\u4ea4\u6613\uff08\u591a\u6b21\u4e70\u5356\u4e00\u652f\u80a1\u7968\uff09\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5356\u51fa\u80a1\u7968\u540e\uff0c\u4f60\u65e0\u6cd5\u5728\u7b2c\u4e8c\u5929\u4e70\u5165\u80a1\u7968\uff08\u5373\u51b7\u51bb\u671f\u4e3a 1 \u5929\uff09<\/li>\n\n\n\n<li>\u4f60\u4e0d\u80fd\u540c\u65f6\u53c2\u4e0e\u591a\u7b14\u4ea4\u6613\uff08\u4f60\u5fc5\u987b\u5728\u518d\u6b21\u8d2d\u4e70\u524d\u51fa\u552e\u6389\u4e4b\u524d\u7684\u80a1\u7968\uff09<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u4f7f\u7528\u72b6\u6001\u673aDP\uff0c\u5b9a\u4e49\u4e09\u79cd\u72b6\u6001\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dp0<\/code>\uff1a\u4e0d\u6301\u80a1\uff0c\u975e\u51b7\u51bb\u671f<\/li>\n\n\n\n<li><code>dp1<\/code>\uff1a\u6301\u80a1<\/li>\n\n\n\n<li><code>dp2<\/code>\uff1a\u51b7\u51bb\u671f<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u72b6\u6001dp0\u3001dp1\u3001dp2\u4e4b\u95f4\u5206\u522b\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\uff0c\u5178\u578b\u7684\u591a\u79cd\u72b6\u6001\u4e92\u76f8\u8f6c\u79fb\u7684\u9898\u578b\u3002\u5176\u4e2d\uff0cdp0\u3001dp1\u3001dp2\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u4e3a\u4e09\u4e2a\u6570\u7ec4\uff08\u7d22\u5f15\u4ece1\u5f00\u59cb\uff09\uff0c\u6bd4\u5982dp0[i]\u8868\u793a\u7b2ci\u5929\u7ed3\u675f\u540e\u4e3a\u201c\u4e0d\u6301\u80a1\uff0c\u975e\u51b7\u51bb\u671f\u201d\u72b6\u6001\u3002\u4f46\u662f\uff0c\u4f60\u5f00\u8f9f\u4e86\u66f4\u591a\u7684\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define MAX(a, b) (a &gt; b ? a : b)\n\nint maxProfit(int* prices, int pricesSize) {\n    int dp0 = 0;              \/\/ \u4e0d\u6301\u80a1\uff0c\u975e\u51b7\u51bb\u671f(\u7b2c\u4e00\u5929\u7ed3\u675f\u65f6)\n    int dp1 = -prices&#91;0];     \/\/ \u6301\u80a1(\u7b2c\u4e00\u5929\u7ed3\u675f\u65f6)\n    int dp2 = 0;              \/\/ \u51b7\u51bb\u671f(\u7b2c\u4e00\u5929\u7ed3\u675f\u65f6)\n\n    \/\/ \u904d\u5386\u6bcf\u4e00\u5929\uff0c\u4ece\u524d\u4e00\u5929\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\n    for (int i = 1; i &lt; pricesSize; i++) {\n        int new_dp0 = MAX(dp0, dp2);\n        int new_dp1 = MAX(dp0 - prices&#91;i], dp1);\n        int new_dp2 = dp1 + prices&#91;i];\n\n        dp0 = new_dp0;\n        dp1 = new_dp1;\n        dp2 = new_dp2;\n    }\n\n    return MAX(dp0, dp2);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u72b6\u6001\u8f6c\u79fb<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"167\" src=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-10-1024x167.png\" alt=\"\" class=\"wp-image-41\" srcset=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-10-1024x167.png 1024w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-10-300x49.png 300w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-10-768x125.png 768w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-10.png 1032w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8c\u3001\u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a\u542b\u624b\u7eed\u8d39<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u9898\u76ee\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>prices<\/code>\uff0c\u5176\u4e2d <code>prices[i]<\/code> \u8868\u793a\u7b2c i \u5929\u7684\u80a1\u7968\u4ef7\u683c\uff1b\u6574\u6570 <code>fee<\/code> \u4ee3\u8868\u4e86\u4ea4\u6613\u80a1\u7968\u7684\u624b\u7eed\u8d39\u7528\u3002\u4f60\u53ef\u4ee5\u65e0\u9650\u6b21\u5730\u5b8c\u6210\u4ea4\u6613\uff0c\u4f46\u662f\u4f60\u6bcf\u7b14\u4ea4\u6613\u90fd\u9700\u8981\u4ed8\u624b\u7eed\u8d39\u3002\u5982\u679c\u4f60\u5df2\u7ecf\u8d2d\u4e70\u4e86\u4e00\u4e2a\u80a1\u7968\uff0c\u5728\u5356\u51fa\u5b83\u4e4b\u524d\u4f60\u5c31\u4e0d\u80fd\u518d\u7ee7\u7eed\u8d2d\u4e70\u80a1\u7968\u4e86\u3002\u8fd4\u56de\u83b7\u5f97\u5229\u6da6\u7684\u6700\u5927\u503c\u3002<strong>\u6ce8\u610f<\/strong>\uff1a\u8fd9\u91cc\u7684\u4e00\u7b14\u4ea4\u6613\u6307\u4e70\u5165\u6301\u6709\u5e76\u5356\u51fa\u80a1\u7968\u7684\u6574\u4e2a\u8fc7\u7a0b\uff0c\u6bcf\u7b14\u4ea4\u6613\u4f60\u53ea\u9700\u8981\u4e3a\u652f\u4ed8\u4e00\u6b21\u624b\u7eed\u8d39\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u89e3\u9898\u601d\u8def<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u9053\u9898\u76ee\u4e0d\u542b\u51b7\u51bb\u671f\uff0c\u6240\u4ee5\u5c11\u4e86\u4e00\u4e2a\u51b7\u51bb\u671f\u7684\u72b6\u6001\u3002\u56e0\u6b64\uff0c\u53ea\u9700\u8981\u4e24\u79cd\u72b6\u6001\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dp0<\/code>\uff1a\u4e0d\u6301\u80a1<\/li>\n\n\n\n<li><code>dp1<\/code>\uff1a\u6301\u80a1<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define MAX(a, b) (a &gt; b ? a : b)\n\nint maxProfit(int* prices, int pricesSize, int fee) {\n    int dp0 = 0;              \/\/ \u4e0d\u6301\u80a1(\u7b2c\u4e00\u5929\u7ed3\u675f\u540e)\n    int dp1 = -prices&#91;0];     \/\/ \u6301\u80a1(\u7b2c\u4e00\u5929\u7ed3\u675f\u540e)\n\n    for (int i = 1; i &lt; pricesSize; i++) {\n        int new_dp0 = MAX(dp0, dp1 + prices&#91;i] - fee);\n        int new_dp1 = MAX(dp1, dp0 - prices&#91;i]);\n\n        dp0 = new_dp0;\n        dp1 = new_dp1;\n    }\n\n    return dp0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u72b6\u6001\u8f6c\u79fb<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"123\" src=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-11-1024x123.png\" alt=\"\" class=\"wp-image-42\" srcset=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-11-1024x123.png 1024w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-11-300x36.png 300w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-11-768x92.png 768w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/image-11.png 1030w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e09\u3001\u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a III<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u9898\u76ee\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5b83\u7684\u7b2c i \u4e2a\u5143\u7d20\u662f\u4e00\u652f\u7ed9\u5b9a\u7684\u80a1\u7968\u5728\u7b2c i \u5929\u7684\u4ef7\u683c\u3002\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u6765\u8ba1\u7b97\u4f60\u6240\u80fd\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002\u4f60\u6700\u591a\u53ef\u4ee5\u5b8c\u6210 <strong>\u4e24\u7b14<\/strong> \u4ea4\u6613\u3002<strong>\u6ce8\u610f<\/strong>\uff1a\u4f60\u4e0d\u80fd\u540c\u65f6\u53c2\u4e0e\u591a\u7b14\u4ea4\u6613\uff08\u4f60\u5fc5\u987b\u5728\u518d\u6b21\u8d2d\u4e70\u524d\u51fa\u552e\u6389\u4e4b\u524d\u7684\u80a1\u7968\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u89e3\u9898\u601d\u8def<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5f00\u59cb\u6211\u7684\u60f3\u6cd5\u662f\u5b9a\u4e49\u4e00\u4e2aflag1\uff0cfkag2\u6765\u8bb0\u5f55\u5229\u6da6\u7684\u6700\u5927\u503c\u548c\u6b21\u5927\u503c\uff0c\u52a8\u6001\u89c4\u5212\u5b8c\u6210\u540eflag1+flag2\u5c31\u662f\u7ed3\u679c\u3002\u4f46\u662f\u6211\u5ffd\u7565\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u6bd4\u5982\u7b2c\u4e00\u5929\u6211\u4e70\u5165\u4e86\u80a1\u7968\uff0c\u7b2c\u4e8c\u5929\u66f4\u65b0\u4e86\u6700\u5927\u503c\u548c\u6b21\u5927\u503c\uff0c\u7b2c\u4e09\u5929\u66f4\u65b0\u4e86\u6700\u5927\u503c\uff0c\u80fd\u7528\u6700\u5927\u503c\u548c\u6b21\u5927\u503c\u76f8\u52a0\u8868\u793a\u7ed3\u679c\u5417\uff1f\u7b54\u6848\u662f\u4e0d\u80fd\u7684\uff0c\u56e0\u4e3a\u7b2c\u4e00\u5929\u4e70\u7684\u80a1\u7968\u53ea\u80fd\u5356\u4e00\u6b21\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b63\u786e\u601d\u8def\uff1a\u5b9a\u4e49\u56db\u6b21\u72b6\u6001\u7684DP\uff0c\u8868\u793a\u7b2c\u4e00\u6b21\u548c\u7b2c\u4e8c\u6b21\u4ea4\u6613\u7684\u4e70\u5356\u72b6\u6001\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>buy1<\/code>\uff1a\u7b2c\u4e00\u6b21\u4e70\u5165\u540e\u7684\u6700\u5927\u6536\u76ca<\/li>\n\n\n\n<li><code>sell1<\/code>\uff1a\u7b2c\u4e00\u6b21\u5356\u51fa\u540e\u7684\u6700\u5927\u6536\u76ca<\/li>\n\n\n\n<li><code>buy2<\/code>\uff1a\u7b2c\u4e8c\u6b21\u4e70\u5165\u540e\u7684\u6700\u5927\u6536\u76ca<\/li>\n\n\n\n<li><code>sell2<\/code>\uff1a\u7b2c\u4e8c\u6b21\u5356\u51fa\u540e\u7684\u6700\u5927\u6536\u76ca<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define MAX(a, b) (a &gt; b ? a : b)\n\nint maxProfit(int* prices, int pricesSize) {\n    \/\/ \u521d\u59cb\u5316\u7b2c\u4e00\u5929\u7684\u72b6\u6001\n    int buy1 = -prices&#91;0];   \/\/ \u7b2c\u4e00\u5929\u4e70\u5165\n    int sell1 = 0;           \/\/ \u7b2c\u4e00\u5929\u4e70\u5165\u53c8\u5356\u51fa\n    int buy2 = -prices&#91;0];   \/\/ \u7b2c\u4e00\u6b21\u5356\u51fa\u540e\u7acb\u5373\u4e70\u5165\u7b2c\u4e8c\u6b21\n    int sell2 = 0;           \/\/ \u7b2c\u4e8c\u6b21\u5356\u51fa\n\n    for (int i = 1; i &lt; pricesSize; i++) {\n        \/\/ 1. \u7b2c\u4e00\u6b21\u6301\u6709\uff1a\u4ee5\u524d\u4e70\u7684 \u6216 \u4eca\u5929\u521a\u4e70\u5165\n        buy1 = MAX(buy1, 0 - prices&#91;i]);\n\n        \/\/ 2. \u7b2c\u4e00\u6b21\u4e0d\u6301\u6709\uff1a\u4ee5\u524d\u5356\u4e86 \u6216 \u4eca\u5929\u5356\u51fa\n        sell1 = MAX(sell1, buy1 + prices&#91;i]);\n\n        \/\/ 3. \u7b2c\u4e8c\u6b21\u6301\u6709\uff1a\u4ee5\u524d\u5c31\u6301\u6709 \u6216 \u4eca\u5929\u5728\u7b2c\u4e00\u6b21\u4ea4\u6613\u8d5a\u94b1\u7684\u57fa\u7840\u4e0a\u4e70\u5165\n        buy2 = MAX(buy2, sell1 - prices&#91;i]);\n\n        \/\/ 4. \u7b2c\u4e8c\u6b21\u4e0d\u6301\u6709\uff1a\u4ee5\u524d\u5c31\u5356\u4e86 \u6216 \u4eca\u5929\u5356\u51fa\n        sell2 = MAX(sell2, buy2 + prices&#91;i]);\n    }\n\n    return sell2;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u4e2d\u4f53\u73b0\u7684\u4e00\u4e2a\u70b9\u5f88\u91cd\u8981\uff0c\u5c31\u662fbuy2\u7684\u72b6\u6001\u662f\u4f9d\u9760sell1\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u6765\u7684\uff0c\u56e0\u6b64\u8fd9\u6837\u5c31\u7b26\u5408\u4e70\u4e86\u4e24\u6b21\uff0c\u8fd8\u7b26\u5408\u4e70\u7b2c\u4e8c\u6b21\u524d\u51fa\u552e\u6389\u7b2c\u4e00\u6b21\u7684\u80a1\u7968\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u72b6\u6001\u8f6c\u79fb<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"184\" src=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/QQ_1774338579988-1024x184.png\" alt=\"\" class=\"wp-image-43\" srcset=\"https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/QQ_1774338579988-1024x184.png 1024w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/QQ_1774338579988-300x54.png 300w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/QQ_1774338579988-768x138.png 768w, https:\/\/geocommunity.vip\/wp-content\/uploads\/2026\/03\/QQ_1774338579988.png 1034w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u56db\u3001\u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a IV<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u9898\u76ee\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>prices<\/code> \u548c\u4e00\u4e2a\u6574\u6570 <code>k<\/code>\uff0c\u5176\u4e2d <code>prices[i]<\/code> \u662f\u67d0\u652f\u7ed9\u5b9a\u7684\u80a1\u7968\u5728\u7b2c i \u5929\u7684\u4ef7\u683c\u3002\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u6765\u8ba1\u7b97\u4f60\u6240\u80fd\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002\u4f60\u6700\u591a\u53ef\u4ee5\u5b8c\u6210 <strong>k \u7b14<\/strong> \u4ea4\u6613\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u6700\u591a\u53ef\u4ee5\u4e70 k \u6b21\uff0c\u5356 k \u6b21\u3002<strong>\u6ce8\u610f<\/strong>\uff1a\u4f60\u4e0d\u80fd\u540c\u65f6\u53c2\u4e0e\u591a\u7b14\u4ea4\u6613\uff08\u4f60\u5fc5\u987b\u5728\u518d\u6b21\u8d2d\u4e70\u524d\u51fa\u552e\u6389\u4e4b\u524d\u7684\u80a1\u7968\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u89e3\u9898\u601d\u8def<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u9898\u76ee 3 \u7684\u903b\u8f91\u63a8\u5e7f\u5230 k \u6b21\u4ea4\u6613\uff0c\u4f7f\u7528\u6570\u7ec4\u5b58\u50a8\u6bcf\u6b21\u4ea4\u6613\u7684\u4e70\u5356\u72b6\u6001\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>buy[j]<\/code>\uff1a\u7b2c j \u6b21\u4e70\u5165\u540e\u7684\u6700\u5927\u6536\u76ca<\/li>\n\n\n\n<li><code>sell[j]<\/code>\uff1a\u7b2c j \u6b21\u5356\u51fa\u540e\u7684\u6700\u5927\u6536\u76ca<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdlib.h&gt;\n\n#define MAX(a, b) ((a) &gt; (b) ? (a) : (b))\n\nint maxProfit(int k, int* prices, int pricesSize) {\n    int* buy = (int*)malloc(sizeof(int) * (k + 1));\n    int* sell = (int*)malloc(sizeof(int) * (k + 1));\n\n    sell&#91;0] = 0;  \/\/ \u8fd8\u6ca1\u5f00\u59cb\u4ea4\u6613\uff0c\u515c\u91cc\u53ea\u6709 0 \u5143\n\n    \/\/ \u7b2c\u4e00\u5929\u65f6\u6240\u6709\u4ea4\u6613\u6b21\u6570\u540e\u7684\u4e70\/\u5356\u540e\u7684\u91d1\u989d\n    for (int j = 1; j &lt;= k; j++) {\n        buy&#91;j] = -prices&#91;0];\n        sell&#91;j] = 0;\n    }\n\n    for (int i = 1; i &lt; pricesSize; i++) {\n        for (int j = 1; j &lt;= k; j++) {\n            \/\/ \u7b2c j \u6b21\u6301\u6709\uff1a\u53d6\u51b3\u4e8e\u4e4b\u524d\u5c31\u6301\u6709\u6216\u8005\u4eca\u5929\u521a\u4e70\n            buy&#91;j] = MAX(buy&#91;j], sell&#91;j - 1] - prices&#91;i]);\n\n            \/\/ \u7b2c j \u6b21\u5356\u51fa\uff1a\u53d6\u51b3\u4e8e\u4e4b\u524d\u5c31\u5356\u4e86\u6216\u8005\u4eca\u5929\u521a\u5356\n            sell&#91;j] = MAX(sell&#91;j], buy&#91;j] + prices&#91;i]);\n        }\n    }\n\n    int result = sell&#91;k];\n\n    free(buy);\n    free(sell);\n    return result;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u6838\u5fc3\u601d\u60f3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e\u53ea\u80fd\u8fdb\u884c\u4e24\u6b21\u4ea4\u6613\u7684\u4ee3\u7801\u903b\u8f91\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u7528\u6570\u7ec4\u4ee3\u66ff\u4e86\u591a\u4e2a\u53d8\u91cf\u3002\u7528 for \u5faa\u73af\u904d\u5386 k \u6b21\uff0c\u53ef\u4ee5\u770b\u4f5c\u662f <code>buy[1], sell[1], buy[2], sell[2], ..., buy[k], sell[k]<\/code> \u7684\u901a\u7528\u5316\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u52a8\u6001\u89c4\u5212\u7c7b\u95ee\u9898\uff0c\u53ea\u9700\u8981\u4f9d\u6b21\u786e\u5b9adp\u6570\u7ec4\u4ee5\u53cadp[i &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],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":2,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}