最小化给定数组中的移除以使最大值小于最小值的两倍
给定一个整数数组arr[] 。任务是最小化所需的移除次数,以使arr[]中的最大元素小于最小值的两倍。
例子
Input: arr[] = {4, 6, 21, 7, 5, 9, 12}
Output: The minimum number of removal operations is 4.
Explanation: The newly trimmed array is [7, 5, 9] where 9 is max and 5 is min; 9 < 2 × 5.
Input: arr[] = {14, 21, 62, 43, 39, 57}
Output: The minimum number of removal operations is 2.
Explanation: The newly trimmed array is [62, 43, 39, 57]
where 62 is max and 39 is min; 62 < 2 × 39.
方法:这个问题可以通过使用贪心方法来解决。这个想法非常简单有效,将问题视为找到最大元素小于最小值两倍的最大子数组,而不是从数组中删除元素。请按照以下步骤解决给定的问题。
- 从左到右遍历给定数组,并将每个元素视为子数组的起点。
- 用选择的子数组的起始索引,尽可能贪婪地选择子数组的另一端。
- 将子数组向右扩展,一次一个元素,使新添加的元素满足给定条件。
- 对所有子数组执行相同的过程以找到最大大小的子数组。
- 子数组和输入数组的大小之间的差异将是所需的最小移除次数。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to return minimum
int Min(int a, int b)
{
if (a < b)
return a;
else
return b;
}
// Function to return maximum
int Max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
// Function to find minimum removal operations.
int findMinRemovals(int arr[], int n)
{
// Stores the length of the maximum
// size subarray
int max_range = 0;
// To keep track of the minimum and
// the maximum elements
// in a subarray
int minimum, maximum;
// Traverse the array and consider
// each element as a
// starting point of a subarray
for (int i = 0; i < n && n - i >
max_range; i++) {
// Reset the minimum and the
// maximum elements
minimum = maximum = arr[i];
// Find the maximum size subarray
// `arr[i…j]`
for (int j = i; j < n; j++) {
// Find the minimum and the
// maximum elements in the current
// subarray. We must do this in
// constant time.
minimum = Min(minimum, arr[j]);
maximum = Max(maximum, arr[j]);
// Discard the subarray if the
// invariant is violated
if (2 * minimum <= maximum) {
break;
}
// Update `max_range` when a
// bigger subarray is found
max_range = Max(max_range, j - i + 1);
}
}
// Return array size - length of
// the maximum size subarray
return n - max_range;
}
// Driver Code
int main()
{
int arr[] = { 4, 6, 21, 7, 5, 9, 12 };
int N = sizeof(arr) / sizeof(int);
int res = findMinRemovals(arr, N);
// Print the result
cout << res;
return 0;
}
Java
// Java program for above approach
class GFG {
// Function to return minimum
static int Min(int a, int b) {
if (a < b)
return a;
else
return b;
}
// Function to return maximum
static int Max(int a, int b) {
if (a > b)
return a;
else
return b;
}
// Function to find minimum removal operations.
static int findMinRemovals(int arr[], int n)
{
// Stores the length of the maximum
// size subarray
int max_range = 0;
// To keep track of the minimum and
// the maximum elements
// in a subarray
int minimum, maximum;
// Traverse the array and consider
// each element as a
// starting point of a subarray
for (int i = 0; i < n && n - i > max_range; i++) {
// Reset the minimum and the
// maximum elements
minimum = maximum = arr[i];
// Find the maximum size subarray
// `arr[i…j]`
for (int j = i; j < n; j++) {
// Find the minimum and the
// maximum elements in the current
// subarray. We must do this in
// constant time.
minimum = Min(minimum, arr[j]);
maximum = Max(maximum, arr[j]);
// Discard the subarray if the
// invariant is violated
if (2 * minimum <= maximum) {
break;
}
// Update `max_range` when a
// bigger subarray is found
max_range = Max(max_range, j - i + 1);
}
}
// Return array size - length of
// the maximum size subarray
return n - max_range;
}
// Driver Code
public static void main(String args[]) {
int arr[] = { 4, 6, 21, 7, 5, 9, 12 };
int N = arr.length;
int res = findMinRemovals(arr, N);
// Print the result
System.out.println(res);
}
}
// This code is contributed by gfgking.
Python3
# python3 program for above approach
# Function to return minimum
def Min(a, b):
if (a < b):
return a
else:
return b
# Function to return maximum
def Max(a, b):
if (a > b):
return a
else:
return b
# Function to find minimum removal operations.
def findMinRemovals(arr, n):
# Stores the length of the maximum
# size subarray
max_range = 0
# To keep track of the minimum and
# the maximum elements
# in a subarray
# Traverse the array and consider
# each element as a
# starting point of a subarray
for i in range(0, n):
if n - i <= max_range:
break
# Reset the minimum and the
# maximum elements
minimum = arr[i]
maximum = arr[i]
# Find the maximum size subarray
# `arr[i…j]`
for j in range(i, n):
# Find the minimum and the
# maximum elements in the current
# subarray. We must do this in
# constant time.
minimum = Min(minimum, arr[j])
maximum = Max(maximum, arr[j])
# Discard the subarray if the
# invariant is violated
if (2 * minimum <= maximum):
break
# Update `max_range` when a
# bigger subarray is found
max_range = Max(max_range, j - i + 1)
# Return array size - length of
# the maximum size subarray
return n - max_range
# Driver Code
if __name__ == "__main__":
arr = [4, 6, 21, 7, 5, 9, 12]
N = len(arr)
res = findMinRemovals(arr, N)
# Print the result
print(res)
# This code is contributed by rakeshsahni
C#
// C# program for above approach
using System;
class GFG {
// Function to return minimum
static int Min(int a, int b)
{
if (a < b)
return a;
else
return b;
}
// Function to return maximum
static int Max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
// Function to find minimum removal operations.
static int findMinRemovals(int[] arr, int n)
{
// Stores the length of the maximum
// size subarray
int max_range = 0;
// To keep track of the minimum and
// the maximum elements
// in a subarray
int minimum, maximum;
// Traverse the array and consider
// each element as a
// starting point of a subarray
for (int i = 0; i < n && n - i > max_range; i++) {
// Reset the minimum and the
// maximum elements
minimum = maximum = arr[i];
// Find the maximum size subarray
// `arr[i…j]`
for (int j = i; j < n; j++) {
// Find the minimum and the
// maximum elements in the current
// subarray. We must do this in
// constant time.
minimum = Min(minimum, arr[j]);
maximum = Max(maximum, arr[j]);
// Discard the subarray if the
// invariant is violated
if (2 * minimum <= maximum) {
break;
}
// Update `max_range` when a
// bigger subarray is found
max_range = Max(max_range, j - i + 1);
}
}
// Return array size - length of
// the maximum size subarray
return n - max_range;
}
// Driver Code
public static void Main()
{
int[] arr = { 4, 6, 21, 7, 5, 9, 12 };
int N = arr.Length;
int res = findMinRemovals(arr, N);
// Print the result
Console.WriteLine(res);
}
}
// This code is contributes by ukasp.
Javascript
输出
4
时间复杂度: O(N*N)
辅助空间: O(1)