尽量减少填充以到达路径的尽头
给定一个整数目标,表示汽车在直路上行驶的总距离。给定另一个数组,大小为N的station[]表示汽油泵,其中第 i 个汽油泵距离起点的station[i][0]位置并具有station[i][1]燃料量。这辆车有无限的汽油容量,并以M量的燃料启动。任务是找出当汽车使用一单位燃料移动一单位距离时,汽车必须停止加油才能到达终点的最小次数。
注意:如果它到达第i个加油站,剩余0油,它可以从那个油泵加油,油泵的所有燃油都可以转移到汽车上。
例子:
Input: target = 1, M = 1, stations = { }
Output: 0
Explanation: As it is possible to reach the target without refueling.
Input: target = 100, M = 1, stations = { {10, 100} }
Output: -1
Explanation: It is not possible to reach the target (or even the first gas station).
Input: target = 100, M = 10, stations = { {10, 60}, {20, 30}, {30, 30}, {60, 40}}
Output: 2
Explanation: Starting with 10 units of fuel.
Drive to position 10, expending 10 units of fuel. T
hen refuel from 0 liters to 60 units of fuels.
Then, drive from position 10 to position 60, and refuel 40 liters of gas.
Then simply drive and reach the target.
Hence there are 2 refueling stops along the way.
幼稚方法:基本方法是尝试1 个加油泵的每个组合,然后2 个加油站的每个组合,等等,并找到最小的形式。
时间复杂度: O(2 N ) 因为可能的组合总数为N C 1 + N C 2 + N C 3 + 。 . . + N C N = 2 N 。
辅助空间: O(1)
高效方法:这个问题可以使用贪心方法和基于以下思想的最大堆来解决:
- As there is need to refuel minimum number of times so do not refill the tank till it is possible to reach the next petrol pump.
- When it is no more possible to reach the next petrol pump, select one pump from the ones crossed till now and not used for refuelling.
- To minimize the stops it is optimal to choose the petrol pump with maximum amount of fuel.
To find the pump with maximum one from the already visited ones in optimal time, use max heap. Store the petrol pumps in max heap while crossing them, and remove it from the heap when it is used.
请按照以下步骤解决问题:
- 从一开始就按位置的升序对站点进行排序。
- 创建一个最大堆说pq (由优先级队列实现)。
- 遍历从i = 0 到 N-1 的站点:
- 检查汽车是否可以在不加油的情况下到达第i个车站。
- 如果无法到达,则从目前为止经过的站点中,从最大堆中找到燃料最大且未用于加油的站点,并增加加油站的次数。
- 在最大堆中插入第 i 个站
- 返回计数作为答案。
下面是上述方法的实现:
C++14
// C++ code for the above approach:
#include
using namespace std;
// Function to find minimum
// refuel stops
int minRefuelStops(int T, int F,
vector >& S)
{
int N = S.size(), ans = 0;
sort(S.begin(), S.end());
// Initializing max heap
priority_queue pq;
for (int i = 0; i <= N; i++) {
int dist = i == N ? T : S[i][0];
while (F < dist) {
if (!pq.size())
return -1;
F += pq.top(), ans++;
pq.pop();
}
if (i < N)
pq.push(S[i][1]);
}
return ans;
}
// Driver code
int main()
{
int target = 100;
int M = 10;
vector > stations = {
{ 10, 60 }, { 20, 30 }, { 30, 30 }, { 60, 40 }
};
// Function call
cout << minRefuelStops(target, M, stations);
return 0;
}
Java
// Java code to implement the approach
import java.util.*;
public class GFG {
// Function to find the minimum number
// of refueling stops
static int minRefuelStops(int T, int F,
int[][] S)
{
int N = S.length, ans = 0;
// Sort on the basis of
// distance from the start
Arrays.sort(S, new Comparator() {
public int compare(int[] a, int[] b)
{
return Integer.compare(a[0], b[0]);
}
});
PriorityQueue pq
= new PriorityQueue<>((a, b)
-> b - a);
for (int i = 0; i <= N; i++) {
int dist = i == N ? T : S[i][0];
while (F < dist) {
if (pq.size() == 0)
return -1;
F += pq.poll();
ans++;
}
if (i < N)
pq.add(S[i][1]);
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int target = 100, M = 10;
int stations[][] = {
{ 10, 60 }, { 20, 30 }, { 30, 30 }, { 60, 40 }
};
System.out.println(
minRefuelStops(target, M, stations));
}
}
// this code is contributed by prophet1999
2
时间复杂度: O(N * log N)
辅助空间: O(N)