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 ac3910a

Browse files
committed
feat: add solutions to lc problem: No.0911
No.0911.Online Election
1 parent a341d4b commit ac3910a

File tree

9 files changed

+273
-41
lines changed

9 files changed

+273
-41
lines changed

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

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
<li><code>TopVotedCandidate.q(int t)</code>&nbsp;被调用时总是满足&nbsp;<code>t &gt;= times[0]</code>。</li>
3636
</ol>
3737

38-
3938
## 解法
4039

4140
<!-- 这里可写通用的实现逻辑 -->
@@ -50,7 +49,6 @@
5049
class TopVotedCandidate:
5150

5251
def __init__(self, persons: List[int], times: List[int]):
53-
self.persons = persons
5452
self.times = times
5553
mx, cur_win, n = -1, -1, len(persons)
5654
counter = [0] * (n + 1)
@@ -63,7 +61,7 @@ class TopVotedCandidate:
6361
self.win_persons[i] = cur_win
6462

6563
def q(self, t: int) -> int:
66-
left, right = 0, len(self.persons) - 1
64+
left, right = 0, len(self.win_persons) - 1
6765
while left < right:
6866
mid = (left + right + 1) >> 1
6967
if self.times[mid] <= t:
@@ -83,12 +81,10 @@ class TopVotedCandidate:
8381

8482
```java
8583
class TopVotedCandidate {
86-
private int[] persons;
8784
private int[] times;
8885
private int[] winPersons;
8986

9087
public TopVotedCandidate(int[] persons, int[] times) {
91-
this.persons = persons;
9288
this.times = times;
9389
int mx = -1, curWin = -1;
9490
int n = persons.length;
@@ -102,9 +98,9 @@ class TopVotedCandidate {
10298
winPersons[i] = curWin;
10399
}
104100
}
105-
101+
106102
public int q(int t) {
107-
int left = 0, right = persons.length - 1;
103+
int left = 0, right = winPersons.length - 1;
108104
while (left < right) {
109105
int mid = (left + right + 1) >> 1;
110106
if (times[mid] <= t) {
@@ -124,6 +120,96 @@ class TopVotedCandidate {
124120
*/
125121
```
126122

123+
### **C++**
124+
125+
```cpp
126+
class TopVotedCandidate {
127+
public:
128+
vector<int> times;
129+
vector<int> winPersons;
130+
131+
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
132+
this->times = times;
133+
int mx = -1, curWin = -1;
134+
int n = persons.size();
135+
vector<int> counter(n + 1);
136+
winPersons.resize(n);
137+
for (int i = 0; i < n; ++i)
138+
{
139+
if (++counter[persons[i]] >= mx)
140+
{
141+
mx = counter[persons[i]];
142+
curWin = persons[i];
143+
}
144+
winPersons[i] = curWin;
145+
}
146+
147+
}
148+
149+
int q(int t) {
150+
int left = 0, right = winPersons.size() - 1;
151+
while (left < right)
152+
{
153+
int mid = (left + right + 1) >> 1;
154+
if (times[mid] <= t) left = mid;
155+
else right = mid - 1;
156+
}
157+
return winPersons[left];
158+
}
159+
};
160+
161+
/**
162+
* Your TopVotedCandidate object will be instantiated and called as such:
163+
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
164+
* int param_1 = obj->q(t);
165+
*/
166+
```
167+
168+
### **Go**
169+
170+
```go
171+
type TopVotedCandidate struct {
172+
times []int
173+
winPersons []int
174+
}
175+
176+
func Constructor(persons []int, times []int) TopVotedCandidate {
177+
mx, curWin, n := -1, -1, len(persons)
178+
counter := make([]int, n+1)
179+
winPersons := make([]int, n)
180+
for i, p := range persons {
181+
counter[p]++
182+
if counter[p] >= mx {
183+
mx = counter[p]
184+
curWin = p
185+
}
186+
winPersons[i] = curWin
187+
}
188+
return TopVotedCandidate{
189+
times, winPersons,
190+
}
191+
}
192+
193+
func (this *TopVotedCandidate) Q(t int) int {
194+
left, right := 0, len(this.winPersons)-1
195+
for left < right {
196+
mid := (left + right + 1) >> 1
197+
if this.times[mid] <= t {
198+
left = mid
199+
} else {
200+
right = mid - 1
201+
}
202+
}
203+
return this.winPersons[left]
204+
}
205+
206+
/**
207+
* Your TopVotedCandidate object will be instantiated and called as such:
208+
* obj := Constructor(persons, times);
209+
* param_1 := obj.Q(t);
210+
*/
211+
```
212+
127213
### **...**
128214

129215
```

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

Lines changed: 93 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,16 @@
66

77
<p>In an election, the <code>i</code>-th&nbsp;vote was cast for <code>persons[i]</code> at time <code>times[i]</code>.</p>
88

9-
10-
119
<p>Now, we would like to implement the following query function: <code>TopVotedCandidate.q(int t)</code> will return the number of the person that was leading the election at time <code>t</code>.&nbsp;&nbsp;</p>
1210

13-
14-
1511
<p>Votes cast at time <code>t</code> will count towards our query.&nbsp; In the case of a tie, the most recent vote (among tied candidates) wins.</p>
1612

17-
18-
1913
<p>&nbsp;</p>
2014

21-
22-
2315
<div>
2416

2517
<p><strong>Example 1:</strong></p>
2618

27-
28-
2919
<pre>
3020

3121
<strong>Input: </strong><span id="example-input-1-1">[&quot;TopVotedCandidate&quot;,&quot;q&quot;,&quot;q&quot;,&quot;q&quot;,&quot;q&quot;,&quot;q&quot;,&quot;q&quot;]</span>, <span id="example-input-1-2">[[[0,1,1,0,0,1,0],[0,5,10,15,20,25,30]],[3],[12],[25],[15],[24],[8]]</span>
@@ -44,16 +34,10 @@ This continues for 3 more queries at time 15, 24, and 8.
4434

4535
</pre>
4636

47-
48-
4937
<p>&nbsp;</p>
5038

51-
52-
5339
<p><strong>Note:</strong></p>
5440

55-
56-
5741
<ol>
5842
<li><code>1 &lt;= persons.length = times.length &lt;= 5000</code></li>
5943
<li><code>0 &lt;= persons[i] &lt;= persons.length</code></li>
@@ -64,8 +48,6 @@ This continues for 3 more queries at time 15, 24, and 8.
6448

6549
</div>
6650

67-
68-
6951
## Solutions
7052

7153
<!-- tabs:start -->
@@ -76,7 +58,6 @@ This continues for 3 more queries at time 15, 24, and 8.
7658
class TopVotedCandidate:
7759

7860
def __init__(self, persons: List[int], times: List[int]):
79-
self.persons = persons
8061
self.times = times
8162
mx, cur_win, n = -1, -1, len(persons)
8263
counter = [0] * (n + 1)
@@ -89,7 +70,7 @@ class TopVotedCandidate:
8970
self.win_persons[i] = cur_win
9071

9172
def q(self, t: int) -> int:
92-
left, right = 0, len(self.persons) - 1
73+
left, right = 0, len(self.win_persons) - 1
9374
while left < right:
9475
mid = (left + right + 1) >> 1
9576
if self.times[mid] <= t:
@@ -107,12 +88,10 @@ class TopVotedCandidate:
10788

10889
```java
10990
class TopVotedCandidate {
110-
private int[] persons;
11191
private int[] times;
11292
private int[] winPersons;
11393

11494
public TopVotedCandidate(int[] persons, int[] times) {
115-
this.persons = persons;
11695
this.times = times;
11796
int mx = -1, curWin = -1;
11897
int n = persons.length;
@@ -126,9 +105,9 @@ class TopVotedCandidate {
126105
winPersons[i] = curWin;
127106
}
128107
}
129-
108+
130109
public int q(int t) {
131-
int left = 0, right = persons.length - 1;
110+
int left = 0, right = winPersons.length - 1;
132111
while (left < right) {
133112
int mid = (left + right + 1) >> 1;
134113
if (times[mid] <= t) {
@@ -148,6 +127,96 @@ class TopVotedCandidate {
148127
*/
149128
```
150129

130+
### **C++**
131+
132+
```cpp
133+
class TopVotedCandidate {
134+
public:
135+
vector<int> times;
136+
vector<int> winPersons;
137+
138+
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
139+
this->times = times;
140+
int mx = -1, curWin = -1;
141+
int n = persons.size();
142+
vector<int> counter(n + 1);
143+
winPersons.resize(n);
144+
for (int i = 0; i < n; ++i)
145+
{
146+
if (++counter[persons[i]] >= mx)
147+
{
148+
mx = counter[persons[i]];
149+
curWin = persons[i];
150+
}
151+
winPersons[i] = curWin;
152+
}
153+
154+
}
155+
156+
int q(int t) {
157+
int left = 0, right = winPersons.size() - 1;
158+
while (left < right)
159+
{
160+
int mid = (left + right + 1) >> 1;
161+
if (times[mid] <= t) left = mid;
162+
else right = mid - 1;
163+
}
164+
return winPersons[left];
165+
}
166+
};
167+
168+
/**
169+
* Your TopVotedCandidate object will be instantiated and called as such:
170+
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
171+
* int param_1 = obj->q(t);
172+
*/
173+
```
174+
175+
### **Go**
176+
177+
```go
178+
type TopVotedCandidate struct {
179+
times []int
180+
winPersons []int
181+
}
182+
183+
func Constructor(persons []int, times []int) TopVotedCandidate {
184+
mx, curWin, n := -1, -1, len(persons)
185+
counter := make([]int, n+1)
186+
winPersons := make([]int, n)
187+
for i, p := range persons {
188+
counter[p]++
189+
if counter[p] >= mx {
190+
mx = counter[p]
191+
curWin = p
192+
}
193+
winPersons[i] = curWin
194+
}
195+
return TopVotedCandidate{
196+
times, winPersons,
197+
}
198+
}
199+
200+
func (this *TopVotedCandidate) Q(t int) int {
201+
left, right := 0, len(this.winPersons)-1
202+
for left < right {
203+
mid := (left + right + 1) >> 1
204+
if this.times[mid] <= t {
205+
left = mid
206+
} else {
207+
right = mid - 1
208+
}
209+
}
210+
return this.winPersons[left]
211+
}
212+
213+
/**
214+
* Your TopVotedCandidate object will be instantiated and called as such:
215+
* obj := Constructor(persons, times);
216+
* param_1 := obj.Q(t);
217+
*/
218+
```
219+
151220
### **...**
152221

153222
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class TopVotedCandidate {
2+
public:
3+
vector<int> times;
4+
vector<int> winPersons;
5+
6+
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
7+
this->times = times;
8+
int mx = -1, curWin = -1;
9+
int n = persons.size();
10+
vector<int> counter(n + 1);
11+
winPersons.resize(n);
12+
for (int i = 0; i < n; ++i)
13+
{
14+
if (++counter[persons[i]] >= mx)
15+
{
16+
mx = counter[persons[i]];
17+
curWin = persons[i];
18+
}
19+
winPersons[i] = curWin;
20+
}
21+
22+
}
23+
24+
int q(int t) {
25+
int left = 0, right = winPersons.size() - 1;
26+
while (left < right)
27+
{
28+
int mid = (left + right + 1) >> 1;
29+
if (times[mid] <= t) left = mid;
30+
else right = mid - 1;
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+
*/

0 commit comments

Comments
(0)

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