📌  相关文章
📜  每次删除总和最多为K的最小对来清空阵列的最小步骤

📅  最后修改于: 2021-06-27 00:12:55             🧑  作者: Mango

给定数组arr []和目标值K。任务是找到从数组中获取所有元素所需的最少步骤数。在每个步骤中,最多可以从数组中选择两个元素,以使它们的总和不得超过目标值K。
注意:数组中的所有元素均小于或等于K。

方法:可以使用贪婪方法和两个指针技术来解决上述问题。这个想法是从数组中选择最小和最大的元素,并检查总和是否不超过N,然后删除这些元素并计算此步骤,否则删除最大的元素,然后重复上述步骤,直到所有元素都被删除为止。步骤如下:

  1. 对给定数组arr []进行排序。
  2. 初始化两个索引i = 0j = N – 1
  3. 如果元素arr [i]arr [j]的总和不超过N,则将i递增,将j递减。
  4. 否则递减j
  5. 重复上述步骤,直到i <= j并计算每个步骤。

下面是上述方法的实现:

C++
// C++ program for the above approach 
#include  
using namespace std; 
  
// Function to count minimum steps 
int countMinSteps(int arr[], int target, 
                int n) 
{ 
  
    // Function to sort the array 
    sort(arr, arr + n); 
    int minimumSteps = 0; 
    int i = 0, j = n - 1; 
  
    // Run while loop 
    while (i <= j) { 
  
        // Condition to check whether 
        // sum exceed the target or not 
        if (arr[i] + arr[j] <= target) { 
            i++; 
            j--; 
        } 
        else { 
            j--; 
        } 
  
        // Increment the step 
        // by 1 
        minimumSteps++; 
    } 
  
    // Return minimum steps 
    return minimumSteps; 
} 
  
// Driver Code 
int main() 
{ 
    // Given array arr[] 
    int arr[] = { 4, 6, 2, 9, 6, 5, 8, 10 }; 
  
    // Given target value 
    int target = 11; 
  
    int size = sizeof(arr) / sizeof(arr[0]); 
  
    // Function call 
    cout << countMinSteps(arr, target, size); 
  
    return 0; 
}


Java
// Java program implementation
// of the above approach
import java.util.*;
import java.io.*;
  
class GFG{
  
// Function to count minimum steps     
static int countMinSteps(int arr[],
                         int target, 
                         int n) 
{
      
    // Function to sort the array
    Arrays.sort(arr);
  
    int minimumSteps = 0;
    int i = 0;
    int j = n - 1;
  
    // Run while loop
    while (i <= j)
    {
          
        // Condition to check whether 
        // sum exceed the target or not
        if (arr[i] + arr[j] <= target)
        {
            i += 1;
            j -= 1;
        }
        else 
        {
            j -= 1;
        }
      
        // Increment the step by 1 
        minimumSteps += 1;
    }
  
    // Return minimum steps
    return minimumSteps;
}
  
// Driver code 
public static void main(String[] args) 
{
    int arr[] = { 4, 6, 2, 9, 6, 5, 8, 10 };
  
    // Given target value
    int target = 11;
  
    int size = arr.length;
          
    // Print the minimum flip
    System.out.print(countMinSteps(arr, target,
                                        size));
}
}
  
// This code is contributed by code_hunt


Python3
# Python3 program for the above approach 
  
# Function to count minimum steps 
def countMinSteps(arr, target, n):
      
    # Function to sort the array 
    arr.sort()
  
    minimumSteps = 0
    i, j = 0, n - 1
      
    # Run while loop
    while i <= j:
          
        # Condition to check whether 
        # sum exceed the target or not
        if arr[i] + arr[j] <= target:
            i += 1
            j -= 1
        else:
            j -= 1
              
        # Increment the step 
        # by 1 
        minimumSteps += 1
          
    # Return minimum steps
    return minimumSteps
  
# Driver code
      
# Given array arr[]
arr = [ 4, 6, 2, 9, 6, 5, 8, 10 ]
      
# Given target value
target = 11
  
size = len(arr)
      
# Function call
print(countMinSteps(arr, target, size))
  
# This code is contributed by Stuti Pathak


C#
// C# program implementation
// of the above approach
using System;
  
class GFG{
  
// Function to count minimum steps     
static int countMinSteps(int[] arr,
                         int target, 
                         int n) 
{
      
    // Function to sort the array
    Array.Sort(arr);
  
    int minimumSteps = 0;
    int i = 0;
    int j = n - 1;
  
    // Run while loop
    while (i <= j)
    {
          
        // Condition to check whether 
        // sum exceed the target or not
        if (arr[i] + arr[j] <= target)
        {
            i += 1;
            j -= 1;
        }
        else
        {
            j -= 1;
        }
  
        // Increment the step by 1 
        minimumSteps += 1;
    }
  
    // Return minimum steps
    return minimumSteps;
}
  
// Driver code 
public static void Main() 
{
    int[] arr = new int[]{ 4, 6, 2, 9, 
                           6, 5, 8, 10 };
  
    // Given target value
    int target = 11;
  
    int size = arr.Length;
      
    // Print the minimum flip
    Console.Write(countMinSteps(
                  arr, target, size));
}
}
  
// This code is contributed by sanjoy_62


输出:

5

时间复杂度: O(N * log N)
辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。