LeetCode(871):最低加油次数 Minimum Number of Refueling Stops(Java)
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] < targetimport 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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【致远OA】快速修复安装A6的方法
- 下一篇: 异度装甲解惑(转载)