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

几道加油站加油相关问题

1.选一个加油站能走完一圈:leetcode134. Gas Station
2.加油最少次数(easy)
3.加油最少次数(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.

题目描述:

若干车站排列在一个环形上,每个车站给出储油量和到下一个车站的耗油量,选一个位置能走完一圈,回到出发位置。

解法:

从某个位置开始如果lack+gas>cost,则继续往后遍历,并记录下剩余的汽油,如果<则油不够,此时从下一个开始lack=0,从新遍历如果还不行的话继续从下一个位置重复,如果最后剩下的汽油能补上之前不够的,则可以完成一圈从下一个开始,而不从头到当前位置中的某个开始是因为,前面的每次都会剩下,从头已经是最优。

class Solution {
public:
    /*
    从某个位置开始如果lack+gas>cost,则继续往后遍历,并记录下剩余的汽油,如果<则油不够,此时从下一个开始lack=0从新遍历
    如果还不行的话继续从下一个位置重复,如果最后剩下的汽油能补上之前不够的,则可以完成一圈
    从下一个开始,而不从头到当前位置中的某个开始是因为,前面的每次都会剩下,从头已经是最优。
    */
    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.加油最少次数

一辆汽车加满油后可行驶n公里,假设汽车在起点是加满油的。
旅途中有若干个加油站。设计一个有效算法,指出应在哪些加油站停靠加油,
使到达终点的沿途加油次数最少。
对于给定的n和k个加油站位置,编程输出停靠加油站的位置。
例如:
n = 100
k = 5
d = [50,80,39,60,40,32] (表示加油站之间的距离)

思路

在可以到达某个加油站时候,可以每个加油站判断不加油能不能到下一个,如果可以则不加油继续走;不可以的的话,加满油,继续走,记录下当前车站位置

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

题目描述:

汽车可以无限储油,给一个目的距离,初始油量,以及路上按顺序每个加油站的距离和储油量,求最少加油次数,到不了终点返回-1;

思路

到达某个加油站不加油,而是把每个加油站的油存放在一个优先队列,这样一旦油不够就可以从优先队列找最大的存进去,不够继续找(只要队列不空);
注意需要把重点push到队列中去

class Solution {
public:
    //用一个优先队列保存每个位置的油,这样每次取到的都是油最多的位置;
    //油箱里的油大于等于当前距离则通过,小于则从经过的位置找一个油最多的加进去,加油次数res++,如果加完油不够则继续找,一直到队列空还不够返回-1;
    //遍历每个位置都需要把当前位置的油存储量加入队列,以备后面使用。
    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;
    }
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值