Skip to content

Commit 4dcf03f

Browse files
authoredAug 1, 2023
feat: add solutions to lc problem: No.0649 (#1361)
No.0649.Dota2 Senate
1 parent 11da429 commit 4dcf03f

File tree

7 files changed

+387
-2
lines changed

7 files changed

+387
-2
lines changed
 

‎solution/0600-0699/0649.Dota2 Senate/README.md

+141-1
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,162 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:队列 + 模拟**
64+
65+
我们创建两个队列 $qr$ 和 $qd$,分别记录天辉和夜魇阵营的参议员的下标。然后我们开始进行模拟,每一轮各从队首取出一位参议员,然后根据他的阵营进行不同的操作:
66+
67+
- 如果天辉阵营的参议员编号小于夜魇阵营的参议员编号,那么该天辉阵营的参议员就可以将夜魇阵营的参议员票权永久取消,我们将天辉阵营的参议员的下标加 $n$ 后重新放回队尾,表示该参议员会参与下一轮的投票。
68+
- 如果夜魇阵营的参议员编号小于天辉阵营的参议员编号,那么该夜魇阵营的参议员就可以将天辉阵营的参议员票权永久取消,我们将夜魇阵营的参议员的下标加 $n$ 后重新放回队尾,表示该参议员会参与下一轮的投票。
69+
70+
最后当队列中只剩一种阵营的参议员时,该阵营的参议员获胜。
71+
72+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为参议员的数量。
73+
6374
<!-- tabs:start -->
6475

6576
### **Python3**
6677

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

6980
```python
70-
81+
class Solution:
82+
def predictPartyVictory(self, senate: str) -> str:
83+
qr = deque()
84+
qd = deque()
85+
for i, c in enumerate(senate):
86+
if c == "R":
87+
qr.append(i)
88+
else:
89+
qd.append(i)
90+
n = len(senate)
91+
while qr and qd:
92+
if qr[0] < qd[0]:
93+
qr.append(qr[0] + n)
94+
else:
95+
qd.append(qd[0] + n)
96+
qr.popleft()
97+
qd.popleft()
98+
return "Radiant" if qr else "Dire"
7199
```
72100

73101
### **Java**
74102

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

77105
```java
106+
class Solution {
107+
public String predictPartyVictory(String senate) {
108+
int n = senate.length();
109+
Deque<Integer> qr = new ArrayDeque<>();
110+
Deque<Integer> qd = new ArrayDeque<>();
111+
for (int i = 0; i < n; ++i) {
112+
if (senate.charAt(i) == 'R') {
113+
qr.offer(i);
114+
} else {
115+
qd.offer(i);
116+
}
117+
}
118+
while (!qr.isEmpty() && !qd.isEmpty()) {
119+
if (qr.peek() < qd.peek()) {
120+
qr.offer(qr.peek() + n);
121+
} else {
122+
qd.offer(qd.peek() + n);
123+
}
124+
qr.poll();
125+
qd.poll();
126+
}
127+
return qr.isEmpty() ? "Dire" : "Radiant";
128+
}
129+
}
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
string predictPartyVictory(string senate) {
138+
int n = senate.size();
139+
queue<int> qr;
140+
queue<int> qd;
141+
for (int i = 0; i < n; ++i) {
142+
if (senate[i] == 'R') {
143+
qr.push(i);
144+
} else {
145+
qd.push(i);
146+
}
147+
}
148+
while (!qr.empty() && !qd.empty()) {
149+
int r = qr.front();
150+
int d = qd.front();
151+
qr.pop();
152+
qd.pop();
153+
if (r < d) {
154+
qr.push(r + n);
155+
} else {
156+
qd.push(d + n);
157+
}
158+
}
159+
return qr.empty() ? "Dire" : "Radiant";
160+
}
161+
};
162+
```
163+
164+
### **Go**
165+
166+
```go
167+
func predictPartyVictory(senate string) string {
168+
n := len(senate)
169+
qr := []int{}
170+
qd := []int{}
171+
for i, c := range senate {
172+
if c == 'R' {
173+
qr = append(qr, i)
174+
} else {
175+
qd = append(qd, i)
176+
}
177+
}
178+
for len(qr) > 0 && len(qd) > 0 {
179+
r, d := qr[0], qd[0]
180+
qr, qd = qr[1:], qd[1:]
181+
if r < d {
182+
qr = append(qr, r+n)
183+
} else {
184+
qd = append(qd, d+n)
185+
}
186+
}
187+
if len(qr) > 0 {
188+
return "Radiant"
189+
}
190+
return "Dire"
191+
}
192+
```
78193

194+
### **TypeScript**
195+
196+
```ts
197+
function predictPartyVictory(senate: string): string {
198+
const n = senate.length;
199+
const qr: number[] = [];
200+
const qd: number[] = [];
201+
for (let i = 0; i < n; ++i) {
202+
if (senate[i] === 'R') {
203+
qr.push(i);
204+
} else {
205+
qd.push(i);
206+
}
207+
}
208+
while (qr.length > 0 && qd.length > 0) {
209+
const r = qr.shift()!;
210+
const d = qd.shift()!;
211+
if (r < d) {
212+
qr.push(r + n);
213+
} else {
214+
qd.push(d + n);
215+
}
216+
}
217+
return qr.length > 0 ? 'Radiant' : 'Dire';
218+
}
79219
```
80220

81221
### **...**

‎solution/0600-0699/0649.Dota2 Senate/README_EN.md

+130-1
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,142 @@ And in round 2, the third senator can just announce the victory since he is the
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def predictPartyVictory(self, senate: str) -> str:
64+
qr = deque()
65+
qd = deque()
66+
for i, c in enumerate(senate):
67+
if c == "R":
68+
qr.append(i)
69+
else:
70+
qd.append(i)
71+
n = len(senate)
72+
while qr and qd:
73+
if qr[0] < qd[0]:
74+
qr.append(qr[0] + n)
75+
else:
76+
qd.append(qd[0] + n)
77+
qr.popleft()
78+
qd.popleft()
79+
return "Radiant" if qr else "Dire"
6380
```
6481

6582
### **Java**
6683

6784
```java
85+
class Solution {
86+
public String predictPartyVictory(String senate) {
87+
int n = senate.length();
88+
Deque<Integer> qr = new ArrayDeque<>();
89+
Deque<Integer> qd = new ArrayDeque<>();
90+
for (int i = 0; i < n; ++i) {
91+
if (senate.charAt(i) == 'R') {
92+
qr.offer(i);
93+
} else {
94+
qd.offer(i);
95+
}
96+
}
97+
while (!qr.isEmpty() && !qd.isEmpty()) {
98+
if (qr.peek() < qd.peek()) {
99+
qr.offer(qr.peek() + n);
100+
} else {
101+
qd.offer(qd.peek() + n);
102+
}
103+
qr.poll();
104+
qd.poll();
105+
}
106+
return qr.isEmpty() ? "Dire" : "Radiant";
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
string predictPartyVictory(string senate) {
117+
int n = senate.size();
118+
queue<int> qr;
119+
queue<int> qd;
120+
for (int i = 0; i < n; ++i) {
121+
if (senate[i] == 'R') {
122+
qr.push(i);
123+
} else {
124+
qd.push(i);
125+
}
126+
}
127+
while (!qr.empty() && !qd.empty()) {
128+
int r = qr.front();
129+
int d = qd.front();
130+
qr.pop();
131+
qd.pop();
132+
if (r < d) {
133+
qr.push(r + n);
134+
} else {
135+
qd.push(d + n);
136+
}
137+
}
138+
return qr.empty() ? "Dire" : "Radiant";
139+
}
140+
};
141+
```
142+
143+
### **Go**
144+
145+
```go
146+
func predictPartyVictory(senate string) string {
147+
n := len(senate)
148+
qr := []int{}
149+
qd := []int{}
150+
for i, c := range senate {
151+
if c == 'R' {
152+
qr = append(qr, i)
153+
} else {
154+
qd = append(qd, i)
155+
}
156+
}
157+
for len(qr) > 0 && len(qd) > 0 {
158+
r, d := qr[0], qd[0]
159+
qr, qd = qr[1:], qd[1:]
160+
if r < d {
161+
qr = append(qr, r+n)
162+
} else {
163+
qd = append(qd, d+n)
164+
}
165+
}
166+
if len(qr) > 0 {
167+
return "Radiant"
168+
}
169+
return "Dire"
170+
}
171+
```
68172

173+
### **TypeScript**
174+
175+
```ts
176+
function predictPartyVictory(senate: string): string {
177+
const n = senate.length;
178+
const qr: number[] = [];
179+
const qd: number[] = [];
180+
for (let i = 0; i < n; ++i) {
181+
if (senate[i] === 'R') {
182+
qr.push(i);
183+
} else {
184+
qd.push(i);
185+
}
186+
}
187+
while (qr.length > 0 && qd.length > 0) {
188+
const r = qr.shift()!;
189+
const d = qd.shift()!;
190+
if (r < d) {
191+
qr.push(r + n);
192+
} else {
193+
qd.push(d + n);
194+
}
195+
}
196+
return qr.length > 0 ? 'Radiant' : 'Dire';
197+
}
69198
```
70199

71200
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
string predictPartyVictory(string senate) {
4+
int n = senate.size();
5+
queue<int> qr;
6+
queue<int> qd;
7+
for (int i = 0; i < n; ++i) {
8+
if (senate[i] == 'R') {
9+
qr.push(i);
10+
} else {
11+
qd.push(i);
12+
}
13+
}
14+
while (!qr.empty() && !qd.empty()) {
15+
int r = qr.front();
16+
int d = qd.front();
17+
qr.pop();
18+
qd.pop();
19+
if (r < d) {
20+
qr.push(r + n);
21+
} else {
22+
qd.push(d + n);
23+
}
24+
}
25+
return qr.empty() ? "Dire" : "Radiant";
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func predictPartyVictory(senate string) string {
2+
n := len(senate)
3+
qr := []int{}
4+
qd := []int{}
5+
for i, c := range senate {
6+
if c == 'R' {
7+
qr = append(qr, i)
8+
} else {
9+
qd = append(qd, i)
10+
}
11+
}
12+
for len(qr) > 0 && len(qd) > 0 {
13+
r, d := qr[0], qd[0]
14+
qr, qd = qr[1:], qd[1:]
15+
if r < d {
16+
qr = append(qr, r+n)
17+
} else {
18+
qd = append(qd, d+n)
19+
}
20+
}
21+
if len(qr) > 0 {
22+
return "Radiant"
23+
}
24+
return "Dire"
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public String predictPartyVictory(String senate) {
3+
int n = senate.length();
4+
Deque<Integer> qr = new ArrayDeque<>();
5+
Deque<Integer> qd = new ArrayDeque<>();
6+
for (int i = 0; i < n; ++i) {
7+
if (senate.charAt(i) == 'R') {
8+
qr.offer(i);
9+
} else {
10+
qd.offer(i);
11+
}
12+
}
13+
while (!qr.isEmpty() && !qd.isEmpty()) {
14+
if (qr.peek() < qd.peek()) {
15+
qr.offer(qr.peek() + n);
16+
} else {
17+
qd.offer(qd.peek() + n);
18+
}
19+
qr.poll();
20+
qd.poll();
21+
}
22+
return qr.isEmpty() ? "Dire" : "Radiant";
23+
}
24+
}

0 commit comments

Comments
 (0)