-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path911-OnlineElection.go
More file actions
134 lines (117 loc) · 4.79 KB
/
911-OnlineElection.go
File metadata and controls
134 lines (117 loc) · 4.79 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
// 911. Online Election
// You are given two integer arrays persons and times.
// In an election, the ith vote was cast for persons[i] at time times[i].
// For each query at a time t, find the person that was leading the election at time t.
// Votes cast at time t will count towards our query.
// In the case of a tie, the most recent vote (among tied candidates) wins.
// Implement the TopVotedCandidate class:
// TopVotedCandidate(int[] persons, int[] times)
// Initializes the object with the persons and times arrays.
// int q(int t)
// Returns the number of the person that was leading the election at time t according to the mentioned rules.
// Example 1:
// Input
// ["TopVotedCandidate", "q", "q", "q", "q", "q", "q"]
// [[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]]
// Output
// [null, 0, 1, 1, 0, 0, 1]
// Explanation
// TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
// topVotedCandidate.q(3); // return 0, At time 3, the votes are [0], and 0 is leading.
// topVotedCandidate.q(12); // return 1, At time 12, the votes are [0,1,1], and 1 is leading.
// topVotedCandidate.q(25); // return 1, At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
// topVotedCandidate.q(15); // return 0
// topVotedCandidate.q(24); // return 0
// topVotedCandidate.q(8); // return 1
// Constraints:
// 1 <= persons.length <= 5000
// times.length == persons.length
// 0 <= persons[i] < persons.length
// 0 <= times[i] <= 10^9
// times is sorted in a strictly increasing order.
// times[0] <= t <= 10^9
// At most 10^4 calls will be made to q.
import "fmt"
import "sort"
type TopVotedCandidate struct {
list [][2]int
}
func Constructor(persons []int, times []int) TopVotedCandidate {
list, dict, winner := [][2]int{}, make(map[int]int), -1
for i, v := range persons {
if winner == -1 || v == winner {
winner = v
dict[winner]++
} else {
dict[v]++
if dict[v] >= dict[winner] {
winner = v
}
}
list = append(list, [2]int{times[i], winner})
}
return TopVotedCandidate{list: list}
}
func (this *TopVotedCandidate) Q(t int) int {
index := sort.Search(len(this.list), func(i int) bool {
return (this.list)[i][0] > t
})
return (this.list)[index - 1][1]
}
type TopVotedCandidate1 struct {
arr, times []int
}
func Constructor1(persons []int, times []int) TopVotedCandidate1 {
arr, cnt, cur := make([]int, len(times)), make([]int, len(times)), -1
for i := range times {
cnt[persons[i]]++
if cur < 0 || cnt[persons[i]] >= cnt[cur] {
cur = persons[i]
}
arr[i] = cur
}
return TopVotedCandidate1{arr, times}
}
func (this *TopVotedCandidate1) Q(t int) int {
i := sort.Search(len(this.times), func(i int) bool {
return this.times[i] > t
})
return this.arr[i-1]
}
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* obj := Constructor(persons, times);
* param_1 := obj.Q(t);
*/
func main() {
// TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
obj := Constructor([]int{0, 1, 1, 0, 0, 1, 0},[]int{0, 5, 10, 15, 20, 25, 30})
fmt.Println(obj)
// topVotedCandidate.q(3); // return 0, At time 3, the votes are [0], and 0 is leading.
fmt.Println(obj.Q(3)) // 0
// topVotedCandidate.q(12); // return 1, At time 12, the votes are [0,1,1], and 1 is leading.
fmt.Println(obj.Q(12)) // 1
// topVotedCandidate.q(25); // return 1, At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
fmt.Println(obj.Q(25)) // 1
// topVotedCandidate.q(15); // return 0
fmt.Println(obj.Q(15)) // 0
// topVotedCandidate.q(24); // return 0
fmt.Println(obj.Q(24)) // 0
// topVotedCandidate.q(8); // return 1
fmt.Println(obj.Q(8)) // 1
obj1 := Constructor([]int{0, 1, 1, 0, 0, 1, 0},[]int{0, 5, 10, 15, 20, 25, 30})
fmt.Println(obj1)
// topVotedCandidate.q(3); // return 0, At time 3, the votes are [0], and 0 is leading.
fmt.Println(obj1.Q(3)) // 0
// topVotedCandidate.q(12); // return 1, At time 12, the votes are [0,1,1], and 1 is leading.
fmt.Println(obj1.Q(12)) // 1
// topVotedCandidate.q(25); // return 1, At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
fmt.Println(obj1.Q(25)) // 1
// topVotedCandidate.q(15); // return 0
fmt.Println(obj1.Q(15)) // 0
// topVotedCandidate.q(24); // return 0
fmt.Println(obj1.Q(24)) // 0
// topVotedCandidate.q(8); // return 1
fmt.Println(obj1.Q(8)) // 1
}