在一条笔直的道路上有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 )花费更少的城市j 。
- 计算成本为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
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。