C++實(shí)現(xiàn)LeetCode(134.加油站問(wèn)題)
[LeetCode] 134.Gas Station 加油站問(wèn)題
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
這道轉(zhuǎn)圈加油問(wèn)題不算很難,只要想通其中的原理就很簡(jiǎn)單。我們首先要知道能走完整個(gè)環(huán)的前提是gas的總量要大于cost的總量,這樣才會(huì)有起點(diǎn)的存在。假設(shè)開始設(shè)置起點(diǎn)start = 0, 并從這里出發(fā),如果當(dāng)前的gas值大于cost值,就可以繼續(xù)前進(jìn),此時(shí)到下一個(gè)站點(diǎn),剩余的gas加上當(dāng)前的gas再減去cost,看是否大于0,若大于0,則繼續(xù)前進(jìn)。當(dāng)?shù)竭_(dá)某一站點(diǎn)時(shí),若這個(gè)值小于0了,則說(shuō)明從起點(diǎn)到這個(gè)點(diǎn)中間的任何一個(gè)點(diǎn)都不能作為起點(diǎn),則把起點(diǎn)設(shè)為下一個(gè)點(diǎn),繼續(xù)遍歷。當(dāng)遍歷完整個(gè)環(huán)時(shí),當(dāng)前保存的起點(diǎn)即為所求。代碼如下:
解法一:
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int total = 0, sum = 0, start = 0;
for (int i = 0; i < gas.size(); ++i) {
total += gas[i] - cost[i];
sum += gas[i] - cost[i];
if (sum < 0) {
start = i + 1;
sum = 0;
}
}
return (total < 0) ? -1 : start;
}
};
我們也可以從后往前遍歷,用一個(gè)變量mx來(lái)記錄出現(xiàn)過(guò)的剩余油量的最大值,total記錄當(dāng)前剩余油量的值,start還是記錄起點(diǎn)的位置。當(dāng)total大于mx的時(shí)候,說(shuō)明當(dāng)前位置可以作為起點(diǎn),更新start,并且更新mx。為啥呢?因?yàn)槲覀兠看蝨otal加上的都是當(dāng)前位置的油量減去消耗,如果這個(gè)差值大于0的話,說(shuō)明當(dāng)前位置可以當(dāng)作起點(diǎn),因?yàn)閺漠?dāng)前位置到末尾都不會(huì)出現(xiàn)油量不夠的情況,而一旦差值小于0的話,說(shuō)明當(dāng)前位置如果是起點(diǎn)的話,油量就不夠,無(wú)法走完全程,所以我們不更新起點(diǎn)位置start。最后結(jié)束后我們還是看totoa是否大于等于0,如果其小于0的話,說(shuō)明沒(méi)有任何一個(gè)起點(diǎn)能走完全程,因?yàn)榭傆土慷疾粔?,參見代碼如下:
解法二:
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int total = 0, mx = -1, start = 0;
for (int i = gas.size() - 1; i >= 0; --i) {
total += gas[i] - cost[i];
if (total > mx) {
start = i;
mx = total;
}
}
return (total < 0) ? -1 : start;
}
};
類似題目:
Cheapest Flights Within K Stops
參考資料:
https://leetcode.com/problems/gas-station/discuss/42568/Share-some-of-my-ideas.
https://leetcode.com/problems/gas-station/discuss/42656/8ms-simple-O(n)-c++-solution
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(134.加油站問(wèn)題)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)加油站問(wèn)題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(148.鏈表排序)
- C++實(shí)現(xiàn)LeetCode(147.鏈表插入排序)
- C++實(shí)現(xiàn)LeetCode(146.近最少使用頁(yè)面置換緩存器)
- C++實(shí)現(xiàn)LeetCode(140.拆分詞句之二)
- C++實(shí)現(xiàn)LeetCode(135.分糖果問(wèn)題)
- C++實(shí)現(xiàn)LeetCode(138.拷貝帶有隨機(jī)指針的鏈表)
- C++實(shí)現(xiàn)LeetCode(133.克隆無(wú)向圖)
- C++實(shí)現(xiàn)LeetCode(149.共線點(diǎn)個(gè)數(shù))
相關(guān)文章
c++報(bào)錯(cuò)問(wèn)題解決方案lvalue required as left opera
這篇文章主要介紹了c++報(bào)錯(cuò):lvalue required as left operand of assignment,出現(xiàn)此錯(cuò)誤原因,是因?yàn)?,等?hào)左邊是不可被修改的表達(dá)式或常量,而表達(dá)式或常量不能作為左值,需要的朋友可以參考下2023-01-01
Cocos2d-x 3.x入門教程(二):Node節(jié)點(diǎn)類
這篇文章主要介紹了Cocos2d-x 3.x入門教程(二):Node節(jié)點(diǎn)類,本文對(duì)Node節(jié)點(diǎn)類做了一個(gè)簡(jiǎn)明講解及Node類提供的函數(shù)做了說(shuō)明,需要的朋友可以參考下2014-11-11
C語(yǔ)言初學(xué)者代碼中的常見錯(cuò)誤與問(wèn)題
C語(yǔ)言初學(xué)者犯過(guò)的很多錯(cuò)誤都非常典型,在初學(xué)者中非常普遍,于是整理了一下,應(yīng)該對(duì)其他初學(xué)者有借鑒意義2013-11-11
C語(yǔ)言進(jìn)階數(shù)據(jù)的存儲(chǔ)機(jī)制完整版
這篇文章主要為大家完整的介紹了C語(yǔ)言進(jìn)階數(shù)據(jù)的存儲(chǔ)機(jī)制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02
c++優(yōu)先隊(duì)列(priority_queue)用法詳解
這篇文章主要介紹了c++優(yōu)先隊(duì)列(priority_queue)用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

