几道加油站加油相关问题

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;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.

题目描述:

汽车可以无限储油,给一个目的距离,初始油量,以及路上按顺序每个加油站的距离和储油量,求最少加油次数,到不了终点返回-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. 优先队列之加油站最小加油次数

                                                                                   分析:这里最笨的方法是使用递归下的深度优先 ...

  2. Java 小白 设计加油站类和汽车类,加油站提供一个给车加油的方法,参数为剩余汽油数量。每次执行加油方法,汽车的剩余数量都会加2

    //设计加油站类和汽车类,加油站提供一个给车加油的方法,参数为剩余汽油数量.每次执行加油方法,汽车的剩余数量都会加2 public class fourteen {public static void ...

  3. 计算机专业校运会加油稿,软件学院运动会加油稿

    软件学院运动会加油稿 软件学院运动会加油稿 10月11日8点,东华理工大学软件学院2014年运动会在田径场盛大举行,在软件学院学团办主任郑爱华.软件学院团委书记郑纲.各专业辅导员以及参赛队员们和全体团 ...

  4. 加油折扣系统,加油优惠软件

    加油小程序.加油APP是一款网上优惠卡加油充值软件,加油app怎么开发?加油APP加油卡充值软件系统用户在软件中每天都能了解到自己的热门活动,让你在软件中提供更多的优惠充值服务加油APP是一款很不错的 ...

  5. mysql 5.0.26 log文档_我家加油app下载-我家加油 v5.0.26 手机版 - 量产资源网

    我家加油app,为广大车主提供便捷又方便的加油站服务,车主这里绑定加油站,可以实时了解到所绑加油站的各种油价变更.还有活动等信息,一旦有新的信息更新,车主第一时间了解.每次车辆加油所获得的积分,这里都 ...

  6. 加油四班!加油佟穆!我们的征途是星辰大海!!!

    大家好,我是雄雄,欢迎关注微信公众号[雄雄的小课堂] 首先,感谢佟老师与范老师,在我不在的时候,三班的就业和四班的学习都落在了两位老师的肩头,其次,还需要特别感谢冯老师,能抽时间为四班讲课,辛苦三位老 ...

  7. 一文整理14道MySQL索引相关面试题

    精心整理14道MySQL索引相关面试题(珍藏版) 如果仅仅是死记硬背MySQL索引相关面试题一定是相当枯燥的,不容易记却容易忘,这里循序渐进的讲解有关索引有关知识点,让大家在理解的基础上记住一些面试常 ...

  8. Hive分析函数之SUM,AVG,MIN和MAX OVER(PARTITION BY xxx order by xxx,用于求一段时间内截至到每天的累计访问次数、平均访问次数、最小访问次数、最大访问次

            Hive分析函数之SUM,AVG,MIN和MAX OVER(PARTITION BY xxx order by xxx,用于求一段时间内截至到每天的累计访问次数.平均访问次数.最小访问 ...

  9. LeetCode简单题之最小操作次数使数组元素相等

    题目 给你一个长度为 n 的整数数组,每次操作将会使 n - 1 个元素增加 1 .返回让数组所有元素相等的最小操作次数. 示例 1: 输入:nums = [1,2,3] 输出:3 解释: 只需要3次 ...

最新文章

  1. tensorflow学习之常用函数总结:tensorflow官方例子中的诸如tf.reduce_mean()这类函数
  2. hdu2133: What day is it
  3. 第六十节,文本元素标签
  4. Java操作MongoDB之mongodb-driver
  5. 圆弧齿轮啮合原理_图解八种齿轮的加工原理
  6. 定值保险计算举例_机动车辆保险的一些购买原则181536312
  7. Redis 网络编程
  8. HDOJ--1598--find the most comfortable road(并查集+枚举)
  9. error LNK2005 原理及解决办法
  10. Tcl/Tk快速入门
  11. 跨平台跨端的登录流程及其安全设计
  12. 【第三方登录】第三方登录 Part1 —— QQ登录(2016-09最新版)
  13. Feign传输MultipartFile 报错 Error converting request body
  14. 【板栗糖GIS】win11提示无法成功操作 因为文件包含病毒
  15. ORA-00054 错误原因分析
  16. 数学:分数的加减乘除模板
  17. Maven的阿里云镜像配置
  18. rc列联表_R语言入门之频率表和列联表
  19. 使用Onekey Ghost安装器一键还原系统的方法
  20. 分享一篇超全的 Vue 相关的资源,值得收藏!

热门文章

  1. 如何在Linux上创建手册页
  2. mysql一次查询无关联多个表_面试官:为什么mysql不建议执行超过3表以上的多表关联查询?...
  3. 2017第十九届中国国际地面材料及铺装技术展览会会刊(参展商名录)
  4. 蓝桥杯 单片机 决赛 第7届 电压、频率采集设备
  5. 服务器固态硬盘raid0,SSD固态硬盘,撸一把RAID0模式大提速
  6. 这些AI开源项目可以让你创作出卢浮宫级别的艺术品!
  7. Android 仿微信通讯录
  8. 独家 | 机器学习解释模型:黑盒VS白盒(附资料链接)
  9. People seldom do what they believe in. They do what is convenient, then repent.
  10. C. Dima and Staircase(思维)