Skip to content

Commit bd93f3f

Browse files
authored
Create 2780-minimum-index-of-a-valid-split.js
1 parent 8f38541 commit bd93f3f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var minimumIndex = function(nums) {
6+
let n = nums.length
7+
if (n == 1) {
8+
return -1
9+
}
10+
let mp = {}
11+
let mx = 0
12+
for (const num of nums) {
13+
if(mp[num] == null) mp[num] = 0
14+
mp[num]++
15+
if (mp[num] > n/2) {
16+
mx = num
17+
}
18+
}
19+
let c = 0
20+
for (let i = 0; i < n-1; i++) {
21+
let num = nums[i]
22+
if (num == mx) {
23+
c++
24+
}
25+
if (c > (i+1)/2 && mp[mx]-c > (n-i-1)/2) {
26+
return i
27+
}
28+
}
29+
return -1
30+
};

0 commit comments

Comments
 (0)