📌  相关文章
📜  删除数组所有元素所需的最少操作数

📅  最后修改于: 2021-10-27 07:38:54             🧑  作者: Mango

给定一个整数数组arr ,任务是打印删除数组中所有元素所需的最少操作数。
在操作中,可以随机选择数组中的任何元素,并且可以从数组中删除每个可被它整除的元素。
例子:

方法:为获得最佳结果,应从数组中的最小元素中依次从剩余元素中选择,直到删除数组中的所有元素。

  • 按升序对数组进行排序并准备出现的哈希值。
  • 对于从头开始的每个未标记元素,标记所有可被选择元素整除的元素,并增加结果计数器。

下面是上述方法的实现

C++
// C++ implementation of the above approach
 
#include 
#define MAX 10000
 
using namespace std;
 
int hashTable[MAX];
 
// function to find minimum operations
int minOperations(int arr[], int n)
{
    // sort array
    sort(arr, arr + n);
 
    // prepare hash of array
    for (int i = 0; i < n; i++)
        hashTable[arr[i]]++;
 
    int res = 0;
    for (int i = 0; i < n; i++) {
        if (hashTable[arr[i]]) {
            for (int j = i; j < n; j++)
                if (arr[j] % arr[i] == 0)
                    hashTable[arr[j]] = 0;
            res++;
        }
    }
 
    return res;
}
 
// Driver program
int main()
{
    int arr[] = { 4, 6, 2, 8, 7, 21, 24, 49, 44 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << minOperations(arr, n);
    return 0;
}


Java
//Java implementation of the above approach
import java.util.*;
class Solution
{
static final int MAX=10000;
 
static int hashTable[]= new int[MAX];
 
// function to find minimum operations
static int minOperations(int arr[], int n)
{
    // sort array
    Arrays.sort(arr);
 
    // prepare hash of array
    for (int i = 0; i < n; i++)
        hashTable[arr[i]]++;
 
    int res = 0;
    for (int i = 0; i < n; i++) {
        if (hashTable[arr[i]]!=0) {
            for (int j = i; j < n; j++)
                if (arr[j] % arr[i] == 0)
                    hashTable[arr[j]] = 0;
            res++;
        }
    }
 
    return res;
}
 
// Driver program
public static void main(String args[])
{
    int arr[] = { 4, 6, 2, 8, 7, 21, 24, 49, 44 };
    int n = arr.length;
 
    System.out.print( minOperations(arr, n));
  
}
}
// This code is contributed by Arnab Kundu


Python 3
# Python 3 implementation of
# the above approach
MAX = 10000
 
hashTable = [0] * MAX
 
# function to find minimum operations
def minOperations(arr, n):
     
    # sort array
    arr.sort()
 
    # prepare hash of array
    for i in range(n):
        hashTable[arr[i]] += 1
 
    res = 0
    for i in range(n) :
        if (hashTable[arr[i]]) :
            for j in range(i, n):
                if (arr[j] % arr[i] == 0):
                    hashTable[arr[j]] = 0
            res += 1
 
    return res
 
# Driver Code
if __name__ == "__main__":
    arr = [ 4, 6, 2, 8, 7, 21, 24, 49, 44 ]
    n = len(arr)
 
    print(minOperations(arr, n))
 
# This code is contributed
# by ChitraNayal


C#
using System;
 
// C# implementation of the above approach 
public class Solution
{
public const int MAX = 10000;
 
public static int[] hashTable = new int[MAX];
 
// function to find minimum operations 
public static int minOperations(int[] arr, int n)
{
    // sort array 
    Array.Sort(arr);
 
    // prepare hash of array 
    for (int i = 0; i < n; i++)
    {
        hashTable[arr[i]]++;
    }
 
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        if (hashTable[arr[i]] != 0)
        {
            for (int j = i; j < n; j++)
            {
                if (arr[j] % arr[i] == 0)
                {
                    hashTable[arr[j]] = 0;
                }
            }
            res++;
        }
    }
 
    return res;
}
 
// Driver program 
public static void Main(string[] args)
{
    int[] arr = new int[] {4, 6, 2, 8, 7, 21, 24, 49, 44};
    int n = arr.Length;
 
    Console.Write(minOperations(arr, n));
 
}
}
 
// This code is contributed by Shrikant13


PHP


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程