-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
697-Degree-of-an-Array.cpp
50 lines (48 loc) · 1.18 KB
/
697-Degree-of-an-Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
unordered_map<int, int> umap;
for( auto x: nums)
umap[x]++;
int max_count = 0;
for(auto x: umap)
{
if(max_count < x.second)
{
max_count = x.second;
}
}
if(max_count == 1){
return 1;
}
vector<int> allWithMax;
for(auto x: umap)
{
if(max_count == x.second)
{
allWithMax.push_back(x.first);
}
}
int sml_lngth = INT_MAX;
for(auto res: allWithMax){
int i = 0, count = 0, Max_count = max_count;
while(Max_count > 0)
{
if(nums[i] != res && count == 0){
i++;
}else if( nums[i] == res )
{
count++;
Max_count--;
i++;
}else{
count++;
i++;
}
}
if(sml_lngth > count)
sml_lngth = count;
}
return sml_lngth;
}
};