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 ...
随机推荐
- 生成ID之雪花算法
package com.shopping.test; /** * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000 ...
- Delphi 执行线程对象
- git回退到历史版本
问题描述 在开发的过程中,想要修改一个参数的命名.然后修改各种地方,并且push上码云的远程仓库.然后突然发现还要改很多地方,突然后悔不想改动了.那该怎么办呢? 处理步骤 回退本地的git版本 将本地 ...
- LoadRunner(7)
一.参数化策略 1.Select next row(How? 如何取?)取值方式 选择下一行 1)Sequential:顺序的 每个VU都从第一行开始,顺序依次向下取值: 数据取完可以从头循环重复使用 ...
- Java&Selenium 鼠标键盘及滚动条控制相关方法封装
一.摘要 本片博文主要展示在使用Selenium with java做web自动化时,一些不得不模拟鼠标操作.模拟键盘操作和控制滚动条的java代码 二.模拟鼠标操作 package util; im ...
- easyui tree选中指定节点,点击指定节点
功能需求描述如下: A主页面,在datagrid的某行上,操作列,点击详情,Tab页面上加载B页面 B页面,左边是树tree,右边是左边树的详情列表 要求:由A页面链接到B页面,B页面的tree,默认 ...
- [转载]springboot--常用注解--@configration、@Bean
springboot--常用注解--@configration.@Bean @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME ...
- SARS病毒 (生成函数 + 快速幂)
链接:https://ac.nowcoder.com/acm/contest/992/A来源:牛客网 题目描述 目前,SARS 病毒的研究在世界范围内进行,经科学家研究发现,该病毒及其变种的 DNA ...
- 简单的理解 equals和==的区别
直接上代码: //== 比较的是地址 String test = new String("测试"); String test1 = new String("测试" ...
- HDU 5634 (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5634 题意:给出 n 个数,有三种操作,把区间的 ai 变为 φ(ai):把区间的 ai 变为 x:查 ...