📌  相关文章
📜  通过每次删除一对总和至多 K 来清空数组的最小步骤

📅  最后修改于: 2021-09-07 03:10:11             🧑  作者: 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


Javascript


输出:

5

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live