作者: 负雪明烛
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. 67-Gray Code

    Gray Code My Submissions QuestionEditorial Solution Total Accepted: 60277 Total Submissions: 165212 ...

  2. Linux—yum安装python-pip

    centos下安装pip时失败: [root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, ...

  3. mongodb存储的基本使用

    Python连接mongodb一般使用pymongo模块 1. pymongo模块的简单使用 ### MongoDB存储 ## 连接MongoDB import pymongo # 建立连接对象,2种 ...

  4. 安装octave详解

    1. 一些可以替换的库(可跳过) 默认的库安装libblas.dll.OpenBLAS-v2.6.0-0-54e7b37_dynamicarch_nt4(自动检测CPU类型) 在目录下<your ...

  5. Spark3学习入门【基于Java】

    Spark 是离线数据处理的一种大数据技术,和Flick相比数据处理要延后,因为Flick是实时数据处理,而Spark需要先读取数据到内存. Spark的库是基于Scala写的,虽然Scala也是运行 ...

  6. 生产环境高可用centos7 安装配置RocketMQ-双主双从-同步双写(2m-2s-sync)

    添加hosts信息[四台机器] vim /etc/hosts 192.168.119.130 rocketmq-nameserver1 192.168.119.130 rocketmq-master1 ...

  7. NSString类里有个hash

    实际编程总会涉及到比较两个字符串的内容,一般会用 [string1 isEqualsToString:string2] 来比较两个字符串是否一致.对于字符串的isEqualsToString方法,需要 ...

  8. ActiveRecord教程

    (一.ActiveRecord基础) ActiveRecord是Rails提供的一个对象关系映射(ORM)层,从这篇开始,我们来了解Active Record的一些基础内容,连接数据库,映射表,访问数 ...

  9. 【编程思想】【设计模式】【行为模式Behavioral】command

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/command.py #!/usr/bin/env pyt ...

  10. javascript将平行的拥有上下级关系的数据转换成树形结构

    转换函数 var Littlehow = {}; /** * littlehow 2019-05-15 * 平行数据树形转换器 * @type {{format: tree.format, sort: ...