871.Minimum Number of Refueling Stops(加油站的最小数量)

  • Description
      • Difficulty: hard
    • Example 1:
    • Example 2:
    • Example 3:
    • Note:
  • 分析
  • 参考代码

Description

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.

题目链接:https://leetcode.com/problems/minimum-number-of-refueling-stops/
个人主页:http://redtongue.cn or https://redtongue.github.io/

Difficulty: hard

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

分析

  1. updating

参考代码

class Solution:
def minRefuelStops(self, target, startFuel, stations):def _inter(fuel,stations,index):if(len(stations)==0):if(fuel>=target):return indexelse:return -1else:position,fu=stations[0]if(fuel < position):return -1else:x1=_inter(fuel+fu,stations[1:],index+1)x2=_inter(fuel,stations[1:],index)if(x1 != -1):if(x2!=-1):return min(x1,x2)else:return x1else:return x2return _inter(startFuel,stations,0)

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

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

  3. LeetCode 871. Minimum Number of Refueling Stops 最少加油次数

    LeetCode 871. Minimum Number of Refueling Stops 本题是LeetCode 871题,最少加油次数. 题目描述 A car travels from a s ...

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

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

  5. LeetCode: 871. Minimum Number of Refueling Stops

    LeetCode: 871. Minimum Number of Refueling Stops 题目描述 A car travels from a starting position to a de ...

  6. [Swift]LeetCode871. 最低加油次数 | Minimum Number of Refueling Stops

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

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

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

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

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

  9. 【LeetCode 871】 Minimum Number of Refueling Stops

    题目描述 A car travels from a starting position to a destination which is target miles east of the start ...

最新文章

  1. 在win7怎么使用linux指令,Win7系统的Powershell命令怎么使用
  2. RHEL6关闭IPv6
  3. 【AwayPhysics学习笔记】:Away3D物理引擎的简介与使用
  4. SAP采购订单税码增强检查
  5. OpenCV语义细分深度学习网络的实例(附完整代码)
  6. TVM:源码编译安装
  7. 为什么“消费降级”突然火了?数字基尼系数给你一点理论支撑
  8. 【Flink】Flink 源码之ExecutionGraph
  9. 在 const 和 non-const 重载的成员函数中避免代码重复
  10. iOS13免越狱修改微信提示音方法!亲测有用!
  11. 华为荣耀手机 (HUAWEI Honor V9) USB 调试 - ADB 调试
  12. Paragon NTFS 15Mac上NTFS分区的必备工具
  13. 树莓派系统安装和调试
  14. vue 自定义指令 directive
  15. 利用139邮箱的免费短信提醒作为报警接口
  16. Ubuntu 16.04版本的 网易云音乐 linux 安装包资源以及下载过程
  17. 车间调度标准测试集汇总-FJSP、PFSP、JSP、HFSP和分布式车间调度测试集
  18. ubuntu占用空间清理
  19. 【装修选材】自然系原木,才是空间真正的百搭之王!
  20. 【笔记】MOS导通条件

热门文章

  1. Vuex-状态管理(24)
  2. 【51nod1326】遥远的旅途
  3. LEAD_LAG:提前和滞后算法
  4. SAP HANA HDBTable定义table语法
  5. ATFX:美国9月PCE物价指数晚间发布,会出现意外吗?
  6. 2021-2025年中国SWIR-InGaAs光电二极管线阵行业市场供需与战略研究报告
  7. Dowiedz si? wi?cej o Nike Free Run
  8. C中输入半径计算球体的体积
  9. 【集训日志】 金华集训
  10. js 带笔锋 签字版_袁大头签字版市值几十万