当前位置:网站首页>Leetcode-871: minimum refueling times
Leetcode-871: minimum refueling times
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-871: Minimum refueling times
subject
The car set off from the starting point to the destination , The purpose is to the east of the starting position target Miles away .
There are gas stations along the way , Every station[i] For a gas station , It's east of the starting point station[i][0] Miles away , And there are station[i][1] A litre of petrol .
Suppose the capacity of the car's fuel tank is infinite , At first there was startFuel Liter fuel . Every time it drives 1 Miles will be used 1 A litre of petrol .
When the car arrives at the gas station , It may stop to refuel , Transfer all gasoline from the gas station to the car .
In order to reach the destination , What's the minimum number of refuels necessary for a car ? If you can't get to your destination , Then return to -1 .
Be careful : If the remaining fuel when the car arrives at the gas station is 0, It can still refuel there . If the remaining fuel is 0, Still think it has reached its destination .
Problem solving
Method 1 : greedy
This solution and leetcode-45: Jumping game II It's similar , Calculate the maximum coverage distance each time . How many times can you cover the end , It means several times .
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
priority_queue<int> q;
int res=0;
int i=0;
while(startFuel<target){
while(i<stations.size()&&startFuel>=stations[i][0]){
q.push(stations[i++][1]);
}
if(q.empty()) return -1;
startFuel+=q.top();
q.pop();
res++;
}
return res;
}
};
边栏推荐
- Thread start and priority
- 【AutoSAR 三 RTE概述】
- 2022中国3D视觉企业(引导定位、分拣场景)厂商名单
- Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
- 测试右移:线上质量监控 ELK 实战
- 【雅思阅读】王希伟阅读P1(阅读判断题)
- Introduction of UART, RS232, RS485, I2C and SPI
- 【AutoSAR 八 OS】
- Arduino开发之按键检测与正弦信号输出
- [Luogu p4320] road meets (round square tree)
猜你喜欢

【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

Vulkan practice first bullet

Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time

How SQLSEVER removes data with duplicate IDS

mm中的GAN模型架构

Basic use of shell script
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

指针初阶(基础)

Multiprocess programming (I): basic concepts
随机推荐
【日常训练】871. 最低加油次数
lex && yacc && bison && flex 配置的問題
Overlay of shutter (Pop-Up)
leetcode-2280:表示一个折线图的最少线段数
1.12 - Instructions
Set up nacos2 X cluster steps and problems encountered
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
[jetcache] jetcache configuration description and annotation attribute description
【AutoSAR 一 概述】
Illustrated network: what is virtual router redundancy protocol VRRP?
leetcode-224:基本计算器
【AutoSAR 九 C/S原理架构】
cordova-plugin-device获取设备信息插件导致华为审核不通过
【Pulsar文档】概念和架构/Concepts and Architecture
Graduation summary
奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
node_modules删不掉
Rust字符串切片、结构体和枚举类
Array common operation methods sorting (including ES6) and detailed use
腾讯云免费SSL证书扩展文件含义