📜  N个城市之间可能的最小旅行成本

📅  最后修改于: 2021-10-25 06:49:44             🧑  作者: Mango

在一条笔直的道路上有N个城市,每个城市相隔1 个单位的距离。您必须乘坐公共汽车到达第(N + 1)城市。第i城市旅行1 个单位的距离将花费C[i]美元。换句话说,从第i城市到第j城市的旅行成本是abs(i – j ) * C[i]美元。任务是找到从城市1到城市(N + 1)即超出最后一个城市的最低成本。
例子:

方法:方法很简单,乘坐目前成本最低的巴士即可。每当发现成本更低的巴士时,请更换该城市的巴士。以下是解决步骤:

  1. 从第一个城市开始,成本为C[1]
  2. 前往下一个城市,直到找到比前一个城市(我们正在旅行的城市,假设城市i )花费更少的城市j
  3. 计算成本为abs(j – i) * C[i]并将其添加到目前的总成本中。
  4. 重复前面的步骤,直到遍历所有城市。

下面是上述方法的实现:

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 现场工作专业课程学生竞争性编程现场课程