📜  最小化任一端的删除以从数组中删除最小值和最大值

📅  最后修改于: 2022-05-13 01:56:10.350000             🧑  作者: Mango

最小化任一端的删除以从数组中删除最小值和最大值

给定大小为N的整数数组arr[] ,任务是找到最小删除操作的计数,以从数组中删除最小最大元素。只能从数组的任一端删除元素。

例子:

方法:上述问题可以通过以下观察来解决:

假设 max 和 min 元素存在于索引 i 和 j 处,反之亦然,如下所示:

[ _ _ _ _ _ min/max _ _ _ _ _ _ max/min _ _ _ _ _ _ ]
  <-- a -->   (i)   <---  b --->  (j)  <---- c ---->
<-----------------------N ------------------------->

在哪里,

  • i, j : 数组的最大或最小元素的索引
  • a : 最小(或最大)元素到起点的距离
  • b :最小和最大元素之间的距离
  • c :最大(或最小)元素与结束之间的距离
  • N : 数组的长度

现在让我们看看不同的可能删除方式:

  • 从 start中删除一个,从 end中删除另一个:
  • 要从数组的开头删除它们:
  • 要从数组末尾删除它们:

使用上述等式,我们现在可以使用 min 和 max 元素的索引轻松获得距离。答案是这3种情况中的最小值

下面是上述方法的实现:

C++
// C++ code to implement the above approach
 
#include 
using namespace std;
 
// Function to return
// the minimum number of deletions
int minDeletions(vector& nums)
{
    int n = nums.size();
 
    // Index of minimum element
    int minindex
      = min_element(nums.begin(), nums.end())
                   - nums.begin();
 
    // Index of maximum element
    int maxindex
      = max_element(nums.begin(), nums.end())
                   - nums.begin();
 
    // Assume that minimum element
    // always occur before maximum element.
    // If not, then swap its index.
    if (minindex > maxindex)
        swap(minindex, maxindex);
 
    // Deletion operations for case-1
    int bothend = (minindex + 1) +
      (n - maxindex);
 
    // Deletion operations for case-2
    int frontend = (maxindex + 1);
 
    // Deletion operations for case-3
    int backend = (n - minindex);
 
    // Least number of deletions is the answer
    int ans = min(bothend,
                  min(frontend, backend));
 
    return ans;
}
 
// Driver code
int main()
{
    vector arr{ 2, 10, 7, 5, 4, 1, 8, 6 };
    cout << minDeletions(arr) << endl;
 
    vector arr2{ 56 };
    cout << minDeletions(arr2);
 
    return 0;
}


Java
// Java code to implement the above approach
import java.util.Arrays;
import java.util.stream.IntStream;
 
class GFG{
 
// Function to return the
// minimum number of deletions
int minDeletions(int[] nums)
{
    int n = nums.length;
     
    // Index of minimum element
    int minindex = findIndex(nums,
    Arrays.stream(nums).min().getAsInt());
 
    // Index of maximum element
    int maxindex = findIndex(
        nums, Arrays.stream(nums).max().getAsInt());
 
    // Assume that minimum element
    // always occur before maximum element.
    // If not, then swap its index.
    if (minindex > maxindex)
    {
        minindex = minindex + maxindex;
        maxindex = minindex - maxindex;
        minindex = minindex - maxindex;
    }
 
    // Deletion operations for case-1
    int bothend = (minindex + 1) + (n - maxindex);
 
    // Deletion operations for case-2
    int frontend = (maxindex + 1);
 
    // Deletion operations for case-3
    int backend = (n - minindex);
 
    // Least number of deletions is the answer
    int ans = Math.min(
        bothend, Math.min(frontend, backend));
 
    return ans;
}
 
// Function to find the index of an element
public static int findIndex(int arr[], int t)
{
    int len = arr.length;
    return IntStream.range(0, len)
        .filter(i -> t == arr[i])
        .findFirst() // first occurrence
        .orElse(-1); // No element found
}
 
// Driver code
public static void main(String[] args)
{
    int[] arr = { 2, 10, 7, 5, 4, 1, 8, 6 };
    System.out.print(new GFG().minDeletions(arr) + "\n");
 
    int []arr2 = { 56 };
    System.out.print(new GFG().minDeletions(arr2));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 code to implement the above approach
 
# Function to return
# the minimum number of deletions
def minDeletions(nums):
 
    n = len(nums)
 
    # Index of minimum element
    minindex = nums.index(min(nums))
 
    # Index of maximum element
    maxindex = nums.index(max(nums))
 
    # Assume that minimum element
    # always occur before maximum element.
    # If not, then swap its index.
    if (minindex > maxindex):
        minindex, maxindex = maxindex, minindex
 
    # Deletion operations for case-1
    bothend = (minindex + 1) + (n - maxindex)
 
    # Deletion operations for case-2
    frontend = (maxindex + 1)
 
    # Deletion operations for case-3
    backend = (n - minindex)
 
    # Least number of deletions is the answer
    ans = min(bothend,
              min(frontend, backend))
 
    return ans
 
# Driver code
if __name__ == "__main__":
 
    arr = [2, 10, 7, 5, 4, 1, 8, 6]
    print(minDeletions(arr))
 
    arr2 = [56]
    print(minDeletions(arr2))
 
    # This code is contributed by ukasp.


C#
// C# code to implement the above approach
using System;
using System.Linq;
 
public class GFG{
 
// Function to return the
// minimum number of deletions
int minDeletions(int[] nums)
{
    int n = nums.Length;
     
    // Index of minimum element
    int minindex = findIndex(nums,
    nums.Min());
 
    // Index of maximum element
    int maxindex = findIndex(
        nums, nums.Max());
 
    // Assume that minimum element
    // always occur before maximum element.
    // If not, then swap its index.
    if (minindex > maxindex)
    {
        minindex = minindex + maxindex;
        maxindex = minindex - maxindex;
        minindex = minindex - maxindex;
    }
 
    // Deletion operations for case-1
    int bothend = (minindex + 1) + (n - maxindex);
 
    // Deletion operations for case-2
    int frontend = (maxindex + 1);
 
    // Deletion operations for case-3
    int backend = (n - minindex);
 
    // Least number of deletions is the answer
    int ans = Math.Min(
        bothend, Math.Min(frontend, backend));
 
    return ans;
}
 
// Function to find the index of an element
public static int findIndex(int []arr, int t)
{
    int len = arr.Length;
    return  Array.IndexOf(arr, t);
}
 
// Driver code
public static void Main(String[] args)
{
    int[] arr = { 2, 10, 7, 5, 4, 1, 8, 6 };
    Console.Write(new GFG().minDeletions(arr) + "\n");
 
    int []arr2 = { 56 };
    Console.Write(new GFG().minDeletions(arr2));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
5
1

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