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.

Example 1:

Input: target = 1, startFuel = 1, stations = []
Output: 0
Explanation: We can reach the target without refueling.

Example 2:

Input: target = 100, startFuel = 1, stations = [[10,100]]
Output: -1
Explanation: We can't reach the target (or even the first gas station).

Example 3:

Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
Output: 2
Explanation:
We start with 10 liters of fuel.
We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas.
Then, we drive from position 10 to position 60 (expending 50 liters of fuel),
and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target.
We made 2 refueling stops along the way, so we return 2.

Note:

  1. 1 <= target, startFuel, stations[i][1] <= 10^9
  2. 0 <= stations.length <= 500
  3. 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target

Approach #1: C++. [heap]

class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
priority_queue<int> pq;
int cur = startFuel;
int i = 0;
int stops = 0;
while (true) {
if (cur >= target) return stops;
while (i < stations.size() && stations[i][0] <= cur) {
pq.push(stations[i++][1]);
}
if (pq.empty()) break;
cur += pq.top(); pq.pop();
stops++;
}
return -1;
}
};

  

step 1: when the car using the current gas to get to the most far position. we push the gas to a priority_queue which we see in this process.

step 2: if it don't get to the target, we refule gas with the max number in the priority_queue(pq.top()). and stops++.

step3 : repeate the step 1, till we get to the target.

Approach #2: Java. [DP].

class Solution {
public int minRefuelStops(int target, int startFuel, int[][] stations) {
long[] dp = new long[stations.length + 1];
dp[0] = startFuel;
for (int i = 0; i < stations.length; ++i) {
for (int j = i + 1; j > 0; --j) {
if (dp[j-1] >= stations[i][0]) {
dp[j] = Math.max(dp[j], dp[j-1] + stations[i][1]);
}
}
} for (int i = 0; i <= stations.length; ++i) {
if (dp[i] >= target) return i;
} return -1;
}
}

  

Analysis:

dp[i] : represent the max distance refule with i stations.

871. Minimum Number of Refueling Stops的更多相关文章

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

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

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

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

  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. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  6. [LeetCode] 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. Reorder array to construct the minimum number

    Construct minimum number by reordering a given non-negative integer array. Arrange them such that th ...

  8. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. Lintcode: Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

随机推荐

  1. 什么是语义化的HTML

    语义化的HTML使用每个html标签都特定的用途,例如p标签放大段文字, h1~h6常用于标题,strong加粗强掉….. 语义化的好处: 1:去掉或样式丢失的时候能让页面呈现清晰的结构: html本 ...

  2. NetBeans+Xdebug调试原理

    使用Xdebug的远程调试,Xdebug作为一个嵌入到PHP的程序,扮演着客户端的角色,而IDE则作为服务器.下面的动态图展示了连接建立的过程. 服务端的IP为10.0.1.2, 使用HTTP协议,端 ...

  3. 1.3Broker

    Celery需要一种解决消息的发送和接受的方式,我们把这种用来存储消息的的中间装置叫做message broker, 也可叫做消息中间人. 作为中间人,我们有几种方案可选择: 1.RabbitMQ R ...

  4. Anaconda使用总结(文章来自网络)

    序 Python易用,但用好却不易,其中比较头疼的就是包管理和Python不同版本的问题,特别是当你使用Windows的时候.为了解决这些问题,有不少发行版的Python,比如WinPython.An ...

  5. javascript第四节

    闭包: 块级作用域: 私有变量:

  6. javascipt——原型

    1.原型存在的意义 JS不是面向对象的语言,没有类的概念,但是提供了构造器函数,其也可以创建一个对象.构造器函数如下: function people(name, age, sex) { this.n ...

  7. Centos下nginx支持https协议

    1.首先配置nginx及其他插件,这个Google下,很多配置方案. 2.配置服务器的证书.操作步骤如下: [root@localhost ~]# cd /etc/pki/tls/certs [roo ...

  8. Zookeeper 基础、工作流、ZAP协议

    ZooKeeper 基础 在深入了解ZooKeeper的运作之前,让我们来看看ZooKeeper的基本概念.[1] 我们将在本章中讨论以下主题:1.Architecture(架构)2.Hierarch ...

  9. docker 制作本地镜像

    docker commit 55ddf8d62688 py_wb # 容器ID, 容器名称tag py_wb IP地址:5000/my-web:20180511 # 远程registory地址 我的镜 ...

  10. 696. Count Binary Substrings统计配对的01个数

    [抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...