当前位置:网站首页>2022.7.2-----leetcode.871
2022.7.2-----leetcode.871
2022-07-04 05:53:00 【路Lu727】
public int minRefuelStops(int target, int startFuel, int[][] stations) {
//某次路程可以选择的油量
PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> b - a);
int ans = 0,end = startFuel;//某次路程的最大距离
int n = stations.length;
for (int i = 0; i < n; i++) {
if(end>=target) return ans;//某次路程可以到达终点则返回
//到达最大距离后,选择最大的油量,直到足够到达下一站点,开始下一次路程
if(stations[i][0]>end){
while(!pq.isEmpty()) {
end+=pq.poll();
ans++;
if(end>=stations[i][0]) break;
}
if(end<stations[i][0]) return -1;//不能到达下一个站点
}
pq.add(stations[i][1]);//添加可到达站点的油量
}
//经过所有站点后,判断能否到达终点
if(end<target){
while(!pq.isEmpty()) {
end+=pq.poll();
ans++;
if(end>=target) return ans;
}
return -1;
}
return ans;
}
边栏推荐
- XII Golang others
- left_ and_ right_ Net interpretable design
- gslb(global server load balance)技术的一点理解
- Introduction To AMBA 简单理解
- VB. Net simple processing pictures, black and white (class library - 7)
- Kubernets first meeting
- Win10 clear quick access - leave no trace
- Arc135 a (time complexity analysis)
- 【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
- "In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
猜你喜欢
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
BUU-Crypto-[GXYCTF2019]CheckIn
SQL injection - injection based on MSSQL (SQL Server)
1480. Dynamic sum of one-dimensional array
webrtc 快速搭建 视频通话 视频会议
冲击继电器JC-7/11/DC110V
A little understanding of GSLB (global server load balance) technology
剑指 Offer II 038. 每日温度
ansys命令
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
随机推荐
检漏继电器JY82-2P
光模塊字母含義及參數簡稱大全
BUU-Reverse-easyre
VB. Net simple processing pictures, black and white (class library - 7)
One click filtering to select Baidu online disk files
复合非线性反馈控制(二)
Input displays the currently selected picture
Configure cross compilation tool chain and environment variables
Introduction To AMBA 简单理解
Compound nonlinear feedback control (2)
Redis realizes ranking function
Actual cases and optimization solutions of cloud native architecture
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
Google Chrome browser will support the function of selecting text translation
19. Framebuffer application programming
tutle时钟改进版
BUU-Crypto-[GXYCTF2019]CheckIn
XII Golang others
Leakage detection relay jy82-2p
MySQL的information_schema数据库