{"id":2159,"date":"2016-09-01T23:31:45","date_gmt":"2016-09-01T15:31:45","guid":{"rendered":"http:\/\/boweihe.me\/?p=2159"},"modified":"2016-09-01T23:31:45","modified_gmt":"2016-09-01T15:31:45","slug":"leetcode-134-gas-station","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2159","title":{"rendered":"LeetCode 134. Gas Station"},"content":{"rendered":"<h3>\u601d\u8def<\/h3>\n<p>O(n^2)\u505a\u6cd5\uff1a\u76f4\u63a5\u5d4c\u5957\u5faa\u73af\uff0c\u66b4\u529b\u8bd5\u5c31\u884c<br \/>\nO(n)\u505a\u6cd5\uff1a\u7c7b\u4f3c\u8d2a\u5fc3\u7684\u601d\u8def\u3002\u4e3b\u8981\u8fd8\u662f\u770bgas[i]-cost[i]\uff0c\u5982\u679c\u51cf\u51fa\u6765\u662f\u6b63\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u4e3a\u540e\u9762\u7684\u8def\u6bb5\u7559\u4e0b\u70b9\u8d44\u6e90\uff0c\u5982\u679c\u51cf\u51fa\u6765\u662f\u8d1f\u7684\u8bdd\uff0c\u90a3\u4e48\u6700\u597d\u7684\u505a\u6cd5\u662f\u5728\u4e4b\u524d\u79ef\u7d2f\u6700\u591a\u7684\u8d44\u6e90\u6765\u89e3\u51b3\u8fd9\u4e2adebt. \u90a3\u4ec0\u4e48\u65f6\u5019\u6709\u53ef\u80fd\u79ef\u7d2f\u5230\u6700\u591a\u8d44\u6e90\u5462\uff1f\u53ea\u80fd\u5c06\u8fd9\u4e2a\u6709\u6b20\u503a\u7684\u7ad9\u70b9\u4f5c\u4e3a\u6700\u540e\u4e00\u7ad9\uff0c\u6240\u4ee5\u5c06\u8be5\u7ad9\u70b9\u7684\u4e0b\u4e00\u7ad9\u4f5c\u4e3a\u8d77\u59cb\u8bd5\u8bd5\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    int canCompleteCircuit(vector&lt;int&gt;&amp; gas, vector&lt;int&gt;&amp; cost) {\n        int totalLoss = 0;\n        int tank = 0;\n        int max_i = 0;\n        for(int i=0; i&lt;gas.size(); i++){\n            tank += gas[i] - cost[i];\n            if(tank &lt; 0){   \/\/Owe!\n                max_i = i+1; \/\/next possible start\n                totalLoss += tank;  \/\/add in the debt\n                tank = 0;\n            }\n        }\n        if(totalLoss + tank &lt; 0)    \/\/if the debt cannot be paid\n            return -1;\n        return max_i;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8def O(n^2)\u505a\u6cd5\uff1a\u76f4\u63a5\u5d4c\u5957\u5faa\u73af\uff0c\u66b4\u529b\u8bd5\u5c31\u884c O(n)\u505a\u6cd5\uff1a\u7c7b\u4f3c\u8d2a\u5fc3\u7684\u601d\u8def\u3002\u4e3b\u8981\u8fd8\u662f\u770bgas[i]-cost[i]\uff0c\u5982\u679c\u51cf\u51fa\u6765\u662f\u6b63\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u4e3a\u540e\u9762\u7684\u8def\u6bb5\u7559\u4e0b\u70b9\u8d44\u6e90\uff0c\u5982\u679c\u51cf\u51fa\u6765\u662f\u8d1f\u7684\u8bdd\uff0c\u90a3\u4e48\u6700\u597d\u7684\u505a\u6cd5\u662f\u5728\u4e4b\u524d\u79ef\u7d2f\u6700\u591a\u7684\u8d44\u6e90\u6765\u89e3\u51b3\u8fd9\u4e2adebt. \u90a3\u4ec0\u4e48\u65f6\u5019\u6709\u53ef\u80fd\u79ef\u7d2f\u5230\u6700\u591a\u8d44\u6e90\u5462\uff1f\u53ea\u80fd\u5c06\u8fd9\u4e2a\u6709\u6b20\u503a\u7684\u7ad9\u70b9\u4f5c\u4e3a\u6700\u540e\u4e00\u7ad9\uff0c\u6240\u4ee5\u5c06\u8be5\u7ad9\u70b9\u7684\u4e0b\u4e00\u7ad9\u4f5c\u4e3a\u8d77\u59cb\u8bd5\u8bd5\u3002 \u4ee3\u7801 class Solution { public: int canCompleteCircuit(vector&lt;int&gt;&amp; gas, vector&lt;int&gt;&amp; cost) { int totalLoss = 0; int tank = 0; int max_i = 0; for(int i=0; i&lt;gas.size(); i++){ tank += gas[i] &#8211; cost[i]; if(tank &lt; 0){ \/\/Owe! max_i = i+1; \/\/next possible start totalLoss += tank; \/\/add in the debt tank = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-2159","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2159"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2159\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}