假设有一条环形路。那条路上有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 ie s – 1 之前的汽油泵也可以是启动汽油泵,前提是车辆可以在s – 1启动并到达s 。
如果a[i]是汽油泵i可用的燃料, c是油箱的容量, b[i]是车辆从汽油泵i 行驶到i + 1所需的燃料量,那么让定义需求[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_
}
Javascript
2
时间复杂度: O(n)
参考:
https://stackoverflow.com/questions/24408371/dynamic-programming-find-possible-ways-to-reach-destination
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。