Skip to content

Commit 152817f

Browse files
Day 27 : Minimum index of a valid split
1 parent d4ba436 commit 152817f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int minimumIndex(vector<int>& nums) {
4+
int n = nums.size();
5+
unordered_map<int, int> left_mp; // keep one element in the left map
6+
unordered_map<int, int> right_mp; // populate the 1 to n-1 element in the right map
7+
for(int i = 0; i < n; i++){
8+
right_mp[nums[i]] += 1;
9+
}
10+
11+
for(int i = 0; i < n; i++){
12+
left_mp[nums[i]]++;
13+
right_mp[nums[i]]--;
14+
15+
int n1 = i+1;
16+
int n2 = n-i-1;
17+
18+
if(left_mp[nums[i]] > n1/2 && right_mp[nums[i]] > n2/2){
19+
return i;
20+
}
21+
}
22+
23+
return -1;
24+
}
25+
};

0 commit comments

Comments
 (0)