假设有一条环形路。那条路上有n个汽油泵。您将获得两个数组a []和b []以及一个正整数c 。其中a [i]表示我们到达第i个汽油泵时获得的燃料量,b [i]表示从第i个汽油泵到(i + 1)个汽油泵的燃料量,而c表示容量坦克在车辆中的位置。任务是计算车辆能够从中完成循环并返回起点的加油泵的数量。
这篇文章与“查找所有汽油泵的第一次巡回演唱会”不同。
例子:
Input : n = 3, c = 3
a[] = { 3, 1, 2 }
b[] = { 2, 2, 2 }
Output : 2
Explanation:
If we starts with 0th petrol pump, we will gain
3 (a[0]) litres of petrol and lose 2 litre (b[0] to travel
to 1st petrol pump.On refueling 1 litre (a[1])
of petrol on 1st petrol pump, we will lose 2
litres (b[0]) of petrol to reach 2nd petrol pump.
Now the tank is empty.On refueling 2 litres (a[2]) of petrol
at 2nd petrol pump, we can travel back 0th
petrol pump.
If we starts from 1st petrol pump, we will gain 1
litre of petrol but to travel to 2nd petrol pump
we need 2 litres of petrol, which we don’t have. So, we cannot
starts from 1st petrol pump.
If we starts from 2nd petrol pump, we will gain 2
litres of petrol and travel to 0th petrol pump by
losing 2 litres of petrol. On refueling 3 litres on 1st
petrol pump, we can travel to 1st petrol
pump by losing 2 litre petrol. On refueling 1 litre of petrol, we
will have 2 litres of petrol left which we can use by traveling to
2nd petrol pump.
Input : n = 3, c = 3
a[] = { 3, 1, 2 }
b[] = { 2, 2, 1 }
Output : 2
方法:
该问题涉及两个部分,首先涉及是否存在有效的启动汽油泵,其次涉及是否存在这样的汽油泵,请先检查该汽油泵是否也可用作启动汽油泵。
首先,让我们从加油泵s开始,到加油泵s + 1,s + 2,s + 3直到s + j ,假设我们在加油泵s + j + 1之前已经用完燃料,然后介于s和s + j之间的汽油泵不能用作启动汽油泵。因此,我们从s + j + 1开始作为汽油泵。如果在所有汽油泵都用完后再没有这样的汽油泵,答案为0。此步骤的取值为O(n)。
其次,让我们参观一个这样的有效汽油泵(简称为s)。 s s – 1之前的汽油泵也可以是启动汽油泵,只要车辆可以从s – 1起动并达到s即可。
如果a [i]是加油站i上可用的燃油, c是燃油箱的容量,并且b [i]是车辆从加油站i到i + 1行驶的燃油量,则让定义need [i]如下:
need[i] = max(0, need[i + 1] + b[i] - min(c, a[i]))
Need [i]是多余的燃料,如果在行驶开始时在加油站i处存在于车辆中,则该燃油(a [i] _除外,它可以是有效的加油站)。
如果need [i] = 0,则汽油泵i是有效的启动汽油泵。我们知道从步骤1开始需要[s] =0。我们可以评估s – 1,s – 2…是否在启动汽油泵。此步骤还需要O(n)。
C++
// C++ Program to find the number of
// circular tour that visits all petrol pump
#include
using namespace std;
#define N 100
// Return the number of pumps from where we
// can start the journey.
int count(int n, int c, int a[], int b[])
{
int need[N];
// Making Circular Array.
for (int i = 0; i < n; i++) {
a[i + n] = a[i];
b[i + n] = b[i];
}
int s = 0;
int tank = 0;
// for each of the petrol pump.
for (int i = 0; i < 2 * n; i++) {
tank += a[i];
tank = min(tank, c);
tank -= b[i];
// If tank is less than 0.
if (tank < 0) {
tank = 0;
s = i + 1;
}
}
// If starting pump is greater than n,
// return ans as 0.
if (s >= n)
return 0;
int ans = 1;
need[s + n] = 0;
// For each of the petrol pump
for (int i = 1; i < n; i++) {
int id = s + n - i;
// Finding the need array
need[id] = max(0, need[id + 1] + b[id]
- min(a[id], c));
// If need is 0, increment the count.
if (need[id] == 0)
ans++;
}
return ans;
}
// Drivers code
int main()
{
int n = 3;
int c = 3;
int a[2 * N] = { 3, 1, 2 };
int b[2 * N] = { 2, 2, 2 };
cout << count(n, c, a, b) << endl;
return 0;
}
Java
// Java Program to find the number of
// circular tour that visits all petrol pump
import java.io.*;
class GFG
{
static int N = 100;
// Return the number of pumps from where we
// can start the journey.
public static int count(int n, int c, int a[], int b[])
{
int need[] = new int[N];
// Making Circular Array.
for (int i = 0; i < n; i++)
{
a[i + n] = a[i];
b[i + n] = b[i];
}
int s = 0;
int tank = 0;
// for each of the petrol pump.
for (int i = 0; i < 2 * n; i++)
{
tank += a[i];
tank = Math.min(tank, c);
tank -= b[i];
// If tank is less than 0.
if (tank < 0)
{
tank = 0;
s = i + 1;
}
}
// If starting pump is greater
// than n, return ans as 0.
if (s >= n)
return 0;
int ans = 1;
need[s + n] = 0;
// For each of the petrol pump
for (int i = 1; i < n; i++)
{
int id = s + n - i;
// Finding the need array
need[id] = Math.max(0, need[id + 1] + b[id]
- Math.min(a[id], c));
// If need is 0, increment the count.
if (need[id] == 0)
ans++;
}
return ans;
}
// Driver code
public static void main(String args[])
{
int n = 3;
int c = 3;
int[] a = new int[]{ 3, 1, 2, 0, 0, 0 };
int[] b = new int[]{ 2, 2, 2, 0, 0, 0 };
System.out.print(count(n, c, a, b) + "\n");
}
}
// This code is contributed
// by Akanksha Rai
Python3
# Python 3 Program to find the number of
# circular tour that visits all petrol pump
N = 100
# Return the number of pumps from
# where we can start the journey.
def count(n, c, a, b):
need = [0 for i in range(N)]
# Making Circular Array.
for i in range(0, n, 1):
a[i + n] = a[i]
b[i + n] = b[i]
s = 0
tank = 0
# for each of the petrol pump.
for i in range(0, 2 * n, 1):
tank += a[i]
tank = min(tank, c)
tank -= b[i]
# If tank is less than 0.
if (tank < 0):
tank = 0
s = i + 1
# If starting pump is greater
# than n, return ans as 0.
if (s >= n):
return 0
ans = 1
need[s + n] = 0
# For each of the petrol pump
for i in range(1, n, 1):
id = s + n - i
# Finding the need array
need[id] = max(0, need[id + 1] +
b[id] - min(a[id], c))
# If need is 0, increment the count.
if (need[id] == 0):
ans += 1
return ans
# Driver Code
if __name__ == '__main__':
n = 3
c = 3
a = [3, 1, 2, 0, 0, 0]
b = [2, 2, 2, 0, 0, 0]
print(count(n, c, a, b))
# This code is contributed by
# Sahil_Shelangia
C#
// C# Program to find the number of
// circular tour that visits all petrol pump
using System;
class GFG
{
static int N = 100;
// Return the number of pumps from where we
// can start the journey.
public static int count(int n, int c, int[] a, int[] b)
{
int[] need = new int[N];
// Making Circular Array.
for (int i = 0; i < n; i++) {
a[i + n] = a[i];
b[i + n] = b[i];
}
int s = 0;
int tank = 0;
// for each of the petrol pump.
for (int i = 0; i < 2 * n; i++) {
tank += a[i];
tank = Math.Min(tank, c);
tank -= b[i];
// If tank is less than 0.
if (tank < 0) {
tank = 0;
s = i + 1;
}
}
// If starting pump is greater than n,
// return ans as 0.
if (s >= n)
return 0;
int ans = 1;
need[s + n] = 0;
// For each of the petrol pump
for (int i = 1; i < n; i++) {
int id = s + n - i;
// Finding the need array
need[id] = Math.Max(0, need[id + 1] + b[id]
- Math.Min(a[id], c));
// If need is 0, increment the count.
if (need[id] == 0)
ans++;
}
return ans;
}
// Drivers code
static void Main()
{
int n = 3;
int c = 3;
int[] a = new int[6]{ 3, 1, 2, 0, 0, 0 };
int[] b = new int[6]{ 2, 2, 2, 0, 0, 0 };
Console.Write(count(n, c, a, b) + "\n");
}
//This code is contributed by DrRoot_
}
2
时间复杂度: O(n)
参考:
https://stackoverflow.com/questions/24408371/dynamic-programming-find-possible-ways-to-reach-destination