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】的更多相关文章

  1. 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

  2. [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 ...

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

  4. [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 ...

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

  6. [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 ...

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

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

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

随机推荐

  1. Spring Cloud(O)服务的注册与发现(Eureka)

    一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通过负载均衡设备共同对外提供服务 1.3 ...

  2. django的几种缓存配置

    前言 首先说,为什么要用缓存的,由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存 ...

  3. Django—views系统:views基础

    Django的View(视图)简介 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...

  4. redis—持久化操作

    简介 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(d ...

  5. 二十,基于K8S集群的PaaS简介

    目录 一.概述 二.生产环境部署k8s,接下来我们描述一下在真正生产环境中我们部署k8s应该部署成什么样子 一.概述 1.通过以往的学习应该可以了解到k8s 和以往提到的devops概念更容易落地了. ...

  6. JAVA-如何打包成jar包

    主线:编译 - 打包 - 运行 准备工作: 1. 手动打可直接执行的jar包 1) 先使用javac编译java文件,得到class文件 2) 新建文件,名字任起,比如可以叫manifest,内容如下 ...

  7. OSI七层协议和TCP/IP四层协议

    1. OSI七层和TCP/IP四层的关系 1.1 OSI引入了服务.接口.协议.分层的概念,TCP/IP借鉴了OSI的这些概念建立TCP/IP模型. 1.2 OSI先有模型,后有协议,先有标准,后进行 ...

  8. java线程基础巩固---如何给你的应用程序注入钩子程序

    这次做一个比较有意思的实验,我们知道当一个程序如果抛异常了其程序肯定会挂掉,那有木有可能在程序异常退出时能执行一段咱们自己的代码,比如说服务器在异常退出时需要做一些额外的资源清理,像这种场景就正好是这 ...

  9. iView - DatePicker组件神坑,如何处理?

    最近使用iView - DatePicker组件时发现一些问题,明明设置是正常的日期时间格式,当需要使用这个时间的时候,页面却显示 Fri Jun 09 2017 12:00:10 GMT+0800 ...

  10. Java8-Stream-No.01

    import java.util.ArrayList; import java.util.List; import java.util.Optional; public class Streams1 ...