Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 16e06ef

Browse files
hongyihengyanglbme
andauthored
feat: add solutions to lc problem: No.0911 (doocs#600)
No.0911.Online Election Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent a471f17 commit 16e06ef

File tree

4 files changed

+204
-4
lines changed

4 files changed

+204
-4
lines changed

‎solution/0900-0999/0911.Online Election/README.md‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,81 @@
4747
<!-- 这里可写当前语言的特殊实现逻辑 -->
4848

4949
```python
50-
50+
class TopVotedCandidate:
51+
52+
def __init__(self, persons: List[int], times: List[int]):
53+
self.persons = persons
54+
self.times = times
55+
mx, cur_win, n = -1, -1, len(persons)
56+
counter = [0] * (n + 1)
57+
self.win_persons = [0] * n
58+
for i, p in enumerate(persons):
59+
counter[p] += 1
60+
if counter[p] >= mx:
61+
mx = counter[p]
62+
cur_win = p
63+
self.win_persons[i] = cur_win
64+
65+
def q(self, t: int) -> int:
66+
left, right = 0, len(self.persons) - 1
67+
while left < right:
68+
mid = (left + right + 1) >> 1
69+
if self.times[mid] <= t:
70+
left = mid
71+
else:
72+
right = mid - 1
73+
return self.win_persons[left]
74+
75+
# Your TopVotedCandidate object will be instantiated and called as such:
76+
# obj = TopVotedCandidate(persons, times)
77+
# param_1 = obj.q(t)
5178
```
5279

5380
### **Java**
5481

5582
<!-- 这里可写当前语言的特殊实现逻辑 -->
5683

5784
```java
58-
85+
class TopVotedCandidate {
86+
private int[] persons;
87+
private int[] times;
88+
private int[] winPersons;
89+
90+
public TopVotedCandidate(int[] persons, int[] times) {
91+
this.persons = persons;
92+
this.times = times;
93+
int mx = -1, curWin = -1;
94+
int n = persons.length;
95+
int[] counter = new int[n + 1];
96+
winPersons = new int[n];
97+
for (int i = 0; i < n; ++i) {
98+
if (++counter[persons[i]] >= mx) {
99+
mx = counter[persons[i]];
100+
curWin = persons[i];
101+
}
102+
winPersons[i] = curWin;
103+
}
104+
}
105+
106+
public int q(int t) {
107+
int left = 0, right = persons.length - 1;
108+
while (left < right) {
109+
int mid = (left + right + 1) >> 1;
110+
if (times[mid] <= t) {
111+
left = mid;
112+
} else {
113+
right = mid - 1;
114+
}
115+
}
116+
return winPersons[left];
117+
}
118+
}
119+
120+
/**
121+
* Your TopVotedCandidate object will be instantiated and called as such:
122+
* TopVotedCandidate obj = new TopVotedCandidate(persons, times);
123+
* int param_1 = obj.q(t);
124+
*/
59125
```
60126

61127
### **...**

‎solution/0900-0999/0911.Online Election/README_EN.md‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,79 @@ This continues for 3 more queries at time 15, 24, and 8.
7373
### **Python3**
7474

7575
```python
76-
76+
class TopVotedCandidate:
77+
78+
def __init__(self, persons: List[int], times: List[int]):
79+
self.persons = persons
80+
self.times = times
81+
mx, cur_win, n = -1, -1, len(persons)
82+
counter = [0] * (n + 1)
83+
self.win_persons = [0] * n
84+
for i, p in enumerate(persons):
85+
counter[p] += 1
86+
if counter[p] >= mx:
87+
mx = counter[p]
88+
cur_win = p
89+
self.win_persons[i] = cur_win
90+
91+
def q(self, t: int) -> int:
92+
left, right = 0, len(self.persons) - 1
93+
while left < right:
94+
mid = (left + right + 1) >> 1
95+
if self.times[mid] <= t:
96+
left = mid
97+
else:
98+
right = mid - 1
99+
return self.win_persons[left]
100+
101+
# Your TopVotedCandidate object will be instantiated and called as such:
102+
# obj = TopVotedCandidate(persons, times)
103+
# param_1 = obj.q(t)
77104
```
78105

79106
### **Java**
80107

81108
```java
82-
109+
class TopVotedCandidate {
110+
private int[] persons;
111+
private int[] times;
112+
private int[] winPersons;
113+
114+
public TopVotedCandidate(int[] persons, int[] times) {
115+
this.persons = persons;
116+
this.times = times;
117+
int mx = -1, curWin = -1;
118+
int n = persons.length;
119+
int[] counter = new int[n + 1];
120+
winPersons = new int[n];
121+
for (int i = 0; i < n; ++i) {
122+
if (++counter[persons[i]] >= mx) {
123+
mx = counter[persons[i]];
124+
curWin = persons[i];
125+
}
126+
winPersons[i] = curWin;
127+
}
128+
}
129+
130+
public int q(int t) {
131+
int left = 0, right = persons.length - 1;
132+
while (left < right) {
133+
int mid = (left + right + 1) >> 1;
134+
if (times[mid] <= t) {
135+
left = mid;
136+
} else {
137+
right = mid - 1;
138+
}
139+
}
140+
return winPersons[left];
141+
}
142+
}
143+
144+
/**
145+
* Your TopVotedCandidate object will be instantiated and called as such:
146+
* TopVotedCandidate obj = new TopVotedCandidate(persons, times);
147+
* int param_1 = obj.q(t);
148+
*/
83149
```
84150

85151
### **...**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class TopVotedCandidate {
2+
private int[] persons;
3+
private int[] times;
4+
private int[] winPersons;
5+
6+
public TopVotedCandidate(int[] persons, int[] times) {
7+
this.persons = persons;
8+
this.times = times;
9+
int mx = -1, curWin = -1;
10+
int n = persons.length;
11+
int[] counter = new int[n + 1];
12+
winPersons = new int[n];
13+
for (int i = 0; i < n; ++i) {
14+
if (++counter[persons[i]] >= mx) {
15+
mx = counter[persons[i]];
16+
curWin = persons[i];
17+
}
18+
winPersons[i] = curWin;
19+
}
20+
}
21+
22+
public int q(int t) {
23+
int left = 0, right = persons.length - 1;
24+
while (left < right) {
25+
int mid = (left + right + 1) >> 1;
26+
if (times[mid] <= t) {
27+
left = mid;
28+
} else {
29+
right = mid - 1;
30+
}
31+
}
32+
return winPersons[left];
33+
}
34+
}
35+
36+
/**
37+
* Your TopVotedCandidate object will be instantiated and called as such:
38+
* TopVotedCandidate obj = new TopVotedCandidate(persons, times);
39+
* int param_1 = obj.q(t);
40+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class TopVotedCandidate:
2+
3+
def __init__(self, persons: List[int], times: List[int]):
4+
self.persons = persons
5+
self.times = times
6+
mx, cur_win, n = -1, -1, len(persons)
7+
counter = [0] * (n + 1)
8+
self.win_persons = [0] * n
9+
for i, p in enumerate(persons):
10+
counter[p] += 1
11+
if counter[p] >= mx:
12+
mx = counter[p]
13+
cur_win = p
14+
self.win_persons[i] = cur_win
15+
16+
def q(self, t: int) -> int:
17+
left, right = 0, len(self.persons) - 1
18+
while left < right:
19+
mid = (left + right + 1) >> 1
20+
if self.times[mid] <= t:
21+
left = mid
22+
else:
23+
right = mid - 1
24+
return self.win_persons[left]
25+
26+
# Your TopVotedCandidate object will be instantiated and called as such:
27+
# obj = TopVotedCandidate(persons, times)
28+
# param_1 = obj.q(t)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /