日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

几道加油站加油相关问题:最小加油次数、能否回到起点

發(fā)布時(shí)間:2024/1/8 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 几道加油站加油相关问题:最小加油次数、能否回到起点 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

幾道加油站加油相關(guān)問題

1.選一個(gè)加油站能走完一圈:leetcode134. Gas Station
2.加油最少次數(shù)(easy)
3.加油最少次數(shù)(hard):leetcode871. Minimum Number of Refueling Stops

題目:1.leetcode134. Gas Station

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 in the clockwise direction, otherwise return -1.

Note:

If there exists a solution, it is guaranteed to be unique.
Both input arrays are non-empty and have the same length.
Each element in the input arrays is a non-negative integer.
Example 1:

Input:
gas = [1,2,3,4,5]
cost = [3,4,5,1,2]

Output: 3

Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.
Example 2:

Input:
gas = [2,3,4]
cost = [3,4,3]

Output: -1

Explanation:
You can’t start at station 0 or 1, as there is not enough gas to travel to the next station.
Let’s start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can’t travel around the circuit once no matter where you start.

題目描述:

若干車站排列在一個(gè)環(huán)形上,每個(gè)車站給出儲(chǔ)油量和到下一個(gè)車站的耗油量,選一個(gè)位置能走完一圈,回到出發(fā)位置。

解法:

從某個(gè)位置開始如果lack+gas>cost,則繼續(xù)往后遍歷,并記錄下剩余的汽油,如果<則油不夠,此時(shí)從下一個(gè)開始lack=0,從新遍歷如果還不行的話繼續(xù)從下一個(gè)位置重復(fù),如果最后剩下的汽油能補(bǔ)上之前不夠的,則可以完成一圈從下一個(gè)開始,而不從頭到當(dāng)前位置中的某個(gè)開始是因?yàn)?#xff0c;前面的每次都會(huì)剩下,從頭已經(jīng)是最優(yōu)。

class Solution { public:/*從某個(gè)位置開始如果lack+gas>cost,則繼續(xù)往后遍歷,并記錄下剩余的汽油,如果<則油不夠,此時(shí)從下一個(gè)開始lack=0從新遍歷如果還不行的話繼續(xù)從下一個(gè)位置重復(fù),如果最后剩下的汽油能補(bǔ)上之前不夠的,則可以完成一圈從下一個(gè)開始,而不從頭到當(dāng)前位置中的某個(gè)開始是因?yàn)?#xff0c;前面的每次都會(huì)剩下,從頭已經(jīng)是最優(yōu)。*/int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {int res=0;int temp=0;int lack=0;for(int i=0;i<gas.size();++i){lack+=gas[i]-cost[i];if(lack<0){temp+=lack;lack=0;res=i+1;}}return lack+temp>=0?res:-1;} };

題目:2.加油最少次數(shù)

一輛汽車加滿油后可行駛n公里,假設(shè)汽車在起點(diǎn)是加滿油的。
旅途中有若干個(gè)加油站。設(shè)計(jì)一個(gè)有效算法,指出應(yīng)在哪些加油站停靠加油,
使到達(dá)終點(diǎn)的沿途加油次數(shù)最少。
對(duì)于給定的n和k個(gè)加油站位置,編程輸出停靠加油站的位置。
例如:
n = 100
k = 5
d = [50,80,39,60,40,32] (表示加油站之間的距離)

思路

在可以到達(dá)某個(gè)加油站時(shí)候,可以每個(gè)加油站判斷不加油能不能到下一個(gè),如果可以則不加油繼續(xù)走;不可以的的話,加滿油,繼續(xù)走,記錄下當(dāng)前車站位置

bool count(vector<int>d,int n){vector<int>station;//保存加油的位置if(n-d[0]<0)return -1;elsen = n-d[0];for(int i=1;i<=k;++i){ //0 1 2 3 4 5 6if(i==k&&n-d[i]>=0){return 1;}else if(i==k&&n-d[i]<0){return -1;}if(n-d[i]>=0){n=n-d[i];}else if(n-d[i]<0){station.push_back(i);n=100;}}}

題目:3.leetcode871. Minimum Number of Refueling Stops

A car travels from a starting position to a destination which is target miles east of the starting position.

Along the way, there are gas stations. Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas.

The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses 1 liter of gas per 1 mile that it drives.

When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.

What is the least number of refueling stops the car must make in order to reach its destination? If it cannot reach the destination, return -1.

Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there. If the car reaches the destination with 0 fuel left, it is still considered to have arrived.

Example 1:

Input: target = 1, startFuel = 1, stations = []
Output: 0
Explanation: We can reach the target without refueling.
Example 2:

Input: target = 100, startFuel = 1, stations = [[10,100]]
Output: -1
Explanation: We can’t reach the target (or even the first gas station).
Example 3:

Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
Output: 2
Explanation:
We start with 10 liters of fuel.
We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas.
Then, we drive from position 10 to position 60 (expending 50 liters of fuel),
and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target.
We made 2 refueling stops along the way, so we return 2.

題目描述:

汽車可以無限儲(chǔ)油,給一個(gè)目的距離,初始油量,以及路上按順序每個(gè)加油站的距離和儲(chǔ)油量,求最少加油次數(shù),到不了終點(diǎn)返回-1;

思路

到達(dá)某個(gè)加油站不加油,而是把每個(gè)加油站的油存放在一個(gè)優(yōu)先隊(duì)列,這樣一旦油不夠就可以從優(yōu)先隊(duì)列找最大的存進(jìn)去,不夠繼續(xù)找(只要隊(duì)列不空);
注意需要把重點(diǎn)push到隊(duì)列中去

class Solution { public://用一個(gè)優(yōu)先隊(duì)列保存每個(gè)位置的油,這樣每次取到的都是油最多的位置;//油箱里的油大于等于當(dāng)前距離則通過,小于則從經(jīng)過的位置找一個(gè)油最多的加進(jìn)去,加油次數(shù)res++,如果加完油不夠則繼續(xù)找,一直到隊(duì)列空還不夠返回-1;//遍歷每個(gè)位置都需要把當(dāng)前位置的油存儲(chǔ)量加入隊(duì)列,以備后面使用。int minRefuelStops(int target, int startFuel, vector<vector<int> >& stations) {priority_queue<int>pq;int res=0;int loc=0;vector<int>last_station;last_station.push_back(target);last_station.push_back(0);stations.push_back(last_station);for(int i=0;i<stations.size();++i){while(!pq.empty()&&startFuel<stations[i][0]){int max_gas=pq.top();startFuel+=max_gas;pq.pop();res++;cout<<"res:"<<res<<endl;}cout<<startFuel<<" "<<stations[i][0]<<endl;if(pq.empty()&&startFuel<stations[i][0]){cout<<-1;return -1;}//else{//startFuel-=stations[i][0];//loc+=stations[i][1];//}pq.push(stations[i][1]);}return res;} };

總結(jié)

以上是生活随笔為你收集整理的几道加油站加油相关问题:最小加油次数、能否回到起点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。