📜  当移除时间> =等待时间时,从阵列中的最大移除

📅  最后修改于: 2021-05-06 19:09:33             🧑  作者: Mango

给定一个数组中有N个元素。任务是从左到右删除数组中的元素。但是,需要一些时间才能从数组中删除一个元素(我们称其为removeing time )。删除元素的时间等于该元素的值(以秒为单位)。

仅当删除元素所需的时间(删除时间)大于或等于它在数组中等待的时间时,才可以删除该元素。

注意:允许在开始删除元素之前更改数组中元素的顺序。您的任务是找到可以从数组中删除的最大元素数。

例子:

想法是按照删除时间的升序排列所有元素。从左侧开始迭代,并保持移除时间的累积总和(它将用作下一个元素的等待时间)。检查每个元素的清除时间是否大于或等于累积时间(即等待时间)。如果小于,则无法将其删除。如果等于或大于,则可以将其删除,然后将其删除时间添加到累积总和中。继续进行到数组末尾。

下面是上述方法的实现:

C++
// C++ code to find the maximum number of
// elements that can be removed
#include 
using namespace std;
  
// Function to find maximum number of
// elements that can be removed
int maxRemoval(int arr[], int n)
{
    // it will contain frequency of
    // elements that can be removed
    int count = 0;
  
    // maintain cummulative sum of removal time
    int cummulative_sum = 0;
  
    // arrange elements in ascending order
    // of their removal time
    sort(arr, arr + n);
  
    for (int i = 0; i < n; i++) {
        if (arr[i] >= cummulative_sum) {
            count++;
            cummulative_sum += arr[i];
        }
    }
  
    return count;
}
  
// Driver code
int main()
{
    int arr[] = { 10, 5, 3, 7, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << maxRemoval(arr, n);
  
    return 0;
}


Java
// Java code to find the maximum number of
// elements that can be removed
import java.io.*;
import java.util.*;
  
class GFG {
  
// Function to find maximum number of
// elements that can be removed
static int maxRemoval(int arr[], int n)
{
    // it will contain frequency of
    // elements that can be removed
    int count = 0;
  
    // maintain cummulative sum of removal time
    int cummulative_sum = 0;
  
    // arrange elements in ascending order
    // of their removal time
    Arrays.sort(arr);
  
    for (int i = 0; i < n; i++) {
        if (arr[i] >= cummulative_sum) {
            count++;
            cummulative_sum += arr[i];
        }
    }
  
    return count;
}
  
// Driver code
  
    public static void main (String[] args) {
        int arr[] = { 10, 5, 3, 7, 2 };
    int n = arr.length;
    System.out.println(maxRemoval(arr, n));
    }
}
// This code is contributed 
// by inder_verma..


Python3
# Python3 code to find the maximum number 
# of elements that can be removed 
  
# Function to find maximum number of 
# elements that can be removed 
def maxRemoval(arr, n): 
  
    # It will contain frequency of 
    # elements that can be removed 
    count = 0
  
    # maintain cummulative sum of 
    # removal time 
    cummulative_sum = 0
  
    # arrange elements in ascending 
    # order of their removal time 
    arr.sort()
  
    for i in range(n): 
        if arr[i] >= cummulative_sum: 
            count += 1
            cummulative_sum += arr[i] 
  
    return count 
  
# Driver code 
if __name__ == "__main__":
  
    arr = [10, 5, 3, 7, 2] 
    n = len(arr) 
  
    print(maxRemoval(arr, n)) 
  
# This code is contributed by
# Rituraj Jain


C#
// C# code to find the maximum number 
// of elements that can be removed
using System; 
  
class GFG
{
  
// Function to find maximum number
// of elements that can be removed
static int maxRemoval(int[] arr, int n)
{
    // it will contain frequency of
    // elements that can be removed
    int count = 0;
  
    // maintain cummulative sum 
    // of removal time
    int cummulative_sum = 0;
  
    // arrange elements in ascending 
    // order of their removal time
    Array.Sort(arr);
  
    for (int i = 0; i < n; i++)
    {
        if (arr[i] >= cummulative_sum)
        {
            count++;
            cummulative_sum += arr[i];
        }
    }
  
    return count;
}
  
// Driver code
public static void Main ()
{
    int[] arr = { 10, 5, 3, 7, 2 };
    int n = arr.Length;
    Console.Write(maxRemoval(arr, n));
}
}
  
// This code is contributed 
// by ChitraNayal


PHP
= $cummulative_sum)
        {
            $count++;
            $cummulative_sum += $arr[$i];
        }
    }
  
    return $count;
}
  
// Driver code
$arr = array(10, 5, 3, 7, 2 );
$n = sizeof($arr);
  
echo (maxRemoval($arr, $n));
  
// This code is contributed 
// by Shivi_Aggarwal 
?>


输出:
4