给定一个数组arr[]和一个目标值K 。任务是找到从数组中取出所有元素所需的最少步骤数。在每一步中,最多可以从数组中选择两个元素,使得它们的总和不能超过目标值K 。
注意:数组的所有元素都小于或等于K 。
Input: arr[] = [5, 1, 5, 4], K = 8
Output: 3
Explanation:
We can pick {1, 4}, {5}, {5} in 3 steps:
Other possible arrangement can be {1, 5}, {4}, {5} in three steps.
So, the minimum number of steps are required is 3
Input: [1, 2, 1, 1, 3], n = 9
Output: 3
Explanation:
We can pick {1, 1}, {2, 3} and {1} in three steps.
Other possible choices {1, 3}, {1, 2}, {1} or {1, 1}, {1, 3}, {2}
So, the minimum number of steps are required is 3
方法:上述问题可以使用贪心方法和两个指针技术来解决。这个想法是从数组中挑选最小和最大的元素并检查总和是否不超过N然后删除这些元素并计算这一步,否则删除最大的元素,然后重复上述步骤直到删除所有元素。以下是步骤:
- 对给定的数组arr[] 进行排序。
- 初始化两个索引i = 0和j = N – 1 。
- 如果元素arr[i]和arr[j]的总和不超过N,则增加i并减少j 。
- 否则递减j 。
- 重复上述步骤直到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)