Minimum time required to transport all the boxes from source to the destination under the given constraints
Last Updated :
05 Aug, 2021
Given two arrays, box[] and truck[], where box[i] represents the weight of the ith box and truck[i] represents the maximum load that the ith truck can carry. Now each truck takes 1 hour to transport a box from source to destination and another one hour to come back. Now, given that all the boxes are kept at the source, the task is to find the minimum time required to transport all the boxes from the source to the destination.
Note that there will always be some time in which the boxes can be transported and only a single box can be carried by truck at any instance of time.
Examples:
Input: box[] = {7, 6, 5, 4, 3}, truck[] = {10, 3}
Output: 7
1st hour: truck[0] carries box[0] and truck[1] carries box[4]
2nd hour: Both trucks are back at the source location.
Now, truck[1] cannot carry anymore boxes as all the remaining boxes
have weights more than the capacity of a truck[1].
So, truck[0] will carry box[1] and box[2]
in a total of four hours. (source-destination and then destination-source)
And finally, box[3] will take another hour to reach the destination.
So, total time taken = 2 + 4 + 1 = 7
Input: box[] = {10, 2, 16, 19}, truck[] = {29, 25}
Output: 3
Approach: The idea is to use binary search and sort the two arrays. Here the lower bound will be 0 and the upper bound will be 2 * size of box[] because in the worst case, the amount of time required to transport all the boxes will be 2 * size of box array. Now compute the mid-value, and for each mid-value check if all the boxes can be transported by the loaders in time = mid. If yes, then update the upper bound as mid – 1 and if not, then update the lower bound as mid + 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int box[], int truck[],
int n, int m, int min_time)
{
int temp = 0;
int count = 0;
while (count < m) {
for ( int j = 0; j < min_time
&& temp < n
&& truck[count] >= box[temp];
j += 2)
temp++;
count++;
}
if (temp == n)
return true ;
return false ;
}
int minTime( int box[], int truck[], int n, int m)
{
sort(box, box + n);
sort(truck, truck + m);
int l = 0;
int h = 2 * n;
int min_time = 0;
while (l <= h) {
int mid = (l + h) / 2;
if (isPossible(box, truck, n, m, mid)) {
min_time = mid;
h = mid - 1;
}
else
l = mid + 1;
}
return min_time;
}
int main()
{
int box[] = { 10, 2, 16, 19 };
int truck[] = { 29, 25 };
int n = sizeof (box) / sizeof ( int );
int m = sizeof (truck) / sizeof ( int );
printf ( "%d" , minTime(box, truck, n, m));
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static boolean isPossible( int box[], int truck[],
int n, int m, int min_time)
{
int temp = 0 ;
int count = 0 ;
while (count < m)
{
for ( int j = 0 ; j < min_time
&& temp < n
&& truck[count] >= box[temp];
j += 2 )
temp++;
count++;
}
if (temp == n)
return true ;
return false ;
}
static int minTime( int box[], int truck[], int n, int m)
{
Arrays.sort(box);
Arrays.sort(truck);
int l = 0 ;
int h = 2 * n;
int min_time = 0 ;
while (l <= h) {
int mid = (l + h) / 2 ;
if (isPossible(box, truck, n, m, mid))
{
min_time = mid;
h = mid - 1 ;
}
else
l = mid + 1 ;
}
return min_time;
}
public static void main(String[] args)
{
int box[] = { 10 , 2 , 16 , 19 };
int truck[] = { 29 , 25 };
int n = box.length;
int m = truck.length;
System.out.printf( "%d" , minTime(box, truck, n, m));
}
}
|
Python3
def isPossible(box, truck, n, m, min_time) :
temp = 0
count = 0
while (count < m) :
j = 0
while (j < min_time and temp < n and
truck[count] > = box[temp] ):
temp + = 1
j + = 2
count + = 1
if (temp = = n) :
return True
return False
def minTime(box, truck, n, m) :
box.sort();
truck.sort();
l = 0
h = 2 * n
min_time = 0
while (l < = h) :
mid = (l + h) / / 2
if (isPossible(box, truck, n, m, mid)) :
min_time = mid
h = mid - 1
else :
l = mid + 1
return min_time
if __name__ = = "__main__" :
box = [ 10 , 2 , 16 , 19 ]
truck = [ 29 , 25 ]
n = len (box)
m = len (truck)
print (minTime(box, truck, n, m))
|
C#
using System;
class GFG
{
static bool isPossible( int []box, int []truck,
int n, int m, int min_time)
{
int temp = 0;
int count = 0;
while (count < m)
{
for ( int j = 0; j < min_time
&& temp < n
&& truck[count] >= box[temp];
j += 2)
temp++;
count++;
}
if (temp == n)
return true ;
return false ;
}
static int minTime( int []box, int []truck, int n, int m)
{
Array.Sort(box);
Array.Sort(truck);
int l = 0;
int h = 2 * n;
int min_time = 0;
while (l <= h)
{
int mid = (l + h) / 2;
if (isPossible(box, truck, n, m, mid))
{
min_time = mid;
h = mid - 1;
}
else
l = mid + 1;
}
return min_time;
}
public static void Main(String[] args)
{
int [] box = { 10, 2, 16, 19 };
int []truck = { 29, 25 };
int n = box.Length;
int m = truck.Length;
Console.WriteLine( "{0}" , minTime(box, truck, n, m));
}
}
|
PHP
<?php
function isPossible( $box , $truck ,
$n , $m , $min_time )
{
$temp = 0;
$count = 0;
while ( $count < $m )
{
for ( $j = 0; $j < $min_time
&& $temp < $n
&& $truck [ $count ] >= $box [ $temp ];
$j += 2)
$temp ++;
$count ++;
}
if ( $temp == $n )
return true;
return false;
}
function minTime( $box , $truck , $n , $m )
{
sort( $box );
sort( $truck );
$l = 0;
$h = 2 * $n ;
$min_time = 0;
while ( $l <= $h ) {
$mid = intdiv(( $l + $h ) , 2);
if (isPossible( $box , $truck , $n , $m , $mid ))
{
$min_time = $mid ;
$h = $mid - 1;
}
else
$l = $mid + 1;
}
return $min_time ;
}
$box = array ( 10, 2, 16, 19 );
$truck = array ( 29, 25 );
$n = sizeof( $box );
$m = sizeof( $truck );
echo minTime( $box , $truck , $n , $m );
?>
|
Javascript
<script>
function isPossible( box, truck,
n, m, min_time)
{
let temp = 0;
let count = 0;
while (count < m) {
for (let j = 0; j < min_time
&& temp < n
&& truck[count] >= box[temp];
j += 2)
temp++;
count++;
}
if (temp == n)
return true ;
return false ;
}
function minTime(box, truck, n, m)
{
box.sort( function (a,b){ return a-b });
truck.sort( function (a,b){ return a-b });
let l = 0;
let h = 2 * n;
let min_time = 0;
while (l <= h) {
let mid = Math.floor((l + h) / 2);
if (isPossible(box, truck, n, m, mid)) {
min_time = mid;
h = mid - 1;
}
else
l = mid + 1;
}
return min_time;
}
let box = [ 10, 2, 16, 19 ];
let truck = [ 29, 25 ];
let n = box.length;
let m = truck.length;
document.write(minTime(box, truck, n, m));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...