2019.7.25 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新)

这道智力题让我想起了LeetCode 871:加油站。

可以很容易能够想到用回溯法来做,每次经过加油站选择加油或者不加,就是一棵简单的二叉决策树,但是会报超时。

这道题可以用贪心策略做。用一个最大堆模拟后备箱,之后尽可能地将汽油放至后备箱,每次从最多的一桶油开始使用(没有用过的油相当于没有在加油站添加)

1.在汽车当前油量无法到达终点时,将车子开至当前油量能够到达的最远加油站,将途径所有加油站的油装至后备箱

2.如果没有途径加油站,汽车将无法到达终点,返回-1

3.将后备箱当前最多的一桶油给汽车加上,继续前进


传送门:最低加油次数

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.

汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。

沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 station[i][0] 英里处,并且有 station[i][1] 升汽油。

假设汽车油箱的容量是无限的,其中最初有 startFuel 升燃料。它每行驶 1 英里就会用掉 1 升汽油。

当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。

为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 -1 。

注意:如果汽车到达加油站时剩余燃料为 0,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 0,仍然认为它已经到达目的地。

示例 1:
输入:target = 1, startFuel = 1, stations = []
输出:0
解释:我们可以在不加油的情况下到达目的地。示例 2:
输入:target = 100, startFuel = 1, stations = [[10,100]]
输出:-1
解释:我们无法抵达目的地,甚至无法到达第一个加油站。示例 3:
输入:target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
输出:2
解释:
我们出发时有 10 升燃料。
我们开车来到距起点 10 英里处的加油站,消耗 10 升燃料。将汽油从 0 升加到 60 升。
然后,我们从 10 英里处的加油站开到 60 英里处的加油站(消耗 50 升燃料),
并将汽油从 10 升加到 50 升。然后我们开车抵达目的地。
我们沿途在1两个加油站停靠,所以返回 2 。提示:
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


import java.util.PriorityQueue;
import java.util.Queue;/*** 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.* 汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。* 沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 station[i][0] 英里处,并且有 station[i][1] 升汽油。* 假设汽车油箱的容量是无限的,其中最初有 startFuel 升燃料。它每行驶 1 英里就会用掉 1 升汽油。* 当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。* 为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 -1 。* 注意:如果汽车到达加油站时剩余燃料为 0,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 0,仍然认为它已经到达目的地。*/public class MinimumNumberofRefuelingStops {public static void main(String[] args) {int[][] stations = {{10, 60}, {20, 30}, {30, 30}, {60, 40}};System.out.println(minRefuelStops1(100, 10, stations));}//回溯法(会超时)public static int minRefuelStops(int target, int startFuel, int[][] stations) {return Solution(0, 0, target, startFuel, stations, -1);}public static int Solution(int location, int count, int target, int curfuel, int[][] stations, int laststaidx) {int maxdistance = location + curfuel;if (maxdistance >= target)return count;if (laststaidx == stations.length - 1 || maxdistance < stations[laststaidx + 1][0])return -1;int newloc = stations[laststaidx + 1][0];int chooseres = Solution(newloc, count + 1, target, curfuel - (newloc - location) + stations[laststaidx + 1][1], stations, laststaidx + 1);int abandonres = Solution(newloc, count, target, curfuel - (newloc - location), stations, laststaidx + 1);return chooseres > 0 && abandonres > 0 ? Math.min(chooseres, abandonres) : Math.max(chooseres, abandonres);}//贪心public static int minRefuelStops1(int target, int startFuel, int[][] stations) {//建立最大堆模拟后备箱Queue<Integer> priorityQueue = new PriorityQueue<>((Integer i1, Integer i2) -> Integer.compare(i2, i1));int currentFuel = startFuel;int times = 0;int currentPosition = 0;int stationsnums = stations.length;//在汽车当前油量无法到达终点,不断进行加油前进至一个最远可达的加油站while (currentFuel < target) {//将车子开至当前油量能够到达的最远加油站,将途径所有加油站的油装至后备箱while (currentPosition < stationsnums && stations[currentPosition][0] <= currentFuel) {priorityQueue.add(stations[currentPosition++][1]);}//如果没有途径加油站,汽车将无法到达终点if (priorityQueue.isEmpty())return -1;//贪心:将后备箱当前最多的一桶油给汽车加上,继续前进currentFuel += priorityQueue.poll();times++;}return times;}
}

#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#

LeetCode(871):最低加油次数 Minimum Number of Refueling Stops(Java)相关推荐

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

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

  2. LeetCode 871. 最低加油次数

    最近刷LeetCode题目的一些思路,题目信息 汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处. 沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 ...

  3. 79. Leetcode 871. 最低加油次数 (堆-技巧三-事后小诸葛)

    技巧三 - 事后小诸葛这个技巧指的是:当从左到右遍历的时候,我们是不知道右边是什么的,需要等到你到了右边之后才知道.如果想知道右边是什么,一种简单的方式是遍历两次,第一次遍历将数据记录下来,当第二次遍 ...

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

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

  5. LeetCode刷题:871. Minimum Number of Refueling Stops

    LeetCode刷题:871. Minimum Number of Refueling Stops 原题链接:https://leetcode.com/problems/minimum-number- ...

  6. LeetCode: 871. Minimum Number of Refueling Stops

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

  7. LeetCode:871. Minimum Number of Refueling Stops - Python

    问题描述: 871. 最低加油次数 汽车从起点出发驶向目的地,该目的地位于出发位置东面 target英里处. 沿途有加油站,每个station[i] 代表一个加油站,它位于出发位置东面 station ...

  8. 871. Minimum Number of Refueling Stops

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

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

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

最新文章

  1. 算法---删除排序链表中的重复元素 II
  2. 【单页应用巨坑之History】细数History带给单页应用的噩梦
  3. 动态规划1--最长公共子序列
  4. Vue基础进阶 之 实例方法--生命周期
  5. vba 修改下拉列表_Excel隐藏的超实用技能,涉及VBA技巧,建议【收藏】
  6. Active Directory的用户属性说明
  7. SAP License:工程设备采购倒扣备品价
  8. 面试题:Two Sum
  9. mysql配置文件在哪_windows下的mysql配置文件在哪
  10. Windows server 2008 R2和Windows server 2003系统有什么区别?开服用什么系统的更好?
  11. 对嵌入式开发方向的一些思考:在物联网方向
  12. 个人银行账户管理程序(C++)
  13. 做IT民工还是IT精英?
  14. 为什么人需要一个人静静--《孤独力》的读后感
  15. 祭奠一位我无比亲爱的亲人的离去
  16. js 函数function用法
  17. 【Theano】安装教程
  18. 学习笔记-----Material design
  19. 上海世博会展示未来6大生活趋势
  20. Web前端开发工程师与UI设计师的区别是什么?

热门文章

  1. 【CVPR智慧城市挑战赛】无监督交通异常检测,冠军团队技术分享
  2. 什么是SSL数字证书
  3. “火星人”马斯克推论:世界很大可能性是被编程的,上帝可能是个程序员!
  4. JAVA数码宝贝_我的世界1.7.10数码宝贝
  5. 计算机科学概论(第12版)下载
  6. 旧金山大学计算机科学,旧金山大学计算机科学理学硕士研究生申请要求及申请材料要求清单...
  7. 3.2 使用直线段工具标注尺寸信息 [Illustrator CC教程]
  8. 金融数据类——美国对冲基金持仓
  9. 全国青少年软件编程等级考试--scratch-三级-真题-五彩糖葫芦
  10. vue 界面在苹果手机上滑动点击事件等卡顿解决方案