|
60 | 60 |
|
61 | 61 | <!-- 这里可写通用的实现逻辑 -->
|
62 | 62 |
|
| 63 | +**方法一:队列 + 模拟** |
| 64 | + |
| 65 | +我们创建两个队列 $qr$ 和 $qd$,分别记录天辉和夜魇阵营的参议员的下标。然后我们开始进行模拟,每一轮各从队首取出一位参议员,然后根据他的阵营进行不同的操作: |
| 66 | + |
| 67 | +- 如果天辉阵营的参议员编号小于夜魇阵营的参议员编号,那么该天辉阵营的参议员就可以将夜魇阵营的参议员票权永久取消,我们将天辉阵营的参议员的下标加 $n$ 后重新放回队尾,表示该参议员会参与下一轮的投票。 |
| 68 | +- 如果夜魇阵营的参议员编号小于天辉阵营的参议员编号,那么该夜魇阵营的参议员就可以将天辉阵营的参议员票权永久取消,我们将夜魇阵营的参议员的下标加 $n$ 后重新放回队尾,表示该参议员会参与下一轮的投票。 |
| 69 | + |
| 70 | +最后当队列中只剩一种阵营的参议员时,该阵营的参议员获胜。 |
| 71 | + |
| 72 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为参议员的数量。 |
| 73 | + |
63 | 74 | <!-- tabs:start -->
|
64 | 75 |
|
65 | 76 | ### **Python3**
|
66 | 77 |
|
67 | 78 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
68 | 79 |
|
69 | 80 | ```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" |
71 | 99 | ```
|
72 | 100 |
|
73 | 101 | ### **Java**
|
74 | 102 |
|
75 | 103 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
76 | 104 |
|
77 | 105 | ```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 | +``` |
78 | 193 |
|
| 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 | +} |
79 | 219 | ```
|
80 | 220 |
|
81 | 221 | ### **...**
|
|
0 commit comments