作者: 负雪明烛
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. 解决Gitlab的The remote end hung up unexpectedly错误,解决RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large问题

    解决Gitlab的The remote end hung up unexpectedly错误 解决RPC failed; HTTP 413 curl 22 The requested URL retu ...

  2. NFS FTP SAMBA的区别

    Samba服务 samba是一个网络服务器,用于Linux和Windows之间共享文件. samba端口号 samba (启动时会预设多个端口) 数据传输的TCP端口 139.445 进行NetBIO ...

  3. mysql 计算日期为当年第几季度

    select T21620.日期 as F21634, QUARTER('98-04-01')  as quarter                       #返回日期是一年的第几个季度   - ...

  4. markdown语法之如何使用LaTeX语法编写数学公式

    CSDN-markdown语法之如何使用LaTeX语法编写数学公式 目录 目录 正文 标记公式 行内公式 块级公式 上标和下标 分数表示 各种括号 根号表示 省略号 矢量表示 间隔空间 希腊字母 特殊 ...

  5. 07 MySQL安装图解--Windows版本

    MySQL安装图解 使用微信扫码关注微信公众号,并回复:"MySQL环境",免费获取下载链接! 1.安装MySQL 2.校验MySQL 3.登录MySQL 登录MySQL:mysq ...

  6. == 和 equals() 方法的区别

    == 在比较基本数据类型时,是比较两边的数据的值是否相等 // 整数类型 int num1 = 1; // 双精度浮点数类型 double num2 = 1.0; // 输出结果为 true Syst ...

  7. [PE结构]导出表结构浅析

    导出函数的总数-->以导出函数序号最大的减最小的+1,但导出函数序号是可自定义的,所以NumbersOfFunctions是不准确的 1.根据函数名称找,函数名称表->对应索引函数序号表中 ...

  8. MySQL(4):卸载MySQL

    MySQL的安装是比较复杂的,一旦安装出现错误或者出现其他问题,我们想要完全卸载MySQL也是非常麻烦的,下面简单说下怎样可以完全干净的卸载MySQL 卸载步骤 第一步:用管理员的身份打开命令窗口,关 ...

  9. webpack配置(vue)

    Vue-loader Vue-loader 是一个加载器,能把 .vue 文件转换为js模块. Vue Loader 的配置和其它的 loader 不太一样.除了将 vue-loader 应用到所有扩 ...

  10. 使用fastDFS上传和下载图片文件

    package com.xuecheng.test.fastdfs;import org.csource.common.MyException;import org.csource.fastdfs.* ...