日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 871. Minimum Number of Refueling Stops 最少加油次数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

LeetCode 871. Minimum Number of Refueling Stops

本題是LeetCode 871題,最少加油次數。

題目描述

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距離遠的目的地,途中有若干加油站,并給出加油站距起點的距離以及油量。汽車到達某加油站可以選擇停下加油,將加油站所有的油轉到車上。每走1英里消耗1升油。油量為0就開不了了,開始時車上有一定量的油。這輛車到達目的地最少需要加油幾次,如果無法到達就返回-1。

解析

本題要求加油次數盡可能的少,可以用貪心的方法解決,能不加就不加,非得加就加最多的(反正都算一次)。每遇到一個加油站如果油量充足,就把這個加油站的油量存下來,如果遇到油量不足以開到下一站(或終點)就從存下來的油量里找最大的加,從大到小,直到滿足開到下一站,如果全加上都開不到,那就說明無法到達。
C++代碼如下:

int minRefuelStops(int target, int startFuel, vector<vector<int>> & stations) {//greedyif (startFuel >= target) return 0;else if (stations.empty()) return -1;int cur = startFuel;priority_queue<int> f;int next = 0;int cnt = 0;while (next < stations.size() && cur >= stations[next][0] || !f.empty()) {//if the car can reach the next station, no need to refuel, save the fuel in a priority queuewhile ((next < stations.size() && cur >= stations[next][0])) f.push(stations[next++][1]);cur += f.top();f.pop();++cnt;if (cur >= target) return cnt;}return -1; }

首先如果初始油量大于目的地距離,無需加油一定可以到達,返回0即可;
反之如果初始油量不夠但是又沒有加油站,那一定到不了,返回-1即可。
其他情況,我們需要順序遍歷所有加油站,當然題目包含提示加油站信息是按照距離從近到遠給出的,所以無需我們再排序了。
為了從大到小的保存油量信息,我們用到了優先隊列。這里使用從大到小的優先隊列(top最大)由于是模板默認,簡略寫了,寫全的話應該是priority_queue<int,vector<int>,less<int>> f用于保存能夠到達但是沒有加油的那些加油站的油量,以備不時之需。
然后定義cur是當前油量,next是下一個加油站索引,初始為0
那么車什么時候能繼續開呢,分兩種情況,一是當前油量足以堅持到下一個加油站(當然要先驗證next索引不越界),或者保存的油量f不為空(這樣我們可以在對應的加油站先加好油)
這種情況下,如果屬于能夠開到下一站,那么就一直開,不加油,同時將油量放入優先隊列。如果開不到下一站,就從優先隊列從大到小(從top)依次取油,等于在對應的加油站加了一次油,加油次數要加一,同時優先隊列彈出top元素。在過程中判斷cur是否超過了target距離,超過了說明能夠開到終點,后續都不用加油,返回當前的加油次數即可。
如果上述條件不滿足了(跳出了外層while循環)說明無法到達下一站并且沒油可加,那顯然到不了終點了,返回-1.
注意這里并沒有計算每經過一段距離油量的變化,因為題目給出的都是終點以及加油站距離起點的坐標,所以cur記錄的是累積的油量,累積油量大于到起點的距離就證明可達。

總結

以上是生活随笔為你收集整理的LeetCode 871. Minimum Number of Refueling Stops 最少加油次数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。