Schedule elevator to reduce the total time taken

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 = 12Input: 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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the minimum time taken// by the elevator when operating optimallyint minTime(int n, int k, int a[]){    // Sort in descending order    sort(a, a + n, greater<int>());    int minTime = 0;     // Iterate through the groups    for (int i = 0; i < n; i += k)         // Update the time taken for each group        minTime += (2 * a[i]);     // Return the total time taken    return minTime;} // Driver codeint main(){    int k = 2;    int arr[] = { 2, 3, 4 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << minTime(n, k, arr);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{// Function to return the minimum time taken// by the elevator when operating optimallypublic static int minTime(int n, int k, Integer a[]){    // Sort in descending order    Arrays.sort(a , Collections.reverseOrder());     int minTime = 0;     // Iterate through the groups    for (int i = 0; i < n; i += k)         // Update the time taken for each group        minTime += (2 * a[i]);     // Return the total time taken    return minTime;} // Driver codepublic static void main(String args[]){    int k = 2;    Integer arr[] = { 2, 3, 4 };    int n = arr.length;    System.out.println(minTime(n, k, arr));}} // This code is contributed by Pushpesh Raj.
# Python3 implementation of the approach # Function to return the minimum time taken# by the elevator when operating optimallydef minTime(n, k, a) :         # Sort in descending order    a.sort(reverse = True);         minTime = 0;     # Iterate through the groups    for i in range(0, n, k) :         # Update the time taken for        # each group        minTime += (2 * a[i]);     # Return the total time taken    return minTime; # Driver codeif __name__ == "__main__" :         k = 2;    arr = [ 2, 3, 4 ];    n = len(arr) ;    print(minTime(n, k, arr));     # This code is contributed by Ryuga
// C# implementation of the approachusing System; class GFG{ // Function to return the minimum time taken// by the elevator when operating optimallystatic int minTime(int n, int k, int []a){    // Sort in descending order    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;     // Iterate through the groups    for (int i = 0; i < n; i += k)         // Update the time taken for each group        minTime += (2 * a[i]);     // Return the total time taken    return minTime;} // Driver codepublic static void Main(String []args){    int k = 2;    int []arr = { 2, 3, 4 };    int n = arr.Length;    Console.Write(minTime(n, k, arr));}} // This code is contributed by Arnab Kundu
<script> // JavaScript implementation of the approach // Function to return the minimum time taken// by the elevator when operating optimallyfunction minTime(n , k , a){    // Sort in descending order    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;     // Iterate through the groups    for (var i = 0; i < n; i += k)         // Update the time taken for each group        minTime += (2 * a[i]);     // Return the total time taken    return minTime;} // Driver codevar k = 2;var arr = [ 2, 3, 4 ];var n = arr.length;document.write(minTime(n, k, arr)); // This code is contributed by Amit Katiyar </script>
12
Time Complexity: O(N * log(N))Auxiliary Space: O(1)
ankthon
SURENDRA_GANGWAR
andrew1234
amit143katiyar
pankajsharmagfg
pushpeshrajdx01
Arrays
Sorting Quiz
Algorithms
Arrays
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.