Files
LeetCode-Go/leetcode/0911.Online-Election/911. Online Election.go
2020-08-07 17:06:53 +08:00

39 lines
833 B
Go

package leetcode
import (
"sort"
)
// TopVotedCandidate define
type TopVotedCandidate struct {
persons []int
times []int
}
// Constructor911 define
func Constructor911(persons []int, times []int) TopVotedCandidate {
leaders, votes := make([]int, len(persons)), make([]int, len(persons))
leader := persons[0]
for i := 0; i < len(persons); i++ {
p := persons[i]
votes[p]++
if votes[p] >= votes[leader] {
leader = p
}
leaders[i] = leader
}
return TopVotedCandidate{persons: leaders, times: times}
}
// Q define
func (tvc *TopVotedCandidate) Q(t int) int {
i := sort.Search(len(tvc.times), func(p int) bool { return tvc.times[p] > t })
return tvc.persons[i-1]
}
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* obj := Constructor(persons, times);
* param_1 := obj.Q(t);
*/