当前位置:网站首页>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;
}边栏推荐
- left_ and_ right_ Net normal version
- 19. Framebuffer application programming
- Arc135 a (time complexity analysis)
- 剑指 Offer II 038. 每日温度
- Build an Internet of things infrared temperature measuring punch in machine with esp32 / rush to work after the Spring Festival? Baa, no matter how hard you work, you must take your temperature first
- How to implement lazy loading in El select (with search function)
- Basic concept of bus
- LC weekly 300
- 70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
- LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
猜你喜欢

Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式

Build an Internet of things infrared temperature measuring punch in machine with esp32 / rush to work after the Spring Festival? Baa, no matter how hard you work, you must take your temperature first

Actual cases and optimization solutions of cloud native architecture

C语言中的函数(详解)

Component、Container容器常用API详解:Frame、Panel、ScrollPane

Steady! Huawei micro certification Huawei cloud computing service practice is stable!

Online shrimp music will be closed in January next year. Netizens call No

How to solve the component conflicts caused by scrollbars in GridView

js如何将秒转换成时分秒显示

APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
随机推荐
Kubernets first meeting
接地继电器DD-1/60
Excel comparator
C language simple student management system (including source code)
How to configure static IP for Kali virtual machine
BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
JS arguments parameter usage and explanation
Supplement the JS of a video website to decrypt the video
如何判断数组中是否含有某个元素
js获取对象中嵌套的属性值
ES6 模块化
光模塊字母含義及參數簡稱大全
Descriptive analysis of data distribution characteristics (data exploration)
How to solve the component conflicts caused by scrollbars in GridView
Basic concept of bus
[QT] create mycombobox click event
[Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school
[microservice] Nacos cluster building and loading file configuration
我的NVIDIA开发者之旅——优化显卡性能
Wechat applet +php realizes authorized login