-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path2780-minimum-index-of-a-valid-split.js
52 lines (46 loc) · 1.48 KB
/
2780-minimum-index-of-a-valid-split.js
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
51
52
/**
* 2780. Minimum Index of a Valid Split
* https://leetcode.com/problems/minimum-index-of-a-valid-split/
* Difficulty: Medium
*
* An element x of an integer array arr of length m is dominant if more than half the elements
* of arr have a value of x.
*
* You are given a 0-indexed integer array nums of length n with one dominant element.
*
* You can split nums at an index i into two arrays nums[0, ..., i] and nums[i + 1, ..., n - 1],
* but the split is only valid if:
* - 0 <= i < n - 1
* - nums[0, ..., i], and nums[i + 1, ..., n - 1] have the same dominant element.
*
* Here, nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j,
* both ends being inclusive. Particularly, if j < i then nums[i, ..., j] denotes an empty subarray.
*
* Return the minimum index of a valid split. If no valid split exists, return -1.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var minimumIndex = function(nums) {
const map = new Map();
for (const num of nums) {
map.set(num, (map.get(num) || 0) + 1);
}
let dominantElement;
for (const [num, count] of map) {
if (count * 2 > nums.length) {
dominantElement = num;
break;
}
}
let leftCount = 0;
for (let i = 0; i < nums.length - 1; i++) {
leftCount += nums[i] === dominantElement ? 1 : 0;
const rightCount = map.get(dominantElement) - leftCount;
if (leftCount * 2 > i + 1 && rightCount * 2 > nums.length - i - 1) {
return i;
}
}
return -1;
};