Skip to content

Commit 4f4797c

Browse files
committed
feat: add solutions to lc problem: No.0871
No.0871.Minimum Number of Refueling Stops
1 parent 15fbf1f commit 4f4797c

File tree

9 files changed

+320
-7
lines changed

9 files changed

+320
-7
lines changed

solution/0000-0099/0021.Merge Two Sorted Lists/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# self.val = val
55
# self.next = next
66
class Solution:
7-
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
7+
def mergeTwoLists(
8+
self, list1: Optional[ListNode], list2: Optional[ListNode]
9+
) -> Optional[ListNode]:
810
dummy = ListNode()
911
curr = dummy
1012
while list1 and list2:

solution/0200-0299/0241.Different Ways to Add Parentheses/Solution.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def dfs(exp):
77
ans = []
88
for i, c in enumerate(exp):
99
if c in '-+*':
10-
left, right = dfs(exp[:i]), dfs(exp[i + 1:])
10+
left, right = dfs(exp[:i]), dfs(exp[i + 1 :])
1111
for a in left:
1212
for b in right:
1313
if c == '-':
@@ -17,5 +17,5 @@ def dfs(exp):
1717
else:
1818
ans.append(a * b)
1919
return ans
20-
21-
return dfs(expression)
20+
21+
return dfs(expression)

solution/0800-0899/0871.Minimum Number of Refueling Stops/README.md

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,131 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:贪心 + 优先队列(大根堆)**
64+
65+
利用优先队列记录所有已经到达过的加油站的加油量,每次当油量不足时,从队列中取出最大加油量,并累计加油次数 ans。
66+
67+
时间复杂度 $O(nlogn)$。其中 $n$ 表示数组 $stations$ 的长度。
68+
6369
<!-- tabs:start -->
6470

6571
### **Python3**
6672

6773
<!-- 这里可写当前语言的特殊实现逻辑 -->
6874

6975
```python
70-
76+
class Solution:
77+
def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
78+
q = []
79+
prev = ans = 0
80+
stations.append([target, 0])
81+
for a, b in stations:
82+
d = a - prev
83+
startFuel -= d
84+
while startFuel < 0 and q:
85+
startFuel -= heappop(q)
86+
ans += 1
87+
if startFuel < 0:
88+
return -1
89+
heappush(q, -b)
90+
prev = a
91+
return ans
7192
```
7293

7394
### **Java**
7495

7596
<!-- 这里可写当前语言的特殊实现逻辑 -->
7697

7798
```java
99+
class Solution {
100+
public int minRefuelStops(int target, int startFuel, int[][] stations) {
101+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
102+
int n = stations.length;
103+
int prev = 0, ans = 0;
104+
for (int i = 0; i < n + 1; ++i) {
105+
int d = (i < n ? stations[i][0] : target) - prev;
106+
startFuel -= d;
107+
while (startFuel < 0 && !q.isEmpty()) {
108+
startFuel += q.poll();
109+
++ans;
110+
}
111+
if (startFuel < 0) {
112+
return -1;
113+
}
114+
if (i < n) {
115+
q.offer(stations[i][1]);
116+
prev = stations[i][0];
117+
}
118+
}
119+
return ans;
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
130+
priority_queue<int> q;
131+
stations.push_back({target, 0});
132+
int ans = 0, prev = 0;
133+
for (auto& s : stations)
134+
{
135+
int d = s[0] - prev;
136+
startFuel -= d;
137+
while (startFuel < 0 && !q.empty())
138+
{
139+
startFuel += q.top();
140+
q.pop();
141+
++ans;
142+
}
143+
if (startFuel < 0) return -1;
144+
q.push(s[1]);
145+
prev = s[0];
146+
}
147+
return ans;
148+
}
149+
};
150+
```
78151
152+
### **Go**
153+
154+
```go
155+
func minRefuelStops(target int, startFuel int, stations [][]int) int {
156+
stations = append(stations, []int{target, 0})
157+
ans, prev := 0, 0
158+
q := &hp{}
159+
heap.Init(q)
160+
for _, s := range stations {
161+
d := s[0] - prev
162+
startFuel -= d
163+
for startFuel < 0 && q.Len() > 0 {
164+
startFuel += q.pop()
165+
ans++
166+
}
167+
if startFuel < 0 {
168+
return -1
169+
}
170+
q.push(s[1])
171+
prev = s[0]
172+
}
173+
return ans
174+
}
175+
176+
type hp struct{ sort.IntSlice }
177+
178+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
179+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
180+
func (h *hp) Pop() interface{} {
181+
a := h.IntSlice
182+
v := a[len(a)-1]
183+
h.IntSlice = a[:len(a)-1]
184+
return v
185+
}
186+
func (h *hp) push(v int) { heap.Push(h, v) }
187+
func (h *hp) pop() int { return heap.Pop(h).(int) }
79188
```
80189

81190
### **...**

solution/0800-0899/0871.Minimum Number of Refueling Stops/README_EN.md

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,116 @@ We made 2 refueling stops along the way, so we return 2.
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
65+
q = []
66+
prev = ans = 0
67+
stations.append([target, 0])
68+
for a, b in stations:
69+
d = a - prev
70+
startFuel -= d
71+
while startFuel < 0 and q:
72+
startFuel -= heappop(q)
73+
ans += 1
74+
if startFuel < 0:
75+
return -1
76+
heappush(q, -b)
77+
prev = a
78+
return ans
6479
```
6580

6681
### **Java**
6782

6883
```java
84+
class Solution {
85+
public int minRefuelStops(int target, int startFuel, int[][] stations) {
86+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
87+
int n = stations.length;
88+
int prev = 0, ans = 0;
89+
for (int i = 0; i < n + 1; ++i) {
90+
int d = (i < n ? stations[i][0] : target) - prev;
91+
startFuel -= d;
92+
while (startFuel < 0 && !q.isEmpty()) {
93+
startFuel += q.poll();
94+
++ans;
95+
}
96+
if (startFuel < 0) {
97+
return -1;
98+
}
99+
if (i < n) {
100+
q.offer(stations[i][1]);
101+
prev = stations[i][0];
102+
}
103+
}
104+
return ans;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
115+
priority_queue<int> q;
116+
stations.push_back({target, 0});
117+
int ans = 0, prev = 0;
118+
for (auto& s : stations)
119+
{
120+
int d = s[0] - prev;
121+
startFuel -= d;
122+
while (startFuel < 0 && !q.empty())
123+
{
124+
startFuel += q.top();
125+
q.pop();
126+
++ans;
127+
}
128+
if (startFuel < 0) return -1;
129+
q.push(s[1]);
130+
prev = s[0];
131+
}
132+
return ans;
133+
}
134+
};
135+
```
69136
137+
### **Go**
138+
139+
```go
140+
func minRefuelStops(target int, startFuel int, stations [][]int) int {
141+
stations = append(stations, []int{target, 0})
142+
ans, prev := 0, 0
143+
q := &hp{}
144+
heap.Init(q)
145+
for _, s := range stations {
146+
d := s[0] - prev
147+
startFuel -= d
148+
for startFuel < 0 && q.Len() > 0 {
149+
startFuel += q.pop()
150+
ans++
151+
}
152+
if startFuel < 0 {
153+
return -1
154+
}
155+
q.push(s[1])
156+
prev = s[0]
157+
}
158+
return ans
159+
}
160+
161+
type hp struct{ sort.IntSlice }
162+
163+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
164+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
165+
func (h *hp) Pop() interface{} {
166+
a := h.IntSlice
167+
v := a[len(a)-1]
168+
h.IntSlice = a[:len(a)-1]
169+
return v
170+
}
171+
func (h *hp) push(v int) { heap.Push(h, v) }
172+
func (h *hp) pop() int { return heap.Pop(h).(int) }
70173
```
71174

72175
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
4+
priority_queue<int> q;
5+
stations.push_back({target, 0});
6+
int ans = 0, prev = 0;
7+
for (auto& s : stations)
8+
{
9+
int d = s[0] - prev;
10+
startFuel -= d;
11+
while (startFuel < 0 && !q.empty())
12+
{
13+
startFuel += q.top();
14+
q.pop();
15+
++ans;
16+
}
17+
if (startFuel < 0) return -1;
18+
q.push(s[1]);
19+
prev = s[0];
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
func minRefuelStops(target int, startFuel int, stations [][]int) int {
2+
stations = append(stations, []int{target, 0})
3+
ans, prev := 0, 0
4+
q := &hp{}
5+
heap.Init(q)
6+
for _, s := range stations {
7+
d := s[0] - prev
8+
startFuel -= d
9+
for startFuel < 0 && q.Len() > 0 {
10+
startFuel += q.pop()
11+
ans++
12+
}
13+
if startFuel < 0 {
14+
return -1
15+
}
16+
q.push(s[1])
17+
prev = s[0]
18+
}
19+
return ans
20+
}
21+
22+
type hp struct{ sort.IntSlice }
23+
24+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
25+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
26+
func (h *hp) Pop() interface{} {
27+
a := h.IntSlice
28+
v := a[len(a)-1]
29+
h.IntSlice = a[:len(a)-1]
30+
return v
31+
}
32+
func (h *hp) push(v int) { heap.Push(h, v) }
33+
func (h *hp) pop() int { return heap.Pop(h).(int) }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int minRefuelStops(int target, int startFuel, int[][] stations) {
3+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
4+
int n = stations.length;
5+
int prev = 0, ans = 0;
6+
for (int i = 0; i < n + 1; ++i) {
7+
int d = (i < n ? stations[i][0] : target) - prev;
8+
startFuel -= d;
9+
while (startFuel < 0 && !q.isEmpty()) {
10+
startFuel += q.poll();
11+
++ans;
12+
}
13+
if (startFuel < 0) {
14+
return -1;
15+
}
16+
if (i < n) {
17+
q.offer(stations[i][1]);
18+
prev = stations[i][0];
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def minRefuelStops(
3+
self, target: int, startFuel: int, stations: List[List[int]]
4+
) -> int:
5+
q = []
6+
prev = ans = 0
7+
stations.append([target, 0])
8+
for a, b in stations:
9+
d = a - prev
10+
startFuel -= d
11+
while startFuel < 0 and q:
12+
startFuel -= heappop(q)
13+
ans += 1
14+
if startFuel < 0:
15+
return -1
16+
heappush(q, -b)
17+
prev = a
18+
return ans
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Solution:
22
def minimumTime(self, time: List[int], totalTrips: int) -> int:
33
mx = min(time) * totalTrips
4-
return bisect_left(range(mx), totalTrips, key=lambda x: sum(x // v for v in time))
4+
return bisect_left(
5+
range(mx), totalTrips, key=lambda x: sum(x // v for v in time)
6+
)

0 commit comments

Comments
 (0)