当前位置:网站首页>2022.7.2-----leetcode. eight hundred and seventy-one
2022.7.2-----leetcode. eight hundred and seventy-one
2022-07-04 05:55:00 【Lu 727】
public int minRefuelStops(int target, int startFuel, int[][] stations) {
// The amount of fuel you can choose for a journey
PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> b - a);
int ans = 0,end = startFuel;// The maximum distance of a journey
int n = stations.length;
for (int i = 0; i < n; i++) {
if(end>=target) return ans;// If you can reach the end of a journey, you will return
// After reaching the maximum distance , Select the maximum fuel volume , Until it's enough to reach the next station , Start the next journey
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;// Unable to reach the next site
}
pq.add(stations[i][1]);// Add the amount of fuel that can reach the station
}
// After passing all sites , Judge whether you can reach the end
if(end<target){
while(!pq.isEmpty()) {
end+=pq.poll();
ans++;
if(end>=target) return ans;
}
return -1;
}
return ans;
}
边栏推荐
- FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
- left_ and_ right_ Net normal version
- JS execution mechanism
- Sword finger offer II 038 Daily temperature
- 剑指 Offer II 038. 每日温度
- (4) Canal multi instance use
- 检漏继电器JY82-2P
- Nexus 6p从8.0降级6.0+root
- 509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
- Steady! Huawei micro certification Huawei cloud computing service practice is stable!
猜你喜欢
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
gslb(global server load balance)技术的一点理解
Leakage detection relay jy82-2p
BUU-Crypto-[GXYCTF2019]CheckIn
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
724. Find the central subscript of the array
BUU-Crypto-[HDCTF2019]basic rsa
Webrtc quickly set up video call and video conference
Online shrimp music will be closed in January next year. Netizens call No
Actual cases and optimization solutions of cloud native architecture
随机推荐
Design and implementation of redis 7.0 multi part AOF
[excel] PivotChart
High performance parallel programming and optimization | lesson 02 homework at home
win10清除快速访问-不留下痕迹
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
接地继电器DD-1/60
JS execution mechanism
The end of the Internet is rural revitalization
BUU-Pwn-test_ your_ nc
1.1 history of Statistics
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
1480. 一维数组的动态和
Talk about the SQL server version of DTM sub transaction barrier function
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
云原生架构实战案例及优化解决方案
剑指 Offer II 038. 每日温度
注释与注解
Excel comparator
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
QT QTableWidget 表格列置顶需求的思路和代码