当前位置:网站首页>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
边栏推荐
- 世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
- 测试右移:线上质量监控 ELK 实战
- 465. DFS backtracking of optimal bill balance
- 465. 最优账单平衡 DFS 回溯
- 有向图的强连通分量
- Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
- [AUTOSAR II appl overview]
- [shutter] image component (configure local GIF image resources | load placeholder with local resources)
- 数据分析思维分析犯法和业务知识——分析方法(一)
- 【AutoSAR 二 AppL概述】
猜你喜欢

【AutoSAR 六 描述文件】

用Go+绘制爱心给心爱的她表白

RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide

基于ARM RK3568的红外热成像体温检测系统

FPGA - 7 Series FPGA internal structure clocking -04- multi area clock

Key detection and sinusoidal signal output developed by Arduino

Teach you JDBC hand in hand -- structure separation
![[shutter] image component (cached_network_image network image caching plug-in)](/img/cc/967ff62c7f82e1c6613b3d0f26bb3e.gif)
[shutter] image component (cached_network_image network image caching plug-in)

Advanced pointer (I)

【AutoSAR 九 C/S原理架构】
随机推荐
(C语言)数据的存储
465. DFS backtracking of optimal bill balance
leetcode-849:到最近的人的最大距离
百度智能云牵头打造智能云综合标准化平台
[AUTOSAR I overview]
Correctly distinguish the similarities and differences among API, rest API, restful API and web service
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
删除有序链表中重复的元素-II
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Arduino开发之按键检测与正弦信号输出
leetcode-241:为运算表达式设计优先级
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
Inversion de l'intervalle spécifié dans la liste des liens
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
465. 最优账单平衡 DFS 回溯
1.11 - bus
leetcode-871:最低加油次数
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
Embrace the safety concept of platform delivery
Sentry developer contribution Guide - configure pycharm