当前位置:网站首页>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
边栏推荐
- Win10 多种方式解决无法安装.Net3.5的问题
- 全志A40i/T3如何通过SPI转CAN
- 18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
- 世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
- 指针初阶(基础)
- Rk3568 development board evaluation (II): development environment construction
- 异步、邮件、定时三大任务
- In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
- Compare version number
- 【AutoSAR 十一 通信相关机制】
猜你喜欢

Thank you for being together for these extraordinary two years!

Vulkan practice first bullet

【AutoSAR 四 BSW概述】

Win10 can't be installed in many ways Problems with NET3.5

拥抱平台化交付的安全理念

Explain the basic concepts and five attributes of RDD in detail

Illustrated network: what is virtual router redundancy protocol VRRP?

tail -f 、tail -F、tailf的区别
![[C language] branch and loop statements (Part 1)](/img/47/6efcc59bd26e26f66c698635c26c8b.png)
[C language] branch and loop statements (Part 1)

瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
随机推荐
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
【AutoSAR 八 OS】
指针初阶(基础)
tail -f 、tail -F、tailf的区别
Merge K sorted linked lists
465. DFS backtracking of optimal bill balance
The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
[AUTOSAR I overview]
[AUTOSAR VI description document]
【AutoSAR 十二 模式管理】
【案例分享】让新时代教育发展与“数”俱进
(C语言)数据的存储
无向图的割点
cordova-plugin-device获取设备信息插件导致华为审核不通过
(C language) data storage
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
Hdu3507 (slope DP entry)
Several cases of recursive processing organization
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
测试右移:线上质量监控 ELK 实战