URL: https://leetcode.com/problems/minimum-index-of-a-valid-split/ Signed-off-by: Matej Focko <me@mfocko.xyz>
47 lines
631 B
Go
47 lines
631 B
Go
package main
|
|
|
|
func minimumIndex(nums []int) int {
|
|
majorityElement := func() int {
|
|
dom, count := nums[0], 0
|
|
|
|
for _, x := range nums {
|
|
if x == dom {
|
|
count++
|
|
} else {
|
|
count--
|
|
}
|
|
|
|
if count == 0 {
|
|
dom = x
|
|
count = 1
|
|
}
|
|
}
|
|
|
|
return dom
|
|
}
|
|
dom := majorityElement()
|
|
|
|
// get frequency
|
|
domCount := 0
|
|
for _, x := range nums {
|
|
if x == dom {
|
|
domCount++
|
|
}
|
|
}
|
|
|
|
// Check for split
|
|
count := 0
|
|
for i, x := range nums {
|
|
if x == dom {
|
|
count++
|
|
}
|
|
|
|
remaining := domCount - count
|
|
if 2*count > i+1 && 2*remaining > len(nums)-i-1 {
|
|
return i
|
|
}
|
|
}
|
|
|
|
// couldn't find a valid split
|
|
return -1
|
|
}
|