当前位置:网站首页>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;
}
};
边栏推荐
- 百数不断创新,打造自由的低代码办公工具
- 测试右移:线上质量监控 ELK 实战
- The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
- 线程的启动与优先级
- ftrace工具的介绍及使用
- Introduction of UART, RS232, RS485, I2C and SPI
- NC50965 Largest Rectangle in a Histogram
- Shell implements basic file operations (cutting, sorting, and de duplication)
- 【AutoSAR 九 C/S原理架构】
- 【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
猜你喜欢

百度智能云牵头打造智能云综合标准化平台

University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量

Hundreds of continuous innovation to create free low code office tools

Gan model architecture in mm

Teach you JDBC hand in hand -- structure separation

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

Hdu3507 (slope DP entry)

Multiprocess programming (I): basic concepts
![[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)](/img/7e/4f9d96edd04e9ffb26434baf34aa43.jpg)
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)

2022中国3D视觉企业(引导定位、分拣场景)厂商名单
随机推荐
Machine learning: numpy version linear regression predicts Boston house prices
FAQ | FAQ for building applications for large screen devices
【luogu P4320】道路相遇(圆方树)
Rust string slicing, structs, and enumeration classes
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
Nc20806 District interval
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
The most painful programming problem in 2021, adventure of code 2021 Day24
Briefly talk about other uses of operation and maintenance monitoring
1.12 - 指令
【AutoSAR 二 AppL概述】
机器学习:numpy版本线性回归预测波士顿房价
【AutoSAR 三 RTE概述】
leetcode-871:最低加油次数
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
Vulkan is not a "panacea"“
Multi process programming (III): message queue
【AutoSAR 一 概述】
logback配置文件