Find the minimum cost to cross the River
Given an integer N which is the number of villagers who need to cross a river but there is only one boat on which a maximum of 2 person can travel. Each person i has to pay some specific price Pi to travel alone in the boat. If two person i, j travel in the boat then they have to pay max(Pi, Pj). The task is to find the minimum amount all the villagers have to pay to cross the river.
Examples:
Input: Price[] = {30, 40, 60, 70}
Output: 220
P1 and P2 go together (which costs 40)
and P1 comes back (total cost 70 now).
Now P3 and P4 go (total cost 140) and
P2 comes back (total cost 180) and
finally P1 and P2 go together (total cost 220).
Input: Price[] = {892, 124}
Output: 892
Approach: There are two ways for the two most costly person to cross the river:
- Both of them cross the river with the cheapest person turn by turn. So, the total cost will be the cost of the two costly persons + 2 * (cost of the cheapest person) (due to coming back).
- The two cheapest person cross the river and the cheapest person comes back. Now, the 2 most costly person cross the river and 2nd cheapest person comes back. So, the total cost will be the cost of the cheapest and costliest person plus 2 * cost of the second cheapest person.
- Total cost will be the minimum of the above two ways.
Let’s consider the example we used above to understand the approach:
P1 = 30, P2 = 40, P3 = 60, P4 = 70
According to first method, P4 goes with P1 and P1 comes back (cost is P4+P1).
Now, P3 goes with P1 and P1 comes back (cost for this ride will be P4+P1).
So, total cost for sending two most costly person according to method 1 is P4+2*P1+P3 = 190
According to second method, P2 goes with P1 and P1 comes back (cost is P2+P1).
Now, P3 goes with P4 and P2 comes back (cost for this ride will be P4+P2).
So, total cost for sending two most costly person according to method 2 is P4+2*P2+P1 = 180
Hence, cost for sending P3 and P4 will be minimum of 2 methods, i.e., 180.
Now, we are left with P1 and P2 whom we have to send together and cost will
be P2 = 40.
So, total cost for travelling is 180 + 40 = 220.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int minimumCost(ll price[], int n)
{
sort(price, price + n);
ll totalCost = 0;
for ( int i = n - 1; i > 1; i -= 2) {
if (i == 2) {
totalCost += price[2] + price[0];
}
else {
ll price_first = price[i] + price[0] + 2 * price[1];
ll price_second = price[i] + price[i - 1] + 2 * price[0];
totalCost += min(price_first, price_second);
}
}
if (n == 1) {
totalCost += price[0];
}
else {
totalCost += price[1];
}
return totalCost;
}
int main()
{
ll price[] = { 30, 40, 60, 70 };
int n = sizeof (price) / sizeof (price[0]);
cout << minimumCost(price, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static long minimumCost( long price[], int n)
{
Arrays.sort(price);
long totalCost = 0 ;
for ( int i = n - 1 ; i > 1 ; i -= 2 )
{
if (i == 2 )
{
totalCost += price[ 2 ] + price[ 0 ];
}
else
{
long price_first = price[i] + price[ 0 ] + 2 * price[ 1 ];
long price_second = price[i] + price[i - 1 ] + 2 * price[ 0 ];
totalCost += Math.min(price_first, price_second);
}
}
if (n == 1 )
{
totalCost += price[ 0 ];
}
else
{
totalCost += price[ 1 ];
}
return totalCost;
}
public static void main (String[] args)
{
long price[] = { 30 , 40 , 60 , 70 };
int n = price.length;
System.out.println(minimumCost(price, n));
}
}
|
Python
def minimumCost(price, n):
price = sorted (price)
totalCost = 0
for i in range (n - 1 , 1 , - 2 ):
if (i = = 2 ):
totalCost + = price[ 2 ] + price[ 0 ]
else :
price_first = price[i] + price[ 0 ] + 2 * price[ 1 ]
price_second = price[i] + price[i - 1 ] + 2 * price[ 0 ]
totalCost + = min (price_first, price_second)
if (n = = 1 ):
totalCost + = price[ 0 ]
else :
totalCost + = price[ 1 ]
return totalCost
price = [ 30 , 40 , 60 , 70 ]
n = len (price)
print (minimumCost(price, n))
|
C#
using System;
class GFG
{
static long minimumCost( long []price, int n)
{
Array.Sort(price);
long totalCost = 0;
for ( int i = n - 1; i > 1; i -= 2)
{
if (i == 2)
{
totalCost += price[2] + price[0];
}
else
{
long price_first = price[i] + price[0] + 2 * price[1];
long price_second = price[i] + price[i - 1] + 2 * price[0];
totalCost += Math.Min(price_first, price_second);
}
}
if (n == 1)
{
totalCost += price[0];
}
else
{
totalCost += price[1];
}
return totalCost;
}
public static void Main ()
{
long []price = { 30, 40, 60, 70 };
int n = price.Length;
Console.WriteLine(minimumCost(price, n));
}
}
|
Javascript
<script>
function minimumCost(price, n)
{
price.sort();
let totalCost = 0;
for (let i = n - 1; i > 1; i -= 2)
{
if (i == 2)
{
totalCost += price[2] + price[0];
}
else
{
let price_first = price[i] + price[0] + 2 * price[1];
let price_second = price[i] + price[i - 1] + 2 * price[0];
totalCost += Math.min(price_first, price_second);
}
}
if (n == 1)
{
totalCost += price[0];
}
else
{
totalCost += price[1];
}
return totalCost;
}
let price = [ 30, 40, 60, 70 ];
let n = price.length;
document.write(minimumCost(price, n));
</script>
|
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Last Updated :
27 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...