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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

871. Minimum Number of Refueling Stops

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

題目:

A car travels from a starting position to a destination which is?target?miles east of the starting position.

There are gas stations along the way. The gas stations are represented as an array?stations?where?stations[i] = [positioni, fueli]?indicates that the?ith?gas station is?positioni?miles east of the starting position and has?fueli?liters of gas.

The car starts with an infinite tank of gas, which initially has?startFuel?liters of fuel in it. It uses one liter of gas per one 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.

Return?the minimum 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 not 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.

Constraints:

  • 1 <= target, startFuel <= 109
  • 0 <= stations.length <= 500
  • 0 <= positioni?<= positioni+1?< target
  • 1 <= fueli?< 109

思路1:

第一反應是用遞歸來做,f[i][j]表示經過第 i 個加油站后,加油 j 次能夠到達的最大距離,最后只需要找到最小的 j 并且f[i][j]大于等于target即可。狀態轉移方程中f[i][j]可以從三個地方來:1)f[i][j - 1],代表經過了當前車站,但是不在當前車站加油;2)f[i - 1][j],表示到達了第i - 1即前一個車站,并且加了j次油,即上個車站沒加油;3)f[i - 1][j - 1] + stations[i - 1][1],即經過了第i - 1個車站,并且在上個車站加了油,當然這里有條件就是f[i - 1][j - 1]的最大距離必須要比stations[i - 1][0]大,不然無法到達上個車站。初始化我們用 n = stations.size() + 1,即f[i][j]當 j 等于0 時表示f[i][0]都是沒加過油的,那么它們的值應該為startFuel,至于f[0][j],不需要初始化,因為j一定小于等i,即加油次數不能比路過的車站多。

代碼1:

class Solution {
public:
? ? int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
? ? ? ? int n = stations.size() + 1;
? ? ? ? vector<vector<long>> f(n, vector<long>(n));
? ? ? ? for (int i = 0; i < n; i++) {
? ? ? ? ? ? f[i][0] = startFuel;
? ? ? ? } ? ? ?
? ? ? ? for (int i = 1; i < n; i++) {
? ? ? ? ? ? for (int j = 1; j <= i; j++) {
? ? ? ? ? ? ? ? if (f[i - 1][j - 1] >= stations[i - 1][0]) {
? ? ? ? ? ? ? ? ? ? f[i][j] = f[i - 1][j - 1] + stations[i - 1][1];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? f[i][j] = max({f[i - 1][j], f[i][j - 1], f[i][j]});
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? for (int j = 0; j < n; j++) {
? ? ? ? ? ? for (int i = 0; i < n; i++) {
? ? ? ? ? ? ? ? if (f[i][j] >= target)
? ? ? ? ? ? ? ? ? ? return j;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return -1;
? ? }
};

思路2:

從本質上來看,題目要求我們需要在最少的加油次數中加最多的油,即每次加油都加能夠加的最多的油。但是我們每次路過車站,其實只有當前車站可以選擇,那么怎么構成選項呢?答案是每次路過車站先不加油,把能夠加的油記錄進一個優先隊列,等到需要加油的時候我們再取出來為自己加油。這里優先隊列是為了管理最大值,使我們能夠隨時取出來的都是最多的油。首先我們能夠到達的最大距離dist就是startFuel,之后判斷dist和target,一旦dist大于等于target即可結束。循環中,每次我們路過車站,如果當前dist能夠到達車站,那我們就先不加油,而是把油加入優先隊列;如果不能到達車站,那么就加油,如果優先隊列已經無油可加,證明我們不能到達target,直接返回-1;如果優先隊列有油,那么就拿出最大的油,加給dist,同時更新加油次數即可。

代碼2:

class Solution {
public:
? ? int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
? ? ? ? priority_queue<int> q;
? ? ? ? int dist = startFuel, count = 0;
? ? ? ? int i = 0;
? ? ? ? while (dist < target) {
? ? ? ? ? ? if (i < stations.size() && dist >= stations[i][0]) {? //能到車站說明油暫時是夠的
? ? ? ? ? ? ? ? q.push(stations[i][1]);? //先不加油,存起來
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? if (q.size()) {? //有油可加
? ? ? ? ? ? ? ? ? ? dist += q.top();? ? //加油
? ? ? ? ? ? ? ? ? ? q.pop();
? ? ? ? ? ? ? ? ? ? count++;? //更新次數
? ? ? ? ? ? ? ? } else {? //無油可加,無法到達target
? ? ? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return count;
? ? }
};

總結

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

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

主站蜘蛛池模板: 四虎精品一区二区三区 | 日韩精品国产一区二区 | 精品日韩一区二区三区 | 欧美日韩国产在线一区 | 人妖av在线 | 天天影视色 | 一本色道久久综合狠狠躁的推荐 | 亚洲一区天堂 | 久久重口味| 福利一区二区在线 | 蜜桃臀一区二区三区 | 国产亚洲精品久久久久久 | 欧美网站免费 | 国产xxxx做受性欧美88 | 欧美 日韩 国产 一区二区三区 | 国产亚洲精品电影 | 妹子干综合 | 麻豆亚洲精品 | 成人欧美一区二区三区黑人免费 | 欧美视频精品在线 | 午夜性剧场 | 亚洲精品免费观看 | 噜噜噜噜噜色 | 不卡影院一区二区 | 日韩欧美一区二区在线 | 久久国产这里只有精品 | 久草资源网站 | 天堂999 | 色偷偷av | 免费看的一级片 | 91免费视频网站 | 白丝开裆喷水 | 国内精品久久久久久久影视简单 | 18性xxxxx性猛交 | 一级黄色影院 | 欧美精品自拍 | 日日摸天天添天天添破 | chinese hd av| 夜夜骑av | 自拍偷拍视频网 | a视频 | 第四色在线视频 | 日本久久一区二区 | 中文一区在线观看 | 色夜av | 日韩在线三区 | 黄色视屏网站 | wwwxxoo| 久草热线 | 精品国产一区二区三区噜噜噜 | 91射区 | videos另类灌满极品另类 | 精品久久久久一区二区国产 | 国产乱国产乱老熟 | 欧美日韩成人一区二区三区 | 高清国产一区二区 | www.色呦呦| 国产精品久久久久久久成人午夜 | 韩国主播青草200vip视频 | 九色精品视频 | 欧美人与禽猛交乱配 | 亚洲人人干| 美女国产一区 | 噼里啪啦动漫高清在线观看 | 桃色成人网 | 国产精品1 | 免费视频久久 | 亚洲91色 | 欧美日韩一级视频 | 狐狸视频污 | 日本五十肥熟交尾 | av永久 | 久久这里只有精品6 | 春物催眠 | 国产一区二区在线精品 | 午夜神器在线观看 | 视色网| 给我看高清的视频在线观看 | 欧美50p| 国内自拍视频网站 | 久久丝袜美腿 | 国产黄视频在线观看 | 国产成人精品一区二区在线小狼 | 日韩黄色a级片 | 国产无套免费网站69 | 久久国产精品精品国产色婷婷 | 中文字幕资源在线 | 成人一区二区av | 国产精品ww| 色伊人 | 超碰在线观看av | 色五夜 | 欧美色图影院 | 亚洲国产日本 | 伊人福利视频 | 看av在线| 后进极品美女圆润翘臀 | 欧美xxxbbb| 久久综合丁香 |