从 Array 中删除最大值和最小值所需的从前面或后面的最小删除
给定一个由整数组成的数组arr[] 。任务是找到从arr[]中删除初始最小和最大元素所需的最小删除。
注意:可以从正面或背面执行删除 的数组。
例子:
Input: arr[] = {5, 7, 2, 4, 3}
Output: 3
Explanation: Initial minimum = 2, Initial maximum = 7
Deleting first 3 from arr[] updates arr[] to {2, 4, 3} which is free from Initial maximum and minimum elements.
Therefore 3 operations are required which is minimum possible.
Input: arr[] = {2, -1, 3, 5, 8, -7}
Output: 2
方法:给定的问题可以通过使用贪心方法来解决。请按照以下步骤解决给定的问题。
- 初始化两个变量mn , mx以分别存储最小和最大元素。
- 使用两个变量minIndex、maxIndex来存储最后出现的最小和最大元素。
- 用i迭代arr[]
- 更新mn = min(mn, arr[i])
- 更新mx = max(mx, arr[i])
- 使用两个变量 minIndex、maxIndex 来存储最后出现的最小和最大元素。
- 用 i 迭代 arr[]
- 如果arr[i] = mn ,则更新minIndex = i
- 如果arr[i] = mx ,则更新maxIndex = i
- 计算所有删除的情况并存储在变量x, y, z中。
- 返回x, y, z中的最小值作为最终答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate minimum deletions to
// remove initial minimum and maximum
int minDeletions(int *arr, int N)
{
// To store initial minimum and maximum
int mn = INT_MAX, mx = INT_MIN;
// Iterate and find min and max in arr[]
for(int i = 0; i < N; i++) {
mn = min(mn, arr[i]);
mx = max(mx, arr[i]);
}
// To store indices of last min and max
int minIndex, maxIndex;
for(int i = 0; i < N; i++) {
if(arr[i] == mn) minIndex = i;
if(arr[i] == mx) maxIndex = i;
}
int temp = max(minIndex, maxIndex);
minIndex = min(minIndex, maxIndex);
maxIndex = temp;
// Calculating all possible case of
// deletion operations
int x = N - maxIndex + minIndex + 1;
int y = N - minIndex;
int z = maxIndex + 1;
// Return minimum among all the three cases
return min({x, y, z});
}
// Driver Code
int main()
{
int N = 6;
int arr[] = {2, -1, 9, 7, -2, 3};
cout << minDeletions(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to calculate minimum deletions to
// remove initial minimum and maximum
static int minDeletions(int arr[], int N)
{
// To store initial minimum and maximum
int mn = Integer.MAX_VALUE, mx = Integer.MIN_VALUE;
// Iterate and find min and max in arr[]
for (int i = 0; i < N; i++) {
mn = Math.min(mn, arr[i]);
mx = Math.max(mx, arr[i]);
}
// To store indices of last min and max
int minIndx = 0, maxIndx = 0;
for (int i = 0; i < N; i++) {
if (arr[i] == mn)
minIndx = i;
if (arr[i] == mx)
maxIndx = i;
}
int temp = Math.max(minIndx, maxIndx);
minIndx = Math.min(minIndx, maxIndx);
maxIndx = temp;
// Calculating all possible case of
// deletion operations
int x = N - maxIndx + minIndx + 1;
int y = N - minIndx;
int z = maxIndx + 1;
// Return minimum among all the three cases
return Math.min(x, Math.min(y, z));
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, -1, 9, 7, -2, 3 };
int N = 6;
System.out.println(minDeletions(arr, N));
}
}
// This code is contributed by dwivediyash
Python3
# python program for the above approach
INT_MIN = -2147483648
INT_MAX = 2147483647
# Function to calculate minimum deletions to
# remove initial minimum and maximum
def minDeletions(arr, N):
# To store initial minimum and maximum
mn = INT_MAX
mx = INT_MIN
# Iterate and find min and max in arr[]
for i in range(0, N):
mn = min(mn, arr[i])
mx = max(mx, arr[i])
# To store indices of last min and max
minIndex = 0
maxIndex = 0
for i in range(0, N):
if(arr[i] == mn):
minIndex = i
if(arr[i] == mx):
maxIndex = i
temp = max(minIndex, maxIndex)
minIndex = min(minIndex, maxIndex)
maxIndex = temp
# Calculating all possible case of
# deletion operations
x = N - maxIndex + minIndex + 1
y = N - minIndex
z = maxIndex + 1
# Return minimum among all the three cases
return min({x, y, z})
# Driver Code
if __name__ == "__main__":
N = 6
arr = [2, -1, 9, 7, -2, 3]
print(minDeletions(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to calculate minimum deletions to
// remove initial minimum and maximum
static int minDeletions(int[] arr, int N)
{
// To store initial minimum and maximum
int mn = int.MaxValue, mx = int.MinValue;
// Iterate and find min and max in arr[]
for (int i = 0; i < N; i++) {
mn = Math.Min(mn, arr[i]);
mx = Math.Max(mx, arr[i]);
}
// To store indices of last min and max
int minIndx = 0, maxIndx = 0;
for (int i = 0; i < N; i++) {
if (arr[i] == mn)
minIndx = i;
if (arr[i] == mx)
maxIndx = i;
}
int temp = Math.Max(minIndx, maxIndx);
minIndx = Math.Min(minIndx, maxIndx);
maxIndx = temp;
// Calculating all possible case of
// deletion operations
int x = N - maxIndx + minIndx + 1;
int y = N - minIndx;
int z = maxIndx + 1;
// Return minimum among all the three cases
return Math.Min(x, Math.Min(y, z));
}
// Driver Code
public static void Main()
{
int[] arr = { 2, -1, 9, 7, -2, 3 };
int N = 6;
Console.Write(minDeletions(arr, N));
}
}
// This code is contributed by gfgking
Javascript
输出
4
时间复杂度: 在)
辅助空间: O(1)