求栈中最小元素操作_使用散列使所有元素相同的最小操作数

求栈中最小元素操作

Prerequisite:

先决条件:

Problem statement:

问题陈述:

Find the minimum number of operations required to make all elements the same. The possible operations are to add any number, delete any number, multiply with any number, and divide by any number.

找到使所有元素相同所需的最少操作数。 可能的操作是添加任意数字,删除任意数字,乘以任意数字并除以任意数字。

Example:

例:

Input array= [1, 2, 1, 1, 4, 5, 6]
The minimum number of operations required is 4. 
Leave the 1s
Subtract 1 from 2->1 operation
Divide 4 by 4-> one operation
Divide 5 by 5-> one operation
Subtract 5 from 6-> one operation
So the array is now [1, 1, 1, 1, 1, 1, 1] 
and minimum operation required is 4
One can come up with other operations too to convert 
the array in to same elements, but the minimum would 
be 4 and that's optimum.   

Solution:

解:

We can solve this problem using a hash map (hash table). One thing is clear that to achieve the minimum number of operation, we need to keep some elements unchanged and need to convert the rest of the elements to the unchanged element. So, which element should be kept as unchanged to achieve the minimum number of deletion? Of course, the element that has the highest number of occurrences should be kept and the rest of the elements will take one operation each to get converted into the unchanged element.

我们可以使用哈希映射(哈希表)解决此问题。 一件事很清楚,要达到最少的操作数,我们需要保持某些元素不变,并且需要将其余元素转换为不变的元素。 那么,应将哪个元素保持不变以实现最少的删除次数? 当然,应保留出现次数最多的元素,其余元素将各执行一次操作就可以转换为未更改的元素。

For example, In the above input, 1 has the most number of occurrences and that’s why we kept that unchanged and converted other elements into 1.

例如,在上面的输入中,1出现的次数最多,这就是为什么我们保持不变并将其他元素转换为1的原因。

So our goal is to find the element which maximum occurrences and minimum number of operation required will be=n-max frequency where n is the total number of elements
So how can we design the problem with the hash table and what will be the hash function?

因此,我们的目标是找到最大出现次数和所需最小操作次数为= n-最大频率的元素,其中n是元素总数
那么我们如何设计哈希表的问题以及哈希函数是什么呢?

Okay, so here each element is our keys and the hash table has the size of the range of the array. So, if the range of the array is [0,99] then the hash table size would be 100.
What will be our hash function and how would we map the keys to the corresponding location in the hash table?

好的,所以这里的每个元素都是我们的键,哈希表具有数组范围的大小。 因此,如果数组的范围为[0,99],则哈希表大小将为100。
我们的哈希函数将是什么?如何将键映射到哈希表中的对应位置?

The hash function h(x)=x here but instead of storing the key itself using linear probing we will keep the count only as we require frequency for unique keys.

这里的哈希函数h(x)= x ,而不是仅使用线性探测来存储密钥本身,我们仅在我们需要唯一密钥的频率时才保留计数。

So, for example, if we have three 2s as our key, the hash table will store the count(frequency) at location 2.

因此,例如,如果我们将三个2用作键,则哈希表会将计数(频率)存储在位置2。

So, after the hash table is created we can easily find the most occurred elements by each location (index) of the hash table.

因此,在创建哈希表之后,我们可以轻松地按哈希表的每个位置(索引)找到出现次数最多的元素。

So the algorithm will be,

因此算法将是

Step 1:

第1步:

Create the hash table like below:
Initially hash table is empty

For each key in input array:
Hash[key]++

Step 2:

第2步:

Initially max_freq=0
For each location
    If(hash[location]>max_freq)
    Update max_freq as hash[location]

After,
this max_freq contains the number of occurrences 
for the most frequent key and the rest of 
the keys need to be deleted.

So the minimum number of the operation:
n-max_freq
where,
n = total number of keys/input elements

Dry run with the example:

空运行示例:

Input array is = [1, 2, 1, 1, 4, 5, 6]
So hash table size would be (6-1)=6

After creating the hash table as step1 we will have
Index	Value
1	3
2	1
4	1
5	1
6	1
  
So, max_freq = 3
Hence, minimum operations needed = 7-3 = 4

C++ implementation:

C ++实现:

// C++ program to find the minimum operation 
// to make all elements equal in array
#include <bits/stdc++.h>
using namespace std;

int minimum_operation(vector<int> arr, int n)
{
    //create a hash table where for each key 
    //the hash function is h(x)=x
    //we will use stl map as hash table 
    //and will keep frequencies stored
    //so say two keys are mapping to the same location, 
    //then the location will have value 2
    //instead of the keys itself
    map<int, int> hash;

    //for each number
    for (auto i : arr) {
        hash[i]++;
    }

    //now to make all elements same we can 
    //do four types of operation:
    //1. add any number
    //2. subtract any number
    //3. divide any number
    //4. multiply any number

    //we need to keep only the key with maximum frequency
    //we need to convert the other keys to 
    //the key with maximum frequency
    //to convert any number to another number 
    //we only require one operation
    //so find the key with max frequency
    
    int maxfreq = 0;
    for (auto it = hash.begin(); it != hash.end(); it++) {
        if (it->second > maxfreq) {
            maxfreq = it->second;
        }
    }

    //so minimum operation needed is: n-maxfreq
    return n - maxfreq;
}

int main()
{
    int n;
 
    cout << "Enter number of elements\n";
    cin >> n;
 
    vector<int> arr(n, 0);
 
    cout << "Input the array elements\n";
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
 
    cout << "Minimum number of operations required to make all elements same is: ";
    cout << minimum_operation(arr, n) << endl;
    
    return 0;
}

Output:

输出:

Enter number of elements: 7
Input the array elements
1 2 1 1 4 5 6
Minimum number of operations required to make all elements same is: 4


翻译自: https://www.includehelp.com/data-structure-tutorial/minimum-number-of-operations-to-make-all-elements-same-using-hashing.aspx

求栈中最小元素操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值