LC 871. Minimum Number of Refueling Stops 【lock, hard】
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.
经典的小车加油问题。
【一定要注意看题,每个变量是什么意思。前几次都把stations[i][0]看成相邻两个加油站的距离了。。。】
利用优先队列。因为要求最小的加油次数,到了一个加油站不加油,但进入优先队列,不能到达终点或者下一个加油站的时候再取最大的。
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
priority_queue<int> pq;
int driven = ;
int idx = ;
int ret = ;
while (startFuel < target) {
if (idx >= stations.size()) {
if(pq.empty()) return -;
while (!pq.empty() && startFuel < target) {
startFuel += pq.top(); pq.pop();
ret++;
}
if (startFuel >= target) return ret;
else return -;
}
if (startFuel >= stations[idx][]) {
pq.push(stations[idx][]);
}
else {
if (pq.empty()) return -;
while (!pq.empty() && startFuel < stations[idx][]) {
startFuel += pq.top(); pq.pop();
ret++;
if (startFuel >= target) return ret;
}
if(startFuel < stations[idx][]) return -;
pq.push(stations[idx][]);
}
idx++;
}
return ret;
}
};
另一个种解法:
int minRefuelStops(int target, int cur, vector<vector<int>> s) {
int i = , res;
priority_queue<int>pq;
for (res = ; cur < target; res++) {
while (i < s.size() && s[i][] <= cur)
pq.push(s[i++][]);
if (pq.empty()) return -;
cur += pq.top(), pq.pop();
}
return res;
}
LC 871. Minimum Number of Refueling Stops 【lock, hard】的更多相关文章
- 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- [LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数
A car travels from a starting position to a destination which is target miles east of the starting p ...
- 871. Minimum Number of Refueling Stops
A car travels from a starting position to a destination which is target miles east of the starting p ...
- [Swift]LeetCode871. 最低加油次数 | Minimum Number of Refueling Stops
A car travels from a starting position to a destination which is target miles east of the starting p ...
- LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- [LC] 452. Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- LC 499. The Maze III 【lock,hard】
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- LC 656. Coin Path 【lock, Hard】
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
- leetcode:Minimum Depth of Binary Tree【Python版】
1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: # Definition f ...
随机推荐
- Vue移动端焦点状态跳转页面,焦点和键盘不消失的问题
问题:input搜索,在移动端焦点状态跳转页面,焦点不失焦,还在跳转后的页面闪烁,且键盘不消失 //主动隐藏键盘 document.getElementById('inputSearch').blur ...
- 2019年C题 视觉情报信息分析
2019 年第十六届中国研究生数学建模竞赛C 题 任务1中 图三:图3 中拍照者距离地面的高度 目录: 0.试题分析: 1.构建摄像机模型 2.摄像机参数假定 3.像平面坐标计算 4.图像标定及数值测 ...
- Oracle笔记(十) 约束
表虽然建立完成了,但是表中的数据是否合法并不能有所检查,而如果要想针对于表中的数据做一些过滤的话,则可以通过约束完成,约束的主要功能是保证表中的数据合法性,按照约束的分类,一共有五种约束:非空约束.唯 ...
- 二十,基于K8S集群的PaaS简介
目录 一.概述 二.生产环境部署k8s,接下来我们描述一下在真正生产环境中我们部署k8s应该部署成什么样子 一.概述 1.通过以往的学习应该可以了解到k8s 和以往提到的devops概念更容易落地了. ...
- 7. Function Decorators and Closures
A decorator is a callable that takes another function as argument (the decorated function). The deco ...
- layui 单选框、多选框radio 元素判断是必填项 lay-verify='required'
简单验证复选框,单选框必填 例子代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- gerapy+scrapyd组合管理分布式爬虫
Scrapyd是一款用于管理scrapy爬虫的部署和运行的服务,提供了HTTP JSON形式的API来完成爬虫调度涉及的各项指令.Scrapyd是一款开源软件,代码托管于Github上. 点击此链接h ...
- mysql主从同步监控---邮件告警
#!/bin/bash #check MySQL_Slave Status #crontab time : MYSQLPORT=`netstat -na|grep "|awk -F[:&qu ...
- FFmpeg常用命令学习笔记(八)滤镜相关命令
滤镜相关命令 FFmpeg中关于滤镜命令有很多种.比如在视频中加入/删除水印.对视频做反转等等都属于滤镜. FFmpeg滤镜处理流程 解码后的数据帧 ---过滤处理---> 过滤后的数据帧 -- ...
- Oracle 连接排序
---左联操作SELECT e.* FROM hs_opt_ewb e left join hs_workform_main m on e.ewb_no=m.ewb_nowhere e.ewb_no= ...