js 数组添加n次相同元素_数组中两次出现相同元素之间的最大距离

js 数组添加n次相同元素

Prerequisite: Hashing data structure

先决条件: 哈希数据结构

Problem statement:

问题陈述:

Find maximum distance between two occurrences of same element in the array.

查找两次出现的相同元素在数组中的最大距离。

Example:

例:

Input array= [1, 2, 3, 1, 2, 4, 5, 6, 2, 3]
The maximum distance between any two same elements is 7. The leftmost position for key 2 is 1 and the rightmost position for key 2 is 8 and thus the distance is 7 which is maximum. For any other keys, the max distances between their positions are less than 7.

输入数组= [1、2、3、1、2、4、5、6、2、3]
两个相同元素之间的最大距离为7。键2的最左侧位置为1,键2的最右侧位置为8,因此距离为7,最大。 对于其他任何键,其位置之间的最大距离小于7。

Solution:

解:

We can solve this problem using a hash map (hash table). One thing is clear that to achieve maximum distance between two occurrences of the same element, we need to keep track of all positions for the elements (key). So, our target is to find maximum differences between positions for any unique element in the array. For example say, element A has positions 2, 7, 8, and another element B has positions 1, 10. Then the maximum distance for element A is 8-2=6, whereas maximum distance for element B is 10-1=9

我们可以使用哈希映射(哈希表)解决此问题。 一件事很清楚,要在同一元素的两次出现之间达到最大距离,我们需要跟踪元素(键)的所有位置。 因此,我们的目标是找出阵列中任何唯一元素的位置之间的最大差异。 例如,元素A具有位置2、7、8,而另一个元素B具有位置1、10。则元素A的最大距离为8-2 = 6,而元素B的最大距离为10-1 = 9

So how can we design the problem with the hash table and what will be the hash function?

那么我们如何设计哈希表的问题以及哈希函数是什么呢?

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

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

The hash function h(x)=x here but instead of storing the key itself using linear probing, we will store the positions using linear probing. But in my implementation, I have used vector to store easily instead of a linked list.

这里的哈希函数h(x)= x ,而不是使用线性探测存储密钥本身,我们将使用线性探测存储位置。 但是在我的实现中,我使用了vector而不是链表来轻松存储。

So, for example, if we have three 2s as our key, the hash table will store the three unique positions of 2 at location 2.

因此,例如,如果我们将三个2用作键,则哈希表将在位置2存储三个唯一的位置2。

So, after the hash table is created we can easily find the maximum distance between occurrences for each unique key and can finally achieve the maximum distance between two occurrences for the same element in the array.

因此,在创建哈希表之后,我们可以轻松找到每个唯一键的两次出现之间的最大距离,并最终可以为数组中的同一元素获得两次出现之间的最大距离。

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].push_back(key_position)

Step 2:

第2步:

Initially max_distance=0
For each location
    If(difference(last position-first position)>max_distance)
    Update max_distance as difference(last position-first position)

After this,
max_distance contains the maximum distance between 
two occurrences of same elements in the array.
max_distance = the maximum distance between 
two occurrences of same elements in the array.

Dry run with the example:

空运行示例:

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

After creating the hash table as step1 we will have,
Index	Value
1	0->3
2	1->4->8
3	2->9
4	5
5	6
6	7

So max_distance = 7 for both 2(8-1) and 3(9-2)
Thus the answer is 7

C++ implementation:

C ++实现:

// C++ program to find maximum distance between 
// two occurrences of same element in array
#include <bits/stdc++.h>
using namespace std;

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

    map<int, vector<int> > hash;

    //for each number
    for (int i = 0; i < arr.size(); i++) {
        hash[arr[i]].push_back(i);
    }

    //now to find max distance b/w two occurrences
    //we need to check difference b/w first and 
    //last position for each unique keys
    //maxdiff=max(last-first) for each unique key
    int maxdiff = 0;
    for (auto it = hash.begin(); it != hash.end(); it++) {
        int first = it->second[0];
        int last = it->second[it->second.size() - 1];
        if (last - first > maxdiff) {
            maxdiff = last - first;
        }
    }

    //so ans will be updated maxdiff
    return maxdiff;
}

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 deletion required to make all elements same is: ";
    cout << maximum_distance_bw_occurences(arr, n) << endl;
    
    return 0;
}

Output:

输出:

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


翻译自: https://www.includehelp.com/data-structure-tutorial/maximum-distance-between-two-occurrences-of-same-element-in-array.aspx

js 数组添加n次相同元素

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值