当前位置:网站首页>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
边栏推荐
猜你喜欢
excel表格计算时间日期的差值,并转化为分钟数
File operation io-part2
研发一款国产ARM智能边缘计算网关需要什么
Vulkan practice first bullet
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
How to convert Quanzhi a40i/t3 to can through SPI
[AUTOSAR II appl overview]
[AUTOSAR nine c/s principle Architecture]
[AUTOSAR VI description document]
无向图的割点
随机推荐
1.11 - bus
解决ReactNative使用webView存在缓存问题
leetcode-224:基本计算器
Solve the cache problem of reactnative using WebView
lex && yacc && bison && flex 配置的問題
Leetcode 294. Flip game II (game theory)
百度智能云牵头打造智能云综合标准化平台
链表内指定区间反转
寻找标杆战友 | 百万级实时数据平台,终身免费使用
【AutoSAR 九 C/S原理架构】
[AUTOSAR + IO Architecture]
Leetcode-934: the shortest Bridge
飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
关于Fibonacci数列
链表中的节点每k个一组翻转
Linear programming of mathematical modeling (including Matlab code)
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
深度剖析数据在内存中的存储
(C language) data storage
Vulkan practice first bullet