当前位置:网站首页>leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
2022-07-03 01:05:00 【White speed Dragon King's review】
analysis
Be careful , Gas stations are along the way , therefore target It must be on the right side of all gas stations
then , The effect of oil is cumulative , The root jumping game is different , You can't walk for a while and be greedy
We should be greedy with the largest amount of refueling before the maintenance of a large root pile
Thought is , Go somewhere , If the oil after arriving is negative , Then find the maximum value stored in the gas station before and add it , Calculate once
If you can't get everything before you add it, it's cool
Then put the current gas station into the big pile ( Reward ), And update the previous point prev
Be careful ,target It's the last one curr, Count it up
Ac code
class Solution:
def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
# The effect of refueling is cumulative
# So use a big pile , Take the biggest refueling every time to be greedy ( Big root pile )
# If you can't get to the current point after taking it , return -1
ans, nowFuel, prev, pq = 0, startFuel, 0, []
stations.sort()
n = len(stations)
# More than a target The location of
# Right gas station along the way , therefore target It must be on the far right
for i in range(n + 1):
# The current location to go
curr = stations[i][0] if i < n else target
nowFuel -= curr - prev
# Not enough oil , Greedy refueling
while nowFuel < 0 and pq:
nowFuel += -heappop(pq)
ans += 1
# After adding, you can't go
if nowFuel < 0:
return -1
# If you can go to , Put it into the big pile
if i < n:
heappush(pq, -stations[i][1])
# to update prev
prev = curr
return ans
summary
greedy + Big root pile
very classic
边栏推荐
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- 用Go+绘制爱心给心爱的她表白
- [flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
- leetcode-934:最短的桥
- 鏈錶內指定區間反轉
- 1038 Recover the Smallest Number
- Deep analysis of data storage in memory
- Leetcode-2280: represents the minimum number of line segments of a line graph
- [love crash] neglected details of gibaro
- 异步、邮件、定时三大任务
猜你喜欢
【AutoSAR 十二 模式管理】
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
1.12 - 指令
Embrace the safety concept of platform delivery
excel IF公式判断两列是否相同
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
【AutoSAR 十三 NVM】
1.11 - bus
Rk3568 development board evaluation (II): development environment construction
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
随机推荐
全志A40i/T3如何通过SPI转CAN
(C语言)数据的存储
cordova-plugin-device获取设备信息插件导致华为审核不通过
Thank you for being together for these extraordinary two years!
合并K个已排序的链表
leetcode-934:最短的桥
Delete duplicate elements in the ordered linked list -ii
Leetcode-2280: represents the minimum number of line segments of a line graph
MySQL multi table joint deletion
[AUTOSAR VI description document]
The difference between relational database and non relational database
【AutoSAR 四 BSW概述】
【AutoSAR 十 IO架构】
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
比较版本号
File operation io-part2
[shutter] image component (cached_network_image network image caching plug-in)
【AutoSAR 七 工具链简介】
1.11 - bus
18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0