给定一个整数k和一个数组arr [] ,该数组代表当前在底层等待N个人的目的地楼层, k是电梯的容量,即它可以同时容纳的最大人数。电梯从当前楼层到达任何连续楼层需要1个单位时间。我们的任务是安排电梯的时间,以最大程度地减少使所有人员回到目的地楼层然后返回地面楼层所需的总时间。
例子:
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).
方法:以目标的降序对给定数组进行排序。创建K组(从最高楼层开始),每组的成本为2 *(max(当前组中的元素)) 。所有组的总和就是答案。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum time taken
// by the elevator when operating optimally
int minTime(int n, int k, int a[])
{
// Sort in descending order
sort(a, a + n, greater());
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 code
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
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to return the minimum time taken
// by the elevator when operating optimally
static 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 code
public static void main(String args[])
{
int k = 2;
int arr[] = { 2, 3, 4 };
int n = arr.length;
System.out.println(minTime(n, k, arr));
}
}
// This code is contributed by
// Surendra_Gangwar
Python3
# Python3 implementation of the approach
# Function to return the minimum time taken
# by the elevator when operating optimally
def 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 code
if __name__ == "__main__" :
k = 2;
arr = [ 2, 3, 4 ];
n = len(arr) ;
print(minTime(n, k, arr));
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the minimum time taken
// by the elevator when operating optimally
static 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 code
public 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
12
时间复杂度: O(N * log(N))