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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

文章目錄

  • 題目
  • 算法思路
  • C++代碼

題目

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.

  • 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
  • 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.

    算法思路

    本周繼續學習動態規劃的思想,本題也是使用動態規劃的案例,不過這個題目尋找恰當的子問題會困難一些。

    我們考慮以maxlen(n)作為為經過n個加油站能到達的最遠距離,接下來我們考慮怎么沿著加油站列表更新maxlen(n)的值。

  • n=0時,顯然maxlen(0) = startfuel
  • 逐步考慮前i個加油站的maxlen(n)數組,當添加一個新的加油站i時,原maxlen數組中maxlen[j] >= stations[i][0]的部分,即能到達新加油站的值能更新后一個值,maxlen[j+1] = (maxlen[j+1],maxlen[j] + stations[i][1])
  • C++代碼

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

    總結

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

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

    主站蜘蛛池模板: 天堂综合在线 | 欧美久久一区二区三区 | 999av视频| 又爽又黄又无遮挡 | 日本一区二区观看 | 操女人网 | 日本va欧美va精品发布 | 激情五月色婷婷 | 亚洲精品乱码久久 | 日本不卡一区二区在线观看 | 国产欧美一区二 | 91爱爱网 | 老女人毛片50一60岁 | 久久国产精品无码一级毛片 | 亚洲女同视频 | 五月中文字幕 | 久久94| 精品久久香蕉国产线看观看亚洲 | 一级黄色性片 | 成人爽爽爽| 久久久久久久久久一区二区三区 | 亚洲欧美另类图片 | 亚洲精选av | 日韩一级久久 | 日本中文字幕在线不卡 | 超碰人人在线观看 | av青青 | 天堂网av在线 | 少妇一级淫片免费放播放 | 欧美xxxx8888 | 免费一级毛片麻豆精品 | 日本精品一区视频 | 台湾佬成人中文网222vvv | 欧美另类69xxxx | 亚洲嫩草影院 | 中文av在线播放 | 精品人妻一区二区三区蜜桃视频 | 97精品一区二区 | 久久精品国产免费 | 久久久久久麻豆 | 波多野吉衣一区二区 | 国产操视频 | 日韩av片在线免费观看 | 亚洲精品666| 日韩www在线观看 | 久久亚洲AV成人无码一二三 | 最近中文字幕在线免费观看 | 亚洲麻豆av | 亚洲图片自拍偷拍区 | 精品区| 91传媒入口 | 欧美一及片 | 亚洲av日韩av永久无码下载 | 国模吧一区二区三区 | 国产日产欧洲无码视频 | 天天想你在线观看完整版电影高清 | 懂色av一区二区三区 | 久久98| 插插插日日日 | 女人下部全棵看视频 | 松本一香在线播放 | 日本美女视频 | 青青操免费在线视频 | 男人天堂中文字幕 | 丰满少妇毛片 | 欧美一区二区三区四区视频 | 欧美一区二区三区系列电影 | 国产 第1190页 | 天天弄 | 欧美成人免费高清视频 | 樱空桃在线 | 国产sss| 国产精品久久久久久久久免费看 | 别揉我奶头啊嗯一区二区 | 色综合88| 精品久久久久久久久久久久久久久久 | www.插插插| 伊人青青 | 久久精品香蕉 | 欧美一区二区三区日韩 | 久久人体 | 免费一区二区三区视频在线 | 欧美另类国产 | 成人在线三级 | 国产精品一区二区三区在线免费观看 | 波多野av在线 | 国产69精品久久久久久久久久 | 99re热在线视频 | 欧美激情国产精品 | 日韩欧美a级片 | 999热| 国产中文字幕在线免费观看 | 黄色网av | 亚洲涩涩涩 | 国产麻豆精品一区 | 韩国美女啪啪 | 99视频热 | 日本黄色的视频 | 手机电影在线观看 |