当前位置:网站首页>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;
}边栏推荐
- px em rem的区别
- The difference between PX EM rem
- 19. Framebuffer application programming
- How much computing power does transformer have
- AWT常用组件、FileDialog文件选择框
- (4) Canal multi instance use
- QT 获取随机颜色值设置label背景色 代码
- BUU-Crypto-[GUET-CTF2019]BabyRSA
- left_ and_ right_ Net normal version
- 724. Find the central subscript of the array
猜你喜欢
随机推荐
Talk about the SQL server version of DTM sub transaction barrier function
总线的基本概念
VB. Net calls ffmpeg to simply process video (class Library-6)
HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
How to configure static IP for Kali virtual machine
VB. Net simple processing pictures, black and white (class library - 7)
How to solve the component conflicts caused by scrollbars in GridView
谷歌 Chrome 浏览器将支持选取文字翻译功能
Arc135 a (time complexity analysis)
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
Grounding relay dd-1/60
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
AWT常用组件、FileDialog文件选择框
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Flask
MySQL的information_schema数据库
C语言中的函数(详解)
js如何将秒转换成时分秒显示
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式









