当前位置:网站首页>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;
}
边栏推荐
- Experience weekly report no. 102 (July 4, 2022)
- 光模塊字母含義及參數簡稱大全
- 19. Framebuffer application programming
- Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
- Excel 比较日器
- Thinkphp6.0 middleware with limited access frequency think throttle
- BUU-Reverse-easyre
- Introduction To AMBA 简单理解
- 总线的基本概念
- el-select如何实现懒加载(带搜索功能)
猜你喜欢
Supplement the JS of a video website to decrypt the video
[QT] create mycombobox click event
Take you to quickly learn how to use qsort and simulate qsort
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
云原生架构实战案例及优化解决方案
Google Chrome browser will support the function of selecting text translation
JS arguments parameter usage and explanation
My NVIDIA developer journey - optimizing graphics card performance
Solar insect killing system based on single chip microcomputer
随机推荐
1.1 history of Statistics
Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
BUU-Reverse-easyre
如何判断数组中是否含有某个元素
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
每周小结(*63):关于正能量
AWT常用组件、FileDialog文件选择框
Online shrimp music will be closed in January next year. Netizens call No
win10清除快速访问-不留下痕迹
How to determine whether an array contains an element
gslb(global server load balance)技术的一点理解
Basic concept of bus
VB. Net GIF (making and disassembling - optimizing code, class library - 5)
Design and implementation of tcp/ip series overview
js获取对象中嵌套的属性值
SQL injection - injection based on MSSQL (SQL Server)
Halcon image calibration enables subsequent image processing to become the same as the template image
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
webrtc 快速搭建 视频通话 视频会议
left_and_right_net可解释性设计