{"id":329,"date":"2025-07-01T13:50:45","date_gmt":"2025-07-01T05:50:45","guid":{"rendered":"http:\/\/getzs.online\/?p=329"},"modified":"2025-07-01T13:50:45","modified_gmt":"2025-07-01T05:50:45","slug":"134-%e5%8a%a0%e6%b2%b9%e7%ab%99","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/07\/01\/134-%e5%8a%a0%e6%b2%b9%e7%ab%99\/","title":{"rendered":"134. \u52a0\u6cb9\u7ad9"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/gas-station\/\">134. \u52a0\u6cb9\u7ad9<\/a><\/p>\n\n\n\n<p>\u5728\u4e00\u6761\u73af\u8def\u4e0a\u6709&nbsp;<code>n<\/code>&nbsp;\u4e2a\u52a0\u6cb9\u7ad9\uff0c\u5176\u4e2d\u7b2c&nbsp;<code>i<\/code>&nbsp;\u4e2a\u52a0\u6cb9\u7ad9\u6709\u6c7d\u6cb9&nbsp;<code>gas[i]<\/code><em>&nbsp;<\/em>\u5347\u3002<\/p>\n\n\n\n<p>\u4f60\u6709\u4e00\u8f86\u6cb9\u7bb1\u5bb9\u91cf\u65e0\u9650\u7684\u7684\u6c7d\u8f66\uff0c\u4ece\u7b2c<em>&nbsp;<\/em><code>i<\/code><em>&nbsp;<\/em>\u4e2a\u52a0\u6cb9\u7ad9\u5f00\u5f80\u7b2c<em>&nbsp;<\/em><code>i+1<\/code><em>&nbsp;<\/em>\u4e2a\u52a0\u6cb9\u7ad9\u9700\u8981\u6d88\u8017\u6c7d\u6cb9&nbsp;<code>cost[i]<\/code><em>&nbsp;<\/em>\u5347\u3002\u4f60\u4ece\u5176\u4e2d\u7684\u4e00\u4e2a\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u5f00\u59cb\u65f6\u6cb9\u7bb1\u4e3a\u7a7a\u3002<\/p>\n\n\n\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>gas<\/code>&nbsp;\u548c&nbsp;<code>cost<\/code>&nbsp;\uff0c\u5982\u679c\u4f60\u53ef\u4ee5\u6309\u987a\u5e8f\u7ed5\u73af\u8def\u884c\u9a76\u4e00\u5468\uff0c\u5219\u8fd4\u56de\u51fa\u53d1\u65f6\u52a0\u6cb9\u7ad9\u7684\u7f16\u53f7\uff0c\u5426\u5219\u8fd4\u56de&nbsp;<code>-1<\/code>&nbsp;\u3002\u5982\u679c\u5b58\u5728\u89e3\uff0c\u5219&nbsp;<strong>\u4fdd\u8bc1<\/strong>&nbsp;\u5b83\u662f&nbsp;<strong>\u552f\u4e00<\/strong>&nbsp;\u7684\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong> gas = [1,2,3,4,5], cost = [3,4,5,1,2]\n<strong>\u8f93\u51fa:<\/strong> 3\n<strong>\u89e3\u91ca:\n<\/strong>\u4ece 3 \u53f7\u52a0\u6cb9\u7ad9(\u7d22\u5f15\u4e3a 3 \u5904)\u51fa\u53d1\uff0c\u53ef\u83b7\u5f97 4 \u5347\u6c7d\u6cb9\u3002\u6b64\u65f6\u6cb9\u7bb1\u6709 = 0 + 4 = 4 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 4 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 4 - 1 + 5 = 8 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 8 - 2 + 1 = 7 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 7 - 3 + 2 = 6 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 2 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 6 - 4 + 3 = 5 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 3 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u4f60\u9700\u8981\u6d88\u8017 5 \u5347\u6c7d\u6cb9\uff0c\u6b63\u597d\u8db3\u591f\u4f60\u8fd4\u56de\u5230 3 \u53f7\u52a0\u6cb9\u7ad9\u3002\n\u56e0\u6b64\uff0c3 \u53ef\u4e3a\u8d77\u59cb\u7d22\u5f15\u3002<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong> gas = [2,3,4], cost = [3,4,3]\n<strong>\u8f93\u51fa:<\/strong> -1\n<strong>\u89e3\u91ca:\n<\/strong>\u4f60\u4e0d\u80fd\u4ece 0 \u53f7\u6216 1 \u53f7\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u56e0\u4e3a\u6ca1\u6709\u8db3\u591f\u7684\u6c7d\u6cb9\u53ef\u4ee5\u8ba9\u4f60\u884c\u9a76\u5230\u4e0b\u4e00\u4e2a\u52a0\u6cb9\u7ad9\u3002\n\u6211\u4eec\u4ece 2 \u53f7\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u53ef\u4ee5\u83b7\u5f97 4 \u5347\u6c7d\u6cb9\u3002 \u6b64\u65f6\u6cb9\u7bb1\u6709 = 0 + 4 = 4 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 4 - 3 + 2 = 3 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 3 - 3 + 3 = 3 \u5347\u6c7d\u6cb9\n\u4f60\u65e0\u6cd5\u8fd4\u56de 2 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u56e0\u4e3a\u8fd4\u7a0b\u9700\u8981\u6d88\u8017 4 \u5347\u6c7d\u6cb9\uff0c\u4f46\u662f\u4f60\u7684\u6cb9\u7bb1\u53ea\u6709 3 \u5347\u6c7d\u6cb9\u3002\n\u56e0\u6b64\uff0c\u65e0\u8bba\u600e\u6837\uff0c\u4f60\u90fd\u4e0d\u53ef\u80fd\u7ed5\u73af\u8def\u884c\u9a76\u4e00\u5468\u3002<\/pre>\n\n\n\n<p><strong>\u63d0\u793a:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>n == gas.length == cost.length<\/code><\/li>\n\n\n\n<li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li>\n\n\n\n<li><code>0 &lt;= gas[i], cost[i] &lt;= 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li>\u8f93\u5165\u4fdd\u8bc1\u7b54\u6848\u552f\u4e00\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    public int canCompleteCircuit(int&#91;] gas, int&#91;] cost) {\n        return method1(gas, cost, gas.length);\n    }\n\n    \/**\n     *  \u66b4\u529b\u89e3\u6cd5\n     *  \u8d85\u51fa\u65f6\u95f4\u9650\u5236\n     *  \u590d\u6742\u5ea6O(n^2)\n     *\/\n    public int method1(int&#91;] gas, int&#91;] cost, int n) {\n        for (int i = 0 ; i &lt; n ; i++) {\n            int sum = 0;\n            for (int j = i ; j &lt; i + n ; j++) {\n                int index = j % n;\n                sum += gas&#91;index] - cost&#91;index];\n                if (sum &lt; 0) {\n                    break;\n                }\n                if (j == i + n - 1) {\n                    return i;\n                }\n            }\n        }\n        return -1;\n    }\n\n\n    \/**\n     *  \u8d2a\u5fc3\u7b97\u6cd5\n     *  1 ms\n     *  \u590d\u6742\u5ea6O(n)\n     *\/\n    public int method2(int&#91;] gas, int&#91;] cost, int n) {\n        \/*\n         * \u5982\u679cgas\u548c\u5927\u4e8ecost\u548c\u5c31\u4e00\u5b9a\u80fd\u5230\u8fbe\u3002\n         * \u627e\u5230\u6b20\u6700\u591a\u6cb9\u7684index\uff0c\u7136\u540e\u4e0b\u4e00\u4e2a\u5c31\u662f\u51fa\u53d1\u70b9\u3002\n         *\/\n        int sum, index, mn;\n        sum = index = mn = 0;\n        for (int i = 0 ; i &lt; n ; i++) {\n            \/\/ \u83b7\u53d6\u4ece\u5934\u5230\u5c3e\u7684\u5dee\u503c\n            sum += gas&#91;i] - cost&#91;i];\n            \/\/ \u8ba1\u7b97\u6700\u5c0f\u5dee\u503c\uff0c\u5982\u679c\u6700\u540e\u7684\u5269\u4f59\u503c\u5927\u4e8e0\uff0c\u8868\u793a\u5f53\u524d\u70b9\u53ef\u4ee5\u8986\u76d6\u524d\u9762\u7684\u8d1f\u503c\n            if (sum &lt; mn) {\n                index = i + 1;\n                mn = sum;\n            }\n        }\n        return sum &lt; 0 ? -1 : index;\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>134. \u52a0\u6cb9\u7ad9 \u5728\u4e00\u6761\u73af\u8def\u4e0a\u6709&nbsp;n&nbsp;\u4e2a\u52a0\u6cb9\u7ad9\uff0c\u5176\u4e2d\u7b2c&nbsp;i&nbsp;\u4e2a\u52a0\u6cb9\u7ad9\u6709 [&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-329","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/329","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=329"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/329\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}