Minimum possible travel cost among N cities
Last Updated :
04 Aug, 2021
There are N cities situated on a straight road and each is separated by a distance of 1 unit. You have to reach the (N + 1)th city by boarding a bus. The ith city would cost of C[i] dollars to travel 1 unit of distance. In other words, cost to travel from the ith city to the jth city is abs(i – j ) * C[i] dollars. The task is to find the minimum cost to travel from city 1 to city (N + 1) i.e. beyond the last city.
Examples:
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
Approach: The approach is very simple, just travel by the bus which has the lowest cost so far. Whenever a bus with an even lower cost is found, change the bus from that city. Following are the steps to solve:
- Start with the first city with a cost of C[1].
- Travel to the next city until a city j having cost less than the previous city (by which we are travelling, let’s say city i) is found.
- Calculate cost as abs(j – i) * C[i] and add it to the total cost so far.
- Repeat the previous steps until all the cities have been traversed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minCost(vector< int >& cost, int n)
{
int totalCost = 0;
int boardingBus = 0;
for ( int i = 1; i < n; i++) {
if (cost[boardingBus] > cost[i]) {
totalCost += ((i - boardingBus) * cost[boardingBus]);
boardingBus = i;
}
}
totalCost += ((n - boardingBus) * cost[boardingBus]);
return totalCost;
}
int main()
{
vector< int > cost{ 4, 7, 8, 3, 4 };
int n = cost.size();
cout << minCost(cost, n);
return 0;
}
|
Java
class GFG
{
static int minCost( int []cost, int n)
{
int totalCost = 0 ;
int boardingBus = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (cost[boardingBus] > cost[i])
{
totalCost += ((i - boardingBus) * cost[boardingBus]);
boardingBus = i;
}
}
totalCost += ((n - boardingBus) * cost[boardingBus]);
return totalCost;
}
public static void main(String[] args)
{
int []cost = { 4 , 7 , 8 , 3 , 4 };
int n = cost.length;
System.out.print(minCost(cost, n));
}
}
|
Python3
def minCost(cost, n):
totalCost = 0
boardingBus = 0
for i in range ( 1 , n):
if (cost[boardingBus] > cost[i]):
totalCost + = ((i - boardingBus) *
cost[boardingBus])
boardingBus = i
totalCost + = ((n - boardingBus) *
cost[boardingBus])
return totalCost
cost = [ 4 , 7 , 8 , 3 , 4 ]
n = len (cost)
print (minCost(cost, n))
|
C#
using System;
class GFG
{
static int minCost( int []cost, int n)
{
int totalCost = 0;
int boardingBus = 0;
for ( int i = 1; i < n; i++)
{
if (cost[boardingBus] > cost[i])
{
totalCost += ((i - boardingBus) *
cost[boardingBus]);
boardingBus = i;
}
}
totalCost += ((n - boardingBus) *
cost[boardingBus]);
return totalCost;
}
public static void Main(String[] args)
{
int []cost = { 4, 7, 8, 3, 4 };
int n = cost.Length;
Console.Write(minCost(cost, n));
}
}
|
Javascript
<script>
function minCost(cost , n) {
var totalCost = 0;
var boardingBus = 0;
for (i = 1; i < n; i++) {
if (cost[boardingBus] > cost[i]) {
totalCost += ((i - boardingBus) * cost[boardingBus]);
boardingBus = i;
}
}
totalCost += ((n - boardingBus) * cost[boardingBus]);
return totalCost;
}
var cost = [ 4, 7, 8, 3, 4 ];
var n = cost.length;
document.write(minCost(cost, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...