{"id":54033,"date":"2025-08-12T21:09:47","date_gmt":"2025-08-12T13:09:47","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/54033.html"},"modified":"2025-08-12T21:09:47","modified_gmt":"2025-08-12T13:09:47","slug":"%e3%80%90leetcode%e3%80%91134-%e5%8a%a0%e6%b2%b9%e7%ab%99","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/54033.html","title":{"rendered":"\u3010leetcode\u3011134. \u52a0\u6cb9\u7ad9"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\n<ul>\n<li>\u9898\u76ee<\/li>\n<li>\u9898\u89e3<\/li>\n<li>\n<ul>\n<li>1. \u66b4\u529b\u6c42\u89e3<\/li>\n<li>2. \u8d2a\u5fc3<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u9898\u76ee<\/h3>\n<p>134. \u52a0\u6cb9\u7ad9<\/p>\n<p>\u5728\u4e00\u6761\u73af\u8def\u4e0a\u6709 n \u4e2a\u52a0\u6cb9\u7ad9&#xff0c;\u5176\u4e2d\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u6709\u6c7d\u6cb9 gas[i] \u5347\u3002<\/p>\n<p>\u4f60\u6709\u4e00\u8f86\u6cb9\u7bb1\u5bb9\u91cf\u65e0\u9650\u7684\u7684\u6c7d\u8f66&#xff0c;\u4ece\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u5f00\u5f80\u7b2c i&#043;1 \u4e2a\u52a0\u6cb9\u7ad9\u9700\u8981\u6d88\u8017\u6c7d\u6cb9 cost[i] \u5347\u3002\u4f60\u4ece\u5176\u4e2d\u7684\u4e00\u4e2a\u52a0\u6cb9\u7ad9\u51fa\u53d1&#xff0c;\u5f00\u59cb\u65f6\u6cb9\u7bb1\u4e3a\u7a7a\u3002<\/p>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u6574\u6570\u6570\u7ec4 gas \u548c cost &#xff0c;\u5982\u679c\u4f60\u53ef\u4ee5\u6309\u987a\u5e8f\u7ed5\u73af\u8def\u884c\u9a76\u4e00\u5468&#xff0c;\u5219\u8fd4\u56de\u51fa\u53d1\u65f6\u52a0\u6cb9\u7ad9\u7684\u7f16\u53f7&#xff0c;\u5426\u5219\u8fd4\u56de -1 \u3002\u5982\u679c\u5b58\u5728\u89e3&#xff0c;\u5219 \u4fdd\u8bc1 \u5b83\u662f \u552f\u4e00 \u7684\u3002<\/p>\n<p>\u793a\u4f8b 1:<\/p>\n<p>\u8f93\u5165: gas &#061; [1,2,3,4,5], cost &#061; [3,4,5,1,2] \u8f93\u51fa: 3 \u89e3\u91ca: \u4ece 3 \u53f7\u52a0\u6cb9\u7ad9(\u7d22\u5f15\u4e3a 3 \u5904)\u51fa\u53d1&#xff0c;\u53ef\u83b7\u5f97 4 \u5347\u6c7d\u6cb9\u3002\u6b64\u65f6\u6cb9\u7bb1\u6709 &#061; 0 &#043; 4 &#061; 4 \u5347\u6c7d\u6cb9 \u5f00\u5f80 4 \u53f7\u52a0\u6cb9\u7ad9&#xff0c;\u6b64\u65f6\u6cb9\u7bb1\u6709 4 &#8211; 1 &#043; 5 &#061; 8 \u5347\u6c7d\u6cb9 \u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9&#xff0c;\u6b64\u65f6\u6cb9\u7bb1\u6709 8 &#8211; 2 &#043; 1 &#061; 7 \u5347\u6c7d\u6cb9 \u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9&#xff0c;\u6b64\u65f6\u6cb9\u7bb1\u6709 7 &#8211; 3 &#043; 2 &#061; 6 \u5347\u6c7d\u6cb9 \u5f00\u5f80 2 \u53f7\u52a0\u6cb9\u7ad9&#xff0c;\u6b64\u65f6\u6cb9\u7bb1\u6709 6 &#8211; 4 &#043; 3 &#061; 5 \u5347\u6c7d\u6cb9 \u5f00\u5f80 3 \u53f7\u52a0\u6cb9\u7ad9&#xff0c;\u4f60\u9700\u8981\u6d88\u8017 5 \u5347\u6c7d\u6cb9&#xff0c;\u6b63\u597d\u8db3\u591f\u4f60\u8fd4\u56de\u5230 3 \u53f7\u52a0\u6cb9\u7ad9\u3002 \u56e0\u6b64&#xff0c;3 \u53ef\u4e3a\u8d77\u59cb\u7d22\u5f15\u3002<\/p>\n<p>\u793a\u4f8b 2:<\/p>\n<p>\u8f93\u5165: gas &#061; [2,3,4], cost &#061; [3,4,3] \u8f93\u51fa: -1 \u89e3\u91ca: \u4f60\u4e0d\u80fd\u4ece 0 \u53f7\u6216 1 \u53f7\u52a0\u6cb9\u7ad9\u51fa\u53d1&#xff0c;\u56e0\u4e3a\u6ca1\u6709\u8db3\u591f\u7684\u6c7d\u6cb9\u53ef\u4ee5\u8ba9\u4f60\u884c\u9a76\u5230\u4e0b\u4e00\u4e2a\u52a0\u6cb9\u7ad9\u3002 \u6211\u4eec\u4ece 2 \u53f7\u52a0\u6cb9\u7ad9\u51fa\u53d1&#xff0c;\u53ef\u4ee5\u83b7\u5f97 4 \u5347\u6c7d\u6cb9\u3002 \u6b64\u65f6\u6cb9\u7bb1\u6709 &#061; 0 &#043; 4 &#061; 4 \u5347\u6c7d\u6cb9 \u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9&#xff0c;\u6b64\u65f6\u6cb9\u7bb1\u6709 4 &#8211; 3 &#043; 2 &#061; 3 \u5347\u6c7d\u6cb9 \u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9&#xff0c;\u6b64\u65f6\u6cb9\u7bb1\u6709 3 &#8211; 3 &#043; 3 &#061; 3 \u5347\u6c7d\u6cb9 \u4f60\u65e0\u6cd5\u8fd4\u56de 2 \u53f7\u52a0\u6cb9\u7ad9&#xff0c;\u56e0\u4e3a\u8fd4\u7a0b\u9700\u8981\u6d88\u8017 4 \u5347\u6c7d\u6cb9&#xff0c;\u4f46\u662f\u4f60\u7684\u6cb9\u7bb1\u53ea\u6709 3 \u5347\u6c7d\u6cb9\u3002 \u56e0\u6b64&#xff0c;\u65e0\u8bba\u600e\u6837&#xff0c;\u4f60\u90fd\u4e0d\u53ef\u80fd\u7ed5\u73af\u8def\u884c\u9a76\u4e00\u5468\u3002<\/p>\n<h3>\u9898\u89e3<\/h3>\n<h4>1. \u66b4\u529b\u6c42\u89e3<\/h4>\n<p>35 \/ 39 \u4e2a\u901a\u8fc7\u7684\u6d4b\u8bd5\u7528\u4f8b<\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Solution<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">object<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">canCompleteCircuit<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> gas<span class=\"token punctuation\">,<\/span> cost<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;<br \/>\n        :type gas: List[int]<br \/>\n        :type cost: List[int]<br \/>\n        :rtype: int<br \/>\n        &#034;&#034;&#034;<\/span><br \/>\n        <span class=\"token comment\"># \u66b4\u529b\u6cd5<\/span><br \/>\n        n <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">len<\/span><span class=\"token punctuation\">(<\/span>cost<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> i <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span>n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            rest <span class=\"token operator\">&#061;<\/span> gas<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#8211;<\/span> cost<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><br \/>\n            index <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>i <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">%<\/span> n<\/p>\n<p>            <span class=\"token keyword\">while<\/span> rest <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span> <span class=\"token keyword\">and<\/span> index <span class=\"token operator\">!&#061;<\/span> i<span class=\"token punctuation\">:<\/span><br \/>\n                rest <span class=\"token operator\">&#043;&#061;<\/span> gas<span class=\"token punctuation\">[<\/span>index<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#8211;<\/span> cost<span class=\"token punctuation\">[<\/span>index<span class=\"token punctuation\">]<\/span><br \/>\n                index <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>index <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">%<\/span> n<\/p>\n<p>            <span class=\"token keyword\">if<\/span> rest <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">0<\/span> <span class=\"token keyword\">and<\/span> index <span class=\"token operator\">&#061;&#061;<\/span> i<span class=\"token punctuation\">:<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> i<\/p>\n<p>        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><\/p>\n<h4>2. \u8d2a\u5fc3<\/h4>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Solution<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">object<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">canCompleteCircuit<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> gas<span class=\"token punctuation\">,<\/span> cost<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;<br \/>\n        :type gas: List[int]<br \/>\n        :type cost: List[int]<br \/>\n        :rtype: int<br \/>\n        &#034;&#034;&#034;<\/span><br \/>\n        <span class=\"token comment\"># \u8d2a\u5fc3<\/span><br \/>\n        curSum <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><br \/>\n        totalSum <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><br \/>\n        start <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><\/p>\n<p>        <span class=\"token keyword\">for<\/span> i <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">len<\/span><span class=\"token punctuation\">(<\/span>gas<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            curSum <span class=\"token operator\">&#043;&#061;<\/span> gas<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#8211;<\/span> cost<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><br \/>\n            totalSum <span class=\"token operator\">&#043;&#061;<\/span> gas<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#8211;<\/span> cost<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><\/p>\n<p>            <span class=\"token keyword\">if<\/span> curSum <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">:<\/span><br \/>\n                start <span class=\"token operator\">&#061;<\/span> i <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><br \/>\n                curSum <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><\/p>\n<p>        <span class=\"token keyword\">if<\/span> totalSum <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><\/p>\n<p>        <span class=\"token keyword\">return<\/span> start<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2\u6b21\u3002\u4f60\u6709\u4e00\u8f86\u6cb9\u7bb1\u5bb9\u91cf\u65e0\u9650\u7684\u7684\u6c7d\u8f66\uff0c\u4ece\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u5f00\u5f80\u7b2c i+1 \u4e2a\u52a0\u6cb9\u7ad9\u9700\u8981\u6d88\u8017\u6c7d\u6cb9 cost[i] \u5347\u3002\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\u8f93\u5165: gas = [1,2,3,4,5], cost = [3,4,5,1,2]\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\u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 4 &#8211; 3 + 2 = 3 \u5347\u6c7d\u6cb9\u3002\u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 3 &#8211; 3 + 3 = 3 \u5347\u6c7d\u6cb9\u3002\u56e0\u6b64\uff0c3 \u53ef\u4e3a\u8d77\u59cb\u7d22\u5f15\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2692,427,925],"topic":[],"class_list":["post-54033","post","type-post","status-publish","format-standard","hentry","category-server","tag-leetcode","tag-427","tag-925"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010leetcode\u3011134. \u52a0\u6cb9\u7ad9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/54033.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010leetcode\u3011134. \u52a0\u6cb9\u7ad9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2\u6b21\u3002\u4f60\u6709\u4e00\u8f86\u6cb9\u7bb1\u5bb9\u91cf\u65e0\u9650\u7684\u7684\u6c7d\u8f66\uff0c\u4ece\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u5f00\u5f80\u7b2c i+1 \u4e2a\u52a0\u6cb9\u7ad9\u9700\u8981\u6d88\u8017\u6c7d\u6cb9 cost[i] \u5347\u3002\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\u8f93\u5165: gas = [1,2,3,4,5], cost = [3,4,5,1,2]\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\u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 4 - 3 + 2 = 3 \u5347\u6c7d\u6cb9\u3002\u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 3 - 3 + 3 = 3 \u5347\u6c7d\u6cb9\u3002\u56e0\u6b64\uff0c3 \u53ef\u4e3a\u8d77\u59cb\u7d22\u5f15\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/54033.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-12T13:09:47+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/54033.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/54033.html\",\"name\":\"\u3010leetcode\u3011134. \u52a0\u6cb9\u7ad9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-12T13:09:47+00:00\",\"dateModified\":\"2025-08-12T13:09:47+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/54033.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/54033.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/54033.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010leetcode\u3011134. \u52a0\u6cb9\u7ad9\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010leetcode\u3011134. \u52a0\u6cb9\u7ad9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/54033.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010leetcode\u3011134. \u52a0\u6cb9\u7ad9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2\u6b21\u3002\u4f60\u6709\u4e00\u8f86\u6cb9\u7bb1\u5bb9\u91cf\u65e0\u9650\u7684\u7684\u6c7d\u8f66\uff0c\u4ece\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u5f00\u5f80\u7b2c i+1 \u4e2a\u52a0\u6cb9\u7ad9\u9700\u8981\u6d88\u8017\u6c7d\u6cb9 cost[i] \u5347\u3002\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\u8f93\u5165: gas = [1,2,3,4,5], cost = [3,4,5,1,2]\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\u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 4 - 3 + 2 = 3 \u5347\u6c7d\u6cb9\u3002\u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 3 - 3 + 3 = 3 \u5347\u6c7d\u6cb9\u3002\u56e0\u6b64\uff0c3 \u53ef\u4e3a\u8d77\u59cb\u7d22\u5f15\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/54033.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-12T13:09:47+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/54033.html","url":"https:\/\/www.wsisp.com\/helps\/54033.html","name":"\u3010leetcode\u3011134. \u52a0\u6cb9\u7ad9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-12T13:09:47+00:00","dateModified":"2025-08-12T13:09:47+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/54033.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/54033.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/54033.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010leetcode\u3011134. \u52a0\u6cb9\u7ad9"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/54033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=54033"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/54033\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=54033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=54033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=54033"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=54033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}