js 数组添加n次相同元素_数组中两次出现相同元素之间的最大距离
js 數(shù)組添加n次相同元素
Prerequisite: Hashing data structure
先決條件: 哈希數(shù)據(jù)結(jié)構(gòu)
Problem statement:
問題陳述:
Find maximum distance between two occurrences of same element in the array.
查找兩次出現(xiàn)的相同元素在數(shù)組中的最大距離。
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.
輸入數(shù)組= [1、2、3、1、2、4、5、6、2、3]
兩個相同元素之間的最大距離為7。鍵2的最左側(cè)位置為1,鍵2的最右側(cè)位置為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
我們可以使用哈希映射(哈希表)解決此問題。 一件事很清楚,要在同一元素的兩次出現(xiàn)之間達(dá)到最大距離,我們需要跟蹤元素(鍵)的所有位置。 因此,我們的目標(biāo)是找出陣列中任何唯一元素的位置之間的最大差異。 例如,元素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?
那么我們?nèi)绾卧O(shè)計哈希表的問題以及哈希函數(shù)是什么呢?
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?
好的,所以這里的每個元素都是我們的鍵,哈希表具有數(shù)組范圍的大小。 因此,如果數(shù)組的范圍為[0,15],則哈希表大小將為16。
我們的哈希函數(shù)將是什么?如何將鍵映射到哈希表中的對應(yīng)位置?
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.
這里的哈希函數(shù)h(x)= x ,而不是使用線性探測存儲密鑰本身,我們將使用線性探測存儲位置。 但是在我的實現(xiàn)中,我使用了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.
因此,在創(chuàng)建哈希表之后,我們可以輕松找到每個唯一鍵的兩次出現(xiàn)之間的最大距離,并最終可以為數(shù)組中的同一元素獲得兩次出現(xiàn)之間的最大距離。
So the algorithm will be,
因此算法將是
Step 1:
第1步:
Create the hash table like below: Initially hash table is emptyFor each key in input array: Hash[key].push_back(key_position)Step 2:
第2步:
Initially max_distance=0 For each locationIf(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=7After 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 7So max_distance = 7 for both 2(8-1) and 3(9-2) Thus the answer is 7C++ implementation:
C ++實現(xiàn):
// 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 itselfmap<int, vector<int> > hash;//for each numberfor (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 keyint 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 maxdiffreturn 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 數(shù)組添加n次相同元素
總結(jié)
以上是生活随笔為你收集整理的js 数组添加n次相同元素_数组中两次出现相同元素之间的最大距离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 如何统一后端返回格
- 下一篇: python 示例_Python中带有示