当前位置:网站首页>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
边栏推荐
- Rk3568 development board evaluation (II): development environment construction
- Basic use of sringcloud & use of component Nacos
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- matlab 多普勒效应产生振动信号和处理
- 465. DFS backtracking of optimal bill balance
- Embrace the safety concept of platform delivery
- leetcode-849:到最近的人的最大距离
- 鏈錶內指定區間反轉
- The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
猜你喜欢

tail -f 、tail -F、tailf的区别

Thank you for being together for these extraordinary two years!
![[overview of AUTOSAR three RTE]](/img/6a/0df33beb42f165af77a17b5d8b01e2.png)
[overview of AUTOSAR three RTE]
![[AUTOSAR nine c/s principle Architecture]](/img/59/ce32c0ff58ef5d8385fe950136175b.png)
[AUTOSAR nine c/s principle Architecture]
![[AUTOSAR + IO Architecture]](/img/cf/9ea42b50bed298c0546764b63bd957.png)
[AUTOSAR + IO Architecture]

Embrace the safety concept of platform delivery

Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size

(C语言)数据的存储

leetcode-2280:表示一个折线图的最少线段数
![[overview of AUTOSAR four BSW]](/img/19/c2273bbedb7f8d859e5a3805ed5740.png)
[overview of AUTOSAR four BSW]
随机推荐
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Assets, vulnerabilities, threats and events of the four elements of safe operation
Basic use of sringcloud & use of component Nacos
[overview of AUTOSAR three RTE]
KingbaseES ALTER TABLE 中 USING 子句的用法
Thread start and priority
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
Sentry developer contribution Guide - configure pycharm
[AUTOSAR II appl overview]
[AUTOSAR five methodology]
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
【案例分享】让新时代教育发展与“数”俱进
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
详解RDD基本概念、RDD五大属性
leetcode-871:最低加油次数
指针进阶(一)