日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 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 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 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 數組添加n次相同元素

總結

以上是生活随笔為你收集整理的js 数组添加n次相同元素_数组中两次出现相同元素之间的最大距离的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产成人av免费观看 | 涩视频在线观看 | 伊人焦久影院 | 韩国一级一片高清免费观看 | 欧美区一区 | 青青草免费观看 | 国产三级免费观看 | 国语对白做受按摩的注意事项 | 色视频免费 | 国产一级视频 | 姝姝窝人体www聚色窝 | 国产精品毛片一区二区三区 | 国产一级不卡毛片 | 国产欧美在线观看不卡 | 日本免费网 | 天堂网中文在线观看 | xfplay5566色资源网站 | 亚洲超碰av | 91热久久| 国产又黄又粗又猛又爽的视频 | 午夜亚洲aⅴ无码高潮片苍井空 | 一本一道波多野结衣av黑人 | 中文字幕丰满人伦在线 | 在线观看亚洲精品 | 麻豆精品a∨在线观看 | 欧美三级日本三级 | av免费观看在线 | 大象传媒成人在线观看 | 精品九九久久 | 亚洲人久久 | 国产中文字幕精品 | gav久久| 欧美日韩国产麻豆 | 欧美日韩国产一级片 | 99视频精品免费 | www.亚洲免费 | 特黄一区二区 | 在线不卡免费av | 中文字幕在线观看一区二区三区 | 日本福利一区 | 女人18毛片水真多 | 久久看看 | 国产精品一区二区免费在线观看 | 国产精品久久久久久久久久免费 | 午夜影院在线播放 | 91性高潮久久久久久久久 | 国产一区高清 | 日韩一卡二卡三卡四卡 | 伊人影院中文字幕 | 一区二区免费在线观看 | 日本xx视频 | 欧美三个黑人玩3p | 亚洲精品免费在线视频 | 国产精品亚洲一区二区三区 | 免费成人在线视频观看 | 不卡中文一二三区 | 99久久国产综合 | 在线免费观看视频 | 一级生活毛片 | 亚洲黄色在线 | 精品久久久久一区二区 | 蜜桃传媒一区二区亚洲av | 91麻豆精品| 国产精品毛片久久久 | 在线免费观看视频网站 | aa一级视频| 国产精品无码av无码 | 欧美 日韩 国产在线 | 久草精品在线 | 日本xxxx裸体xxxx出水 | 男女黄床上色视频免费的软件 | 欧美激情区 | 亚洲婷婷久久综合 | 一级做a爱片久久 | 超在线视频 | 日韩无码精品一区二区 | 久久橹| 国产免费一区二区三区三州老师 | 艳妇乳肉豪妇荡乳 | 色天使亚洲 | 麻豆av网址 | 激情五月婷婷综合网 | 超碰网址 | 在线色亚洲 | 久久久视频在线 | 国产视频不卡一区 | 插久久 | 熟女一区二区三区视频 | 亚洲偷偷自拍 | 在线免费国产 | 自拍偷拍 亚洲 | 欧美精品人妻一区二区 | 天天色天| 麻豆亚洲| 欧美成人三级在线观看 | 国产微拍一区 | 一本色道久久综合狠狠躁 | 美女高潮在线 | 中文字幕黄色片 |