Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
XILINN
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm
/
src
/
class092
/
Code06_MinimumNumberRefuelingStops.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
57 lines (53 loc) · 1.86 KB
main
Breadcrumbs
algorithm
/
src
/
class092
/
Code06_MinimumNumberRefuelingStops.java
Copy path
Top
File metadata and controls
Code
Blame
57 lines (53 loc) · 1.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package
class092
;
import
java
.
util
.
PriorityQueue
;
// 最低加油次数
// 汽车从起点出发驶向目的地,该目的地位于出发位置东面target英里处
// 沿途有加油站,用数组stations表示,其中 stations[i] = [positioni, fueli]
// 表示第i个加油站位于出发位置东面positioni英里处,并且有fueli升汽油
// 假设汽车油箱的容量是无限的,其中最初有startFuel升燃料
// 它每行驶1英里就会用掉1升汽油
// 当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中
// 为了到达目的地,汽车所必要的最低加油次数是多少?
// 如果无法到达目的地,则返回-1
// 注意:如果汽车到达加油站时剩余燃料为0,它仍然可以在那里加油
// 如果汽车到达目的地时剩余燃料为 0,仍然认为它已经到达目的地
// 测试链接 : https://leetcode.cn/problems/minimum-number-of-refueling-stops/
public
class
Code06_MinimumNumberRefuelingStops
{
public
static
int
minRefuelStops
(
int
target
,
int
startFuel
,
int
[][]
stations
) {
if
(
startFuel
>=
target
) {
return
0
;
}
// 大根堆
PriorityQueue
<
Integer
>
heap
=
new
PriorityQueue
<>((
a
,
b
) ->
b
-
a
);
// 包括初始油量 + 沿途加的油,能让你达到什么位置,to
int
to
=
startFuel
;
int
cnt
=
0
;
for
(
int
[]
station
:
stations
) {
int
position
=
station
[
0
];
int
fuel
=
station
[
1
];
if
(
to
<
position
) {
while
(!
heap
.
isEmpty
() &&
to
<
position
) {
to
+=
heap
.
poll
();
cnt
++;
if
(
to
>=
target
) {
return
cnt
;
}
}
if
(
to
<
position
) {
return
-
1
;
}
}
heap
.
add
(
fuel
);
}
// 代码能走到这里,说明还没到达target
// 如果还有油,看看能不能冲到target
while
(!
heap
.
isEmpty
()) {
to
+=
heap
.
poll
();
cnt
++;
if
(
to
>=
target
) {
return
cnt
;
}
}
return
-
1
;
}
}
You can’t perform that action at this time.