📌  相关文章
📜  最小化使给定数组以任何顺序连续所需的删除

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

最小化使给定数组以任何顺序连续所需的删除

给定一个数组arr[] 。任务是最小化删除次数,以使arr[]中的所有元素连续。

例子

方法:这个问题可以通过使用Hashmaps来解决。请按照以下步骤解决给定的问题。

  • 首先创建一个地图,并在每个元素处,即在每个键处设置其值true
  • 所以每个元素/键都有真正的价值。现在移动地图的键集,看看它是否在地图中包含(key-1)
  • 如果它在那个键上设置为假。
  • 现在在另一个循环中,为那些值为真的键工作,直到它的最长序列并找到最长连续序列的长度。
  • 因此,最小移除次数将是数组长度与最长连续序列长度之差。

请按照以下步骤解决给定的问题。

C++
// C++ program for above approach
#include 
using namespace std;
 
void minRemovalsConsecutive(vector& a, int n)
{
 
  // Hashmap to store the elements
  // in key-value pairs
  map map;
 
  for (int val : a) {
    map[val] = true;
  }
  for (auto it = map.begin(); it != map.end(); ++it) {
    if (map.count(it->first - 1)) {
      map[it->first] = false;
    }
  }
  int max = 0;
 
  // Iterating for all keys in map
  for (auto it = map.begin(); it != map.end(); ++it) {
    if (map.count(it->first)) {
      int t1 = 1;
      while (map.count(it->first + t1)) {
        t1++;
      }
      if (t1 > max) {
        max = t1;
      }
    }
  }
 
  // Printing the final answer
  cout << (n - max);
}
 
// Driver Code
int main()
{
  int N = 5;
  vector arr = { 45, 42, 46, 48, 47 };
 
  // Function Call
  minRemovalsConsecutive(arr, N);
 
  return 0;
}
 
// This code is contributed by rakeshsahni


Java
// Java program for above approach
import java.io.*;
import java.util.*;
class GFG {
 
    public static void
    minRemovalsConsecutive(int a[], int n)
    {
        // Hashmap to store the elements
        // in key-value pairs
        HashMap map
            = new HashMap<>();
        for (int val : a) {
            map.put(val, true);
        }
        for (int val : map.keySet()) {
            if (map.containsKey(val - 1)) {
                map.put(val, false);
            }
        }
        int max = 0;
 
        // Iterating for all keys in map
        for (int val : map.keySet()) {
            if (map.get(val)) {
                int t1 = 1;
                while (map.containsKey(val
                                       + t1)) {
                    t1++;
                }
                if (t1 > max) {
                    max = t1;
                }
            }
        }
 
        // Printing the final answer
        System.out.println(n - max);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5;
        int arr[] = { 45, 42, 46, 48, 47 };
 
        // Function Call
        minRemovalsConsecutive(arr, N);
    }
}


Python3
# Python 3 program for above approach
def minRemovalsConsecutive(a, n):
 
    # Hashmap to store the elements
    # in key-value pairs
    map = {}
 
    for val in a:
        map[val] = True
 
    for it in map:
        if ((it-1) in map):
            map[it] = False
    max = 0
 
    # Iterating for all keys in map
    for it in map:
        if (it in map):
            t1 = 1
            while ((it + t1) in map):
                t1 += 1
 
            if (t1 > max):
                max = t1
    # Printing the final answer
    print(n - max)
 
 
# Driver Code
if __name__ == "__main__":
    N = 5
    arr = [45, 42, 46, 48, 47]
 
    # Function Call
    minRemovalsConsecutive(arr, N)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
  static void minRemovalsConsecutive(int []a, int n)
  {
     
    // Hashmap to store the elements
    // in key-value pairs
    Dictionary map1 =
      new Dictionary();
 
    foreach (int val in a) {
      map1.Add(val, true);
    }
 
    Dictionary map2 =
      new Dictionary();
 
    foreach(KeyValuePair k in map1){
      if (map1.ContainsKey(k.Key - 1)) {
        map2.Add(k.Key, false);
      }
      else {
        map2.Add(k.Key, true);
      }
    }
    int max = 0;
 
    // Iterating for all keys in map
    foreach(KeyValuePair k in map2){
      if (map2.ContainsKey(k.Key)) {
        int t1 = 1;
        while (map2.ContainsKey(k.Key
                                + t1)) {
          t1++;
        }
        if (t1 > max) {
          max = t1;
        }
      }
    }
 
    // Printing the final answer
    Console.WriteLine(n - max);
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 5;
    int []arr = { 45, 42, 46, 48, 47 };
 
    // Function Call
    minRemovalsConsecutive(arr, N);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
1

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