题目:

A car travels from a starting position to a destination which is target miles east of the starting position.

There are gas stations along the way. The gas stations are represented as an array stations where stations[i] = [positioni, fueli] indicates that the ith gas station is positioni miles east of the starting position and has fueli liters of gas.

The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses one liter of gas per one 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.

Return the minimum 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 not 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.

Constraints:

  • 1 <= target, startFuel <= 109
  • 0 <= stations.length <= 500
  • 0 <= positioni <= positioni+1 < target
  • 1 <= fueli < 109

思路1:

第一反应是用递归来做,f[i][j]表示经过第 i 个加油站后,加油 j 次能够到达的最大距离,最后只需要找到最小的 j 并且f[i][j]大于等于target即可。状态转移方程中f[i][j]可以从三个地方来:1)f[i][j - 1],代表经过了当前车站,但是不在当前车站加油;2)f[i - 1][j],表示到达了第i - 1即前一个车站,并且加了j次油,即上个车站没加油;3)f[i - 1][j - 1] + stations[i - 1][1],即经过了第i - 1个车站,并且在上个车站加了油,当然这里有条件就是f[i - 1][j - 1]的最大距离必须要比stations[i - 1][0]大,不然无法到达上个车站。初始化我们用 n = stations.size() + 1,即f[i][j]当 j 等于0 时表示f[i][0]都是没加过油的,那么它们的值应该为startFuel,至于f[0][j],不需要初始化,因为j一定小于等i,即加油次数不能比路过的车站多。

代码1:

class Solution {
public:
    int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
        int n = stations.size() + 1;
        vector<vector<long>> f(n, vector<long>(n));
        for (int i = 0; i < n; i++) {
            f[i][0] = startFuel;
        }      
        for (int i = 1; i < n; i++) {
            for (int j = 1; j <= i; j++) {
                if (f[i - 1][j - 1] >= stations[i - 1][0]) {
                    f[i][j] = f[i - 1][j - 1] + stations[i - 1][1];
                }
                f[i][j] = max({f[i - 1][j], f[i][j - 1], f[i][j]});
            }
        }
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < n; i++) {
                if (f[i][j] >= target)
                    return j;
            }
        }
        return -1;
    }
};

思路2:

从本质上来看,题目要求我们需要在最少的加油次数中加最多的油,即每次加油都加能够加的最多的油。但是我们每次路过车站,其实只有当前车站可以选择,那么怎么构成选项呢?答案是每次路过车站先不加油,把能够加的油记录进一个优先队列,等到需要加油的时候我们再取出来为自己加油。这里优先队列是为了管理最大值,使我们能够随时取出来的都是最多的油。首先我们能够到达的最大距离dist就是startFuel,之后判断dist和target,一旦dist大于等于target即可结束。循环中,每次我们路过车站,如果当前dist能够到达车站,那我们就先不加油,而是把油加入优先队列;如果不能到达车站,那么就加油,如果优先队列已经无油可加,证明我们不能到达target,直接返回-1;如果优先队列有油,那么就拿出最大的油,加给dist,同时更新加油次数即可。

代码2:

class Solution {
public:
    int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
        priority_queue<int> q;
        int dist = startFuel, count = 0;
        int i = 0;
        while (dist < target) {
            if (i < stations.size() && dist >= stations[i][0]) {  //能到车站说明油暂时是够的
                q.push(stations[i][1]);  //先不加油,存起来
                i++;
            } else {
                if (q.size()) {  //有油可加
                    dist += q.top();    //加油
                    q.pop();
                    count++;  //更新次数
                } else {  //无油可加,无法到达target
                    return -1;
                }
            }
        }
        return count;
    }
};

871. Minimum Number of Refueling Stops相关推荐

  1. LeetCode刷题:871. Minimum Number of Refueling Stops

    LeetCode刷题:871. Minimum Number of Refueling Stops 原题链接:https://leetcode.com/problems/minimum-number- ...

  2. LeetCode 871. Minimum Number of Refueling Stops 最少加油次数

    LeetCode 871. Minimum Number of Refueling Stops 本题是LeetCode 871题,最少加油次数. 题目描述 A car travels from a s ...

  3. LeetCode: 871. Minimum Number of Refueling Stops

    LeetCode: 871. Minimum Number of Refueling Stops 题目描述 A car travels from a starting position to a de ...

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

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

  5. 算法练习14:leetcode习题871. Minimum Number of Refueling Stops

    文章目录 题目 算法思路 C++代码 题目 A car travels from a starting position to a destination which is target miles ...

  6. LeetCode:871. Minimum Number of Refueling Stops - Python

    问题描述: 871. 最低加油次数 汽车从起点出发驶向目的地,该目的地位于出发位置东面 target英里处. 沿途有加油站,每个station[i] 代表一个加油站,它位于出发位置东面 station ...

  7. [LeetCode] 871. Minimum Number of Refueling Stops

    题:https://leetcode.com/problems/minimum-number-of-refueling-stops/ 题目大意 起点有油 startFuel 的车,想行驶到 终点 ta ...

  8. [LeetCode] 871. Minimum Number of Refueling Stops @ python

    一.题目: 初始油量startFuel,给一个到达目的地所需的油量target,还有沿途的加油站stations(包括到达加油站所需油量以及储备的油量),求达到目的地所需的最少加油次数,如果到不了返回 ...

  9. 【Leetcode】871. Minimum Number of Refueling Stops

    题目地址: https://leetcode.com/problems/minimum-number-of-refueling-stops/description/ 有一个小车,初始位置在 0 0 0 ...

最新文章

  1. 帧率配置_《骑马与砍杀2》配置探究:CPU显卡怎么搭配达到理想画质和帧数?...
  2. vi查找替换命令详解
  3. 字符串类型的日期如何存储到数据库Date类型的字段中
  4. Android平台RTSP轻量级服务|RTMP推送摄像头或屏幕之音频接口设计
  5. 不是有效的函数或过程名_过程和函数
  6. SQL 审核:基于PG数据库插件hook的SQL规范审核工具
  7. JS案例:使用对象、对象数组、正则表达式
  8. Atitit 中间件之道 attilax著 1. 第1章 中间件产生背景及分布式计算环境 2 2. 中间件分类 2 2.1. 通讯,消息,数据存储中间件 3 3. 第3章 COM相关技术 3 4.
  9. 黑苹果hidp显示不清楚_黑苹果开启缩放分辨率HiDPi以及字体模糊的调整方法总结...
  10. Cisco.Packet.Tracer思科模拟器浮动路由讲解(含实例步骤)
  11. 马云现场演讲:宣布卸任董事局主席——“青山不改,后会有期!”
  12. 绘制地图其实并不难!如何绘制地图?看看Smartbi的制作方法
  13. matlab 创建网格图和曲面图
  14. 这些样式表,你都用过么?
  15. JSP学习笔记(八):使用ArrayList
  16. UVA 11134 Fabled Rooks
  17. 【面试题】Java 高级工程师面试刷题100题(二)
  18. minio Non-XML response from server
  19. 关于emplace_back()的理解
  20. 免费无限大文件存储网盘 -- send.cm!必须收藏!操作简单,国内外都能访问,不用注册或下载,支持FTP,还带API,页面还简洁,真的爱了~

热门文章

  1. 你遇到过启动电脑以后系统桌面无法显示吗
  2. 总结运用kali破解WIFI密码的多种方法
  3. (复习)基础算法--搜索--深入训练(USACO-Feb08、WOW模拟赛Day2-T4、USACO-Dec13、CTSC-1999)
  4. 马士兵mysql_MYSQL相关总结(马士兵教育)
  5. Leetcode刷题——剑指offer_1
  6. 黑皮西瓜文案:黑皮西瓜水果的促销文案,黑皮西瓜水果店的文案怎么写
  7. ubuntu 命令笔记
  8. 计算机网络技术用i3可以吗,买电脑避坑第一步,i3处理器和i5等处理器有什么区别?...
  9. [CTO札记]武侠人物名称稀缺,上起点找吧
  10. 读论文系列(二)Convolutional Neural Networks over Tree Structures for Programming Language Processing