Schedule elevator to reduce the total time taken
Last Updated :
01 Jun, 2022
Given an integer k and an array arr[] representing the destination floors for N people waiting currently at the ground floor and k is the capacity of the elevator i.e. maximum number of people it can hold at the same time. It takes 1 unit time for the elevator to reach any consecutive floor from the current floor. The task is to schedule the elevator in a way to minimize the total time taken to get all the people to their destination floor and then return back to the ground floor.
Examples:
Input: arr[] = {2, 3, 4}, k = 2
Output: 12
Second and the third persons (destination floors 3 and 4) shall go in the first turn taking 8 (4 + 4) unit time. The only person left will take 2 unit time to get to the destination
And then the elevator will take another 2 unit time to get back to the ground floor.
Total time taken = 8 + 2 + 2 = 12
Input: arr[] = {5, 5, 4}, k = 3
Output: 10
Every person can get on the elevator at the same time
Time required will be 10 (5 + 5).
Approach: Sort the given array in decreasing order of destination. Create groups of K (starting from the highest floor), the cost for each group will be 2 * (max(Elements in current group)). The summation across all groups will be the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minTime( int n, int k, int a[])
{
sort(a, a + n, greater< int >());
int minTime = 0;
for ( int i = 0; i < n; i += k)
minTime += (2 * a[i]);
return minTime;
}
int main()
{
int k = 2;
int arr[] = { 2, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minTime(n, k, arr);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int minTime( int n, int k, Integer a[])
{
Arrays.sort(a , Collections.reverseOrder());
int minTime = 0 ;
for ( int i = 0 ; i < n; i += k)
minTime += ( 2 * a[i]);
return minTime;
}
public static void main(String args[])
{
int k = 2 ;
Integer arr[] = { 2 , 3 , 4 };
int n = arr.length;
System.out.println(minTime(n, k, arr));
}
}
|
Python3
def minTime(n, k, a) :
a.sort(reverse = True );
minTime = 0 ;
for i in range ( 0 , n, k) :
minTime + = ( 2 * a[i]);
return minTime;
if __name__ = = "__main__" :
k = 2 ;
arr = [ 2 , 3 , 4 ];
n = len (arr) ;
print (minTime(n, k, arr));
|
C#
using System;
class GFG
{
static int minTime( int n, int k, int []a)
{
int temp;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
int minTime = 0;
for ( int i = 0; i < n; i += k)
minTime += (2 * a[i]);
return minTime;
}
public static void Main(String []args)
{
int k = 2;
int []arr = { 2, 3, 4 };
int n = arr.Length;
Console.Write(minTime(n, k, arr));
}
}
|
Javascript
<script>
function minTime(n , k , a)
{
var temp;
for ( var i = 0; i < n; i++)
{
for ( var j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
} var minTime = 0;
for ( var i = 0; i < n; i += k)
minTime += (2 * a[i]);
return minTime;
}
var k = 2;
var arr = [ 2, 3, 4 ];
var n = arr.length;
document.write(minTime(n, k, arr));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...