当前位置:网站首页>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;
}
边栏推荐
- Recommended system 1 --- framework
- Steady! Huawei micro certification Huawei cloud computing service practice is stable!
- Wechat applet +php realizes authorized login
- Arc135 a (time complexity analysis)
- 冲击继电器JC-7/11/DC110V
- JS get the attribute values nested in the object
- 我的NVIDIA开发者之旅——优化显卡性能
- 如何获取el-tree中所有节点的父节点
- How to get the parent node of all nodes in El tree
- LC周赛300
猜你喜欢
Compound nonlinear feedback control (2)
ES6 模块化
Take you to quickly learn how to use qsort and simulate qsort
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
BUU-Crypto-[GXYCTF2019]CheckIn
AWT常用组件、FileDialog文件选择框
HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
[Excel] 数据透视图
JS execution mechanism
卸载Google Drive 硬盘-必须退出程序才能卸载
随机推荐
如何避免 JVM 内存泄漏?
FRP intranet penetration, reverse proxy
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
注释与注解
How to determine whether an array contains an element
[Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school
What are the reasons for the frequent high CPU of ECS?
Invalid revision: 3.18.1-g262b901-dirty
Excel 比较日器
724. 寻找数组的中心下标
剑指 Offer II 038. 每日温度
js如何将秒转换成时分秒显示
transformer坑了多少算力
Arc135 C (the proof is not very clear)
High performance parallel programming and optimization | lesson 02 homework at home
报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
ANSYS command