直路上有N个城市,每个城市之间的距离为1个单位。您必须搭乘公交车才能到达第(N + 1)个城市。第i个城市每行驶1单位距离将花费C [i]美元。换句话说,从第i个城市到第j个城市的旅行成本为abs(i – j)* C [i]美元。任务是找到从城市1到城市(N +1),即超过最后一个城市的最低旅行成本。
例子:
Input: C[] = {3, 5, 4}
Output: 9
The bus boarded from the first city has the minimum
cost of all so it will be used to travel (N + 1) unit.
Input: C[] = {4, 7, 8, 3, 4}
Output: 18
Board the bus at the first city then change
the bus at the fourth city.
(3 * 4) + (2 * 3) = 12 + 6 = 18
方法:方法非常简单,只需乘坐迄今为止成本最低的公交车即可。只要找到成本更低的公共汽车,就从该城市换公共汽车。以下是要解决的步骤:
- 从第一个城市开始,其成本为C [1] 。
- 旅行到下一个城市,直到具有城市Ĵ成本小于前市(由我们旅行的时候,我们说城市i)发觉。
- 将成本计算为abs(j – i)* C [i] ,并将其加到目前的总成本中。
- 重复前面的步骤,直到遍历所有城市。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum cost to
// travel from the first city to the last
int minCost(vector& cost, int n)
{
// To store the total cost
int totalCost = 0;
// Start from the first city
int boardingBus = 0;
for (int i = 1; i < n; i++) {
// If found any city with cost less than
// that of the previous boarded
// bus then change the bus
if (cost[boardingBus] > cost[i]) {
// Calculate the cost to travel from
// the currently boarded bus
// till the current city
totalCost += ((i - boardingBus) * cost[boardingBus]);
// Update the currently boarded bus
boardingBus = i;
}
}
// Finally calculate the cost for the
// last boarding bus till the (N + 1)th city
totalCost += ((n - boardingBus) * cost[boardingBus]);
return totalCost;
}
// Driver code
int main()
{
vector cost{ 4, 7, 8, 3, 4 };
int n = cost.size();
cout << minCost(cost, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the minimum cost to
// travel from the first city to the last
static int minCost(int []cost, int n)
{
// To store the total cost
int totalCost = 0;
// Start from the first city
int boardingBus = 0;
for (int i = 1; i < n; i++)
{
// If found any city with cost less than
// that of the previous boarded
// bus then change the bus
if (cost[boardingBus] > cost[i])
{
// Calculate the cost to travel from
// the currently boarded bus
// till the current city
totalCost += ((i - boardingBus) * cost[boardingBus]);
// Update the currently boarded bus
boardingBus = i;
}
}
// Finally calculate the cost for the
// last boarding bus till the (N + 1)th city
totalCost += ((n - boardingBus) * cost[boardingBus]);
return totalCost;
}
// Driver code
public static void main(String[] args)
{
int []cost = { 4, 7, 8, 3, 4 };
int n = cost.length;
System.out.print(minCost(cost, n));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach
# Function to return the minimum cost to
# travel from the first city to the last
def minCost(cost, n):
# To store the total cost
totalCost = 0
# Start from the first city
boardingBus = 0
for i in range(1, n):
# If found any city with cost less than
# that of the previous boarded
# bus then change the bus
if (cost[boardingBus] > cost[i]):
# Calculate the cost to travel from
# the currently boarded bus
# till the current city
totalCost += ((i - boardingBus) *
cost[boardingBus])
# Update the currently boarded bus
boardingBus = i
# Finally calculate the cost for the
# last boarding bus till the (N + 1)th city
totalCost += ((n - boardingBus) *
cost[boardingBus])
return totalCost
# Driver code
cost = [ 4, 7, 8, 3, 4]
n = len(cost)
print(minCost(cost, n))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the minimum cost to
// travel from the first city to the last
static int minCost(int []cost, int n)
{
// To store the total cost
int totalCost = 0;
// Start from the first city
int boardingBus = 0;
for (int i = 1; i < n; i++)
{
// If found any city with cost less than
// that of the previous boarded
// bus then change the bus
if (cost[boardingBus] > cost[i])
{
// Calculate the cost to travel from
// the currently boarded bus
// till the current city
totalCost += ((i - boardingBus) *
cost[boardingBus]);
// Update the currently boarded bus
boardingBus = i;
}
}
// Finally calculate the cost for the
// last boarding bus till the (N + 1)th city
totalCost += ((n - boardingBus) *
cost[boardingBus]);
return totalCost;
}
// Driver code
public static void Main(String[] args)
{
int []cost = { 4, 7, 8, 3, 4 };
int n = cost.Length;
Console.Write(minCost(cost, n));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
18