题目描述

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 <= target, startFuel, stations[i][1] <= 10^9
0 <= stations.length <= 500
0 < stations[0][0] < stations[1][0] < … < stations[stations.length-1][0] < target

思路

思路一:动态规划。对每个站点i,遍历它可以作为所有可能的第j个站点的情况。j从1到i+1。dp[i]表示用i个站点,能到达的最远距离。那么,dp[j] = dp[j-1]+stop[i].fuel。前提是,dp[j-1] >= stop[i].pos。因为每个站点只能用一次,注意j要从大到小遍历。
思路二:每次油料不够时,其实都是从之前的所有站点中,取油量最大的来加,所以用优先队列存储当前位置可以加的所有油量。每次不够时,取最大油量。

代码

动态规划:

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

优先队列:

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

自己想的时候一点思路都没有。关键是状态没想到。也没理解好,油量都是累加的,只需要加最多的油量就好了。

【LeetCode 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. 871. Minimum Number of Refueling Stops

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

  5. LeetCode871.Minimum Number of Refueling Stops

    871.Minimum Number of Refueling Stops(加油站的最小数量) Description Difficulty: hard Example 1: Example 2: E ...

  6. 算法设计与分析:Minimum Number of Refueling Stops(Week 11)

    学号:16340008 题目:871. Minimum Number of Refueling Stops Question: A car travels from a starting positi ...

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

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

  8. LeetCode(871):最低加油次数 Minimum Number of Refueling Stops(Java)

    2019.7.25 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 这道智力题让我想起了LeetCode 871:加油站. 可以很容易能够想到用回溯法来做,每次经过加油站选择加 ...

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

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

最新文章

  1. “奥利”来啦,腾讯Robotics X实验室跑出的“轮滑小子”
  2. 如何解决ORA-00054资源正忙,要求指定NOWAIT?
  3. javascript感叹号1_「翻译」JavaScript的可视化学习之三:作用域(链)
  4. 产品微操的艺术:提高核心指标的5个需求原理(1~5完)
  5. 二叉堆详解实现优先级队列
  6. 关于'Deep Neural Networks for YouTube Recommendations'的一些思考和实现
  7. 洛谷OJ P1434 [SHOI2002]滑雪 搜索 递归 记忆化搜索
  8. 【Mac】 自带的播放器quicktimeplayer 如何带声音2倍速播放
  9. cp105b linux 驱动,cp105b驱动下载-富士施乐cp105b驱动下载v2.6.15.0 官方最新版-西西软件下载...
  10. 英语单词词性顺口溜_英语单词词性顺口溜
  11. 计算机显卡风扇有异响,电脑运行中有异响,拍一拍就好了,原来好多人还不知道问题在哪!...
  12. 二年级计算机学什么礼物,二年级的小朋友喜欢什么礼物(最美好的礼物二年级)...
  13. 0314-html img em i stong b标签应用
  14. 清华大学公开课线性代数2——第12讲:复数与复矩阵
  15. 解决Appium Desktop 测试中,元素不能准确定位的问题
  16. redis数据库的概述
  17. 前端VUE3+Vite -- 框架搭建
  18. 如何在未越狱iOS设备上安装IPA
  19. Linux——编写简单的Bash脚本
  20. 【SAP-SD】史上最全的SAP凭证类型总结

热门文章

  1. 计算机网络专业的自我鉴定,计算机网络专业的自我鉴定
  2. 《Android系统源代码情景分析》一书勘误
  3. 华为M-LAG跨设备链路聚合技术理论讲解
  4. 我所学到的EC-2(个人学习总结,不能保证正确,欢迎大佬指正)
  5. 模拟ic设计工程师面试总结
  6. IPTV直播和实时互动直播是一回事吗?
  7. leaftlet 中Polygon的使用属性
  8. python阶乘的代码_python编码阶乘
  9. iMeta期刊部分文章被PubMed收录
  10. 32:汉字表示的大写数字金额