Skip to content

Commit 347fa6a

Browse files
committed
feat: update solutions to lc problem: No.0911
No.0911.Online Election
1 parent 4653764 commit 347fa6a

File tree

7 files changed

+197
-174
lines changed

7 files changed

+197
-174
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265

266266
## 赞助者
267267

268-
感谢以下个人、组织对本项目的赞助
268+
感谢以下个人、组织对本项目的支持和赞助
269269

270270
<a href="https://opencollective.com/doocs-leetcode/backers.svg?width=890" target="_blank"><img src="https://opencollective.com/doocs-leetcode/backers.svg?width=890"></a>
271271

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

+70-48
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,63 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>在选举中,第&nbsp;<code>i</code>&nbsp;张票是在时间为&nbsp;<code>times[i]</code>&nbsp;时投给&nbsp;<code>persons[i]</code>&nbsp;的。</p>
9+
<p>给你两个整数数组 <code>persons</code> 和 <code>times</code> 。在选举中,第&nbsp;<code>i</code>&nbsp;张票是在时刻为&nbsp;<code>times[i]</code>&nbsp;时投给候选人 <code>persons[i]</code>&nbsp;的。</p>
1010

11-
<p>现在,我们想要实现下面的查询函数: <code>TopVotedCandidate.q(int t)</code> 将返回在&nbsp;<code>t</code> 时刻主导选举的候选人的编号。</p>
11+
<p>对于发生在时刻 <code>t</code> 的每个查询,需要找出在&nbsp;<code>t</code> 时刻在选举中领先的候选人的编号。</p>
1212

1313
<p>在&nbsp;<code>t</code> 时刻投出的选票也将被计入我们的查询之中。在平局的情况下,最近获得投票的候选人将会获胜。</p>
1414

15+
<p>实现 <code>TopVotedCandidate</code> 类:</p>
16+
17+
<ul>
18+
<li><code>TopVotedCandidate(int[] persons, int[] times)</code> 使用&nbsp;<code>persons</code> 和 <code>times</code> 数组初始化对象。</li>
19+
<li><code>int q(int t)</code> 根据前面描述的规则,返回在时刻 <code>t</code> 在选举中领先的候选人的编号。</li>
20+
</ul>
21+
&nbsp;
22+
1523
<p><strong>示例:</strong></p>
1624

17-
<pre><strong>输入:</strong>[&quot;TopVotedCandidate&quot;,&quot;q&quot;,&quot;q&quot;,&quot;q&quot;,&quot;q&quot;,&quot;q&quot;,&quot;q&quot;], [[[0,1,1,0,0,1,0],[0,5,10,15,20,25,30]],[3],[12],[25],[15],[24],[8]]
18-
<strong>输出:</strong>[null,0,1,1,0,0,1]
25+
<pre>
26+
<strong>输入:</strong>
27+
["TopVotedCandidate", "q", "q", "q", "q", "q", "q"]
28+
[[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]]
29+
<strong>输出:</strong>
30+
[null, 0, 1, 1, 0, 0, 1]
31+
1932
<strong>解释:</strong>
20-
时间为 3,票数分布情况是 [0],编号为 0 的候选人领先。
21-
时间为 12,票数分布情况是 [0,1,1],编号为 1 的候选人领先。
22-
时间为 25,票数分布情况是 [0,1,1,0,0,1],编号为 1 的候选人领先(因为最近的投票结果是平局)。
23-
在时间 15、24 和 8 处继续执行 3 个查询。
33+
TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
34+
topVotedCandidate.q(3); // 返回 0 ,在时刻 3 ,票数分布为 [0] ,编号为 0 的候选人领先。
35+
topVotedCandidate.q(12); // 返回 1 ,在时刻 12 ,票数分布为 [0,1,1] ,编号为 1 的候选人领先。
36+
topVotedCandidate.q(25); // 返回 1 ,在时刻 25 ,票数分布为 [0,1,1,0,0,1] ,编号为 1 的候选人领先。(在平局的情况下,1 是最近获得投票的候选人)。
37+
topVotedCandidate.q(15); // 返回 0
38+
topVotedCandidate.q(24); // 返回 0
39+
topVotedCandidate.q(8); // 返回 1
2440
</pre>
2541

2642
<p>&nbsp;</p>
2743

2844
<p><strong>提示:</strong></p>
2945

30-
<ol>
31-
<li><code>1 &lt;= persons.length = times.length &lt;= 5000</code></li>
32-
<li><code>0 &lt;= persons[i] &lt;= persons.length</code></li>
33-
<li><code>times</code>&nbsp;是严格递增的数组,所有元素都在&nbsp;<code>[0, 10^9]</code>&nbsp;范围中。</li>
34-
<li>每个测试用例最多调用&nbsp;<code>10000</code>&nbsp;次&nbsp;<code>TopVotedCandidate.q</code>。</li>
35-
<li><code>TopVotedCandidate.q(int t)</code>&nbsp;被调用时总是满足&nbsp;<code>t &gt;= times[0]</code>。</li>
36-
</ol>
46+
<ul>
47+
<li><code>1 &lt;= persons.length &lt;= 5000</code></li>
48+
<li><code>times.length == persons.length</code></li>
49+
<li><code>0 &lt;= persons[i] &lt; persons.length</code></li>
50+
<li><code>0 &lt;= times[i] &lt;= 10<sup>9</sup></code></li>
51+
<li><code>times</code> 是一个严格递增的有序数组</li>
52+
<li><code>times[0] &lt;= t &lt;= 10<sup>9</sup></code></li>
53+
<li>每个测试用例最多调用 <code>10<sup>4</sup></code> 次 <code>q</code></li>
54+
</ul>
3755

3856
## 解法
3957

4058
<!-- 这里可写通用的实现逻辑 -->
4159

60+
二分查找。
61+
62+
先预处理得到每个时刻的领先的候选人编号 `wins[i]`
63+
64+
然后对于每次查询 q,二分查找得到小于等于 t 时刻的最大时刻 left,返回 `wins[left]` 即可。
65+
4266
<!-- tabs:start -->
4367

4468
### **Python3**
@@ -49,26 +73,26 @@
4973
class TopVotedCandidate:
5074

5175
def __init__(self, persons: List[int], times: List[int]):
76+
mx = cur = 0
77+
counter = Counter()
5278
self.times = times
53-
mx, cur_win, n = -1, -1, len(persons)
54-
counter = [0] * (n + 1)
55-
self.win_persons = [0] * n
79+
self.wins = [0] * len(persons)
5680
for i, p in enumerate(persons):
5781
counter[p] += 1
5882
if counter[p] >= mx:
59-
mx = counter[p]
60-
cur_win = p
61-
self.win_persons[i] = cur_win
83+
mx, cur = counter[p], p
84+
self.wins[i] = cur
6285

6386
def q(self, t: int) -> int:
64-
left, right = 0, len(self.win_persons) - 1
87+
left, right = 0, len(self.wins) - 1
6588
while left < right:
6689
mid = (left + right + 1) >> 1
6790
if self.times[mid] <= t:
6891
left = mid
6992
else:
7093
right = mid - 1
71-
return self.win_persons[left]
94+
return self.wins[left]
95+
7296

7397
# Your TopVotedCandidate object will be instantiated and called as such:
7498
# obj = TopVotedCandidate(persons, times)
@@ -126,35 +150,35 @@ class TopVotedCandidate {
126150
class TopVotedCandidate {
127151
public:
128152
vector<int> times;
129-
vector<int> winPersons;
153+
vector<int> wins;
130154

131155
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
132-
this->times = times;
133-
int mx = -1, curWin = -1;
134156
int n = persons.size();
135-
vector<int> counter(n + 1);
136-
winPersons.resize(n);
157+
wins.resize(n);
158+
int mx = 0, cur = 0;
159+
this->times = times;
160+
vector<int> counter(n);
137161
for (int i = 0; i < n; ++i)
138162
{
139-
if (++counter[persons[i]] >= mx)
163+
int p = persons[i];
164+
if (++counter[p] >= mx)
140165
{
141-
mx = counter[persons[i]];
142-
curWin = persons[i];
166+
mx = counter[p];
167+
cur = p;
143168
}
144-
winPersons[i] = curWin;
169+
wins[i] = cur;
145170
}
146-
147171
}
148172

149173
int q(int t) {
150-
int left = 0, right = winPersons.size() - 1;
174+
int left = 0, right = wins.size() - 1;
151175
while (left < right)
152176
{
153-
int mid = (left + right + 1) >> 1;
177+
int mid = left + right + 1 >> 1;
154178
if (times[mid] <= t) left = mid;
155179
else right = mid - 1;
156180
}
157-
return winPersons[left];
181+
return wins[left];
158182
}
159183
};
160184

@@ -169,29 +193,27 @@ public:
169193
170194
```go
171195
type TopVotedCandidate struct {
172-
times []int
173-
winPersons []int
196+
times []int
197+
wins []int
174198
}
175199
176200
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)
201+
mx, cur, n := 0, 0, len(persons)
202+
counter := make([]int, n)
203+
wins := make([]int, n)
180204
for i, p := range persons {
181205
counter[p]++
182206
if counter[p] >= mx {
183207
mx = counter[p]
184-
curWin = p
208+
cur = p
185209
}
186-
winPersons[i] = curWin
187-
}
188-
return TopVotedCandidate{
189-
times, winPersons,
210+
wins[i] = cur
190211
}
212+
return TopVotedCandidate{times, wins}
191213
}
192214
193215
func (this *TopVotedCandidate) Q(t int) int {
194-
left, right := 0, len(this.winPersons)-1
216+
left, right := 0, len(this.wins)-1
195217
for left < right {
196218
mid := (left + right + 1) >> 1
197219
if this.times[mid] <= t {
@@ -200,7 +222,7 @@ func (this *TopVotedCandidate) Q(t int) int {
200222
right = mid - 1
201223
}
202224
}
203-
return this.winPersons[left]
225+
return this.wins[left]
204226
}
205227
206228
/**

0 commit comments

Comments
 (0)