题目描述

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. MySql Unknown column 的解决方案
  2. 如何使用CocoStudio场景编辑器制作魔卡幻想
  3. 一个比较方便的转换NSString为UTF8编码的函数
  4. jstl视图_使用JSTL视图探索Spring Controller
  5. 聚类方法(Clustering)
  6. python怎么读取石墨表格_每2秒进行一次statsd xaxis石墨统计 - python
  7. mfc编程vc6.0实现进程的创建和通信_免费送书:windows黑客编程技术详解
  8. WinForm里ListBox实现加入项目,并且排序。
  9. 浮点错误的意思-PAT 、OJ
  10. JAVA重写和重载的区别
  11. Dubbo的failsafe容错策略
  12. git系列之-放弃修改
  13. 鸿蒙试炼多少可以单挑,知己知彼《灭神》单挑虚无之地大BOSS
  14. android carlife 源码,CarLife开发总结
  15. 嵌入式计算机应用方面,嵌入式实时软件在计算机中的应用研究
  16. 微信小程序如何发布小程序?
  17. 调试omnet几个BUG(未完待续)
  18. 如何迎新年?手工自制2021年精美简朴行事历
  19. 安利一个最适合数据分析师的小众高薪兼职!
  20. 顾维灏谈百度地图数据采集:POI自动处理率达90%

热门文章

  1. SSL数字证书下载流程是怎么样的
  2. IT人必看!2018年上半年云栖大会300份干货PPT免费开放!最前沿的技术都在这了!...
  3. 计算机科学概论(第12版)下载
  4. c语言程序设计猪八戒吃西瓜,三年级语文下册教案——《猪八戒吃西瓜》教学设计之二...
  5. 优雅的进行线上数据订正
  6. HTTPS安全通讯 1. 密码学基础
  7. 【Nginx学习01】--Nginx的安装
  8. IEEE 1588 Ordinary clocks
  9. 小米mix2安兔兔html5跑分,2019安兔兔性能跑分排行榜 小米9华丽登顶,荣耀v20排第9...
  10. 东京语言学校推荐|日本语言学校哪个好