作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/minimum-number-of-refueling-stops/

题目描述

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

题目大意

一个车刚开始的时候有一些油,现在在一条直线上有一些加油站,第i个加油站距离出发点的距离和储油量是station[i][0]和station[i][1],假设汽车的油箱是无限大的,现在求从出发点出发能否到达结束点target,如果可以的话,需要经历的最少加油站是多少。

解题方法

贪心算法

这个题是我遇到的腾讯面试题和2019年百度笔试题。

这个题的直观思路是当油箱剩的有油的时候,不能遇到加油站就去加油,因为可能在油用完之前遇到另一个存油量很多的加油站。所以在这个思路下,如何解呢?

这个题的方法是,我们使用一个大根堆保存所有经历过的加油站的存量,也就相当于把油放到后备箱里(注意不是油箱)。当我们无法到达某个加油站的时候,就是在半路熄火了,此时应该从后备箱中拿出最大的那个油桶进行加油,如果仍然不够到达加油站,则继续把后备箱的油拿出来加上。

所以,如果把后备箱的油全部都拿出来用完了仍然不能到达加油站的情况下,则返回-1.否则,由于是个贪心策略,所以使用了最少的加油站的油。

代码的思路是,使用一个大根堆保存经历过的加油站的油量,即放到了后备箱里。使用prev保存上一个加油站的位置,对所有的加油站进行遍历,判断从上一个加油站到当前加油站需要用掉多少油,如果油箱不够用了则贪心使用后备箱中最大的油桶。当后备箱的油全部用完了,仍然不能到达现在的加油站,则返回-1。

注意,python的heapq默认是小根堆,如果想用大根堆,需要在放入数字的时候添加负号。

Python代码如下:

class Solution(object):
def minRefuelStops(self, target, startFuel, stations):
"""
:type target: int
:type startFuel: int
:type stations: List[List[int]]
:rtype: int
"""
# [pos, fuel]
stations.append([target, float("inf")])
# -fuel
que = []
pos = 0
tank = startFuel
res = 0
prev = 0
for p, g in stations:
tank -= p - prev
while que and tank < 0:
tank += -heapq.heappop(que)
res += 1
if tank < 0:
return -1
heapq.heappush(que, -g)
prev = p
return res

参考资料:https://leetcode.com/problems/minimum-number-of-refueling-stops/solution/

日期

2019 年 4 月 3 日 —— 好久不刷题了,越来越手生

【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)的更多相关文章

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

  4. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

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

  6. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  7. 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)

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

  8. 【LeetCode】933. Number of Recent Calls 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 队列 相似题目 参考资料 日期 题目地址: ...

  9. 【LeetCode】750. Number Of Corner Rectangles 解题报告 (C++)

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

随机推荐

  1. expr计算字符串长度

    命令:expr length "quanzhiqinag" #!/bin/bash for N in quan zhi qiang do if [ `expr length $N ...

  2. SQL-用到的数据库语句总结

    0.SELECT * FROM  CHARACTER_SETS LIMIT 0,10   #从CHARACTER_SETS表中,从第1行开始,提取10行[包含第1行] 1.SELECT * FROM  ...

  3. 【模板】二分图最大权完美匹配(KM算法)/洛谷P6577

    题目链接 https://www.luogu.com.cn/problem/P6577 题目大意 给定一个二分图,其左右点的个数各为 \(n\),带权边数为 \(m\),保证存在完美匹配. 求一种完美 ...

  4. dart系列之:HTML的专属领域,除了javascript之外,dart也可以

    目录 简介 DOM操作 CSS操作 处理事件 总结 简介 虽然dart可以同时用作客户端和服务器端,但是基本上dart还是用做flutter开发的基本语言而使用的.除了andorid和ios之外,we ...

  5. HBase【操作Java api】

    一.导入依赖 创建模块,导入以下依赖,maven默认编译版本是1.5,用1.8编译. pom.xml <dependencies> <dependency> <group ...

  6. Flume对接Kafka

    目录 一.简单实现 1)flume的配置文件 二.自定义interceptor(使用kafka sink) 1)自定义 flume 拦截器 2)编写 flume 的配置文件 3)创建topic 4)启 ...

  7. 容器之分类与各种测试(三)——queue

    queue是单端队列,但是在其实现上是使用的双端队列,所以在queue的实现上多用的是deque的方法.(只要用双端队列的一端只出数据,另一端只进数据即可从功能上实现单端队列)如下图 例程 #incl ...

  8. Java 性能优化的 50 个细节

    在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. #尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间, ...

  9. [学习总结]6、Android异步消息处理机制完全解析,带你从源码的角度彻底理解

    开始进入正题,我们都知道,Android UI是线程不安全的,如果在子线程中尝试进行UI操作,程序就有可能会崩溃.相信大家在日常的工作当中都会经常遇到这个问题,解决的方案应该也是早已烂熟于心,即创建一 ...

  10. Linux学习 - 文件系统常用命令

    一.文件系统查看命令df df [选项] [挂载点] -a 查看所有文件系统信息,包括特殊文件系统 -h 使用习惯单位显示容量 -T 显示文件系统类型 -m 以MB为单位显示容量 -k 以KB为单位显 ...