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. CHD-5.3.6集群安装

    我是基于Apache-hadoop2.7.3版本安装成功后,已有的环境进行安装chd-5..6 已用的环境: JDK版本: java version "1.8.0_191" Jav ...

  2. Oracle笔记(十二) 集合、序列

    一.集合 在数学的操作之中存在交.差.并.补的概念,而在数据的查询中也存在此概念,有如下几个连接符号: UNION:连接两个查询,相同的部分不显示: UNION ALL:连接两个查询,相同的部分显示: ...

  3. 05_Hive分区总结

    2.1.创建分区表并将本地文件的数据加载到分区表: 使用下面的命令来创建一个带分区的表 通过partitioned by(country string)关键字声明该表是分区表,且分区字段不能为crea ...

  4. linux-2.6.38poll机制简析(以tiny6410按键中断程序为基础)

    一.应用程序 /* struct pollfd { int fd; //文件描述符 short events; //表示请求检测的事件 short revents; //表示检测之后返回的事件 }; ...

  5. cursor:not-allowed

    今天发现了一个鼠标样式:not-allowed,是一个红色圈加一个斜杠,表示禁止的意思,似乎IE ,chrome firefox 都可以正常显示,很好用

  6. 5.caffe:train.sh 和 test.sh (训练与测试 )

    一,train.sh #!/usr/bin/env sh ./build/tools/caffe train --solver=myself/00b/solver.prototxt # cd CAFF ...

  7. MYSQL安全模式SQL语法需要注意的地方

    MYSQL安全模式 Mysql版本: 背景: 为了避免在执行delete.update将全表数据清空或者覆盖修改,在新项目营销云中开启了mysql的安全模式. 安全模式要求不能对非主键的条件查询做up ...

  8. body element height id small, but the backgroud color is full screen

    http://www.cnblogs.com/xiaoyuersdch/p/9156240.html ------------------------------------------------- ...

  9. Apache、nginx 、lighttpd性能比较

    Apache.nginx .lighttpd性能比较 1. web服务器简介 1. lighttpd Lighttpd是一个德国人领导的开源软件,其根本的目的是提供一个专门针对高性能网站,安全.快速. ...

  10. BZOJ 2244: [SDOI2011]拦截导弹 (CDQ分治 三维偏序 DP)

    题意 略- 分析 就是求最长不上升子序列,坐标取一下反就是求最长不下降子序列,比较大小是二维(h,v)(h,v)(h,v)的比较.我们不看概率,先看第一问怎么求最长不降子序列.设f[i]f[i]f[i ...