作者:
yam276 ('_')
2025-06-12 18:25:00649. Dota2 Senate
題目:
兩黨人有行動順序搞政治鬥爭
每次行動會大罷免掉敵對黨最快行動的人
求最後剩下哪一黨的人
思路:
幫他們各自照 index 排序
每次派一個最快的生死鬥
贏的人回去最後排隊
Code:
use std::collections::VecDeque;
impl Solution {
pub fn predict_party_victory(senate: String) -> String {
let mut radiant = VecDeque::new();
let mut dire = VecDeque::new();
for (i, c) in senate.chars().enumerate() {
if c == 'R' {
radiant.push_back(i);
} else if c == 'D' {
dire.push_back(i);
}
}
let n = senate.len();
while !radiant.is_empty() && !dire.is_empty() {
if let (Some(r1), Some(d1)) =
(radiant.pop_front(), dire.pop_front()) {
if r1 < d1 {
radiant.push_back(r1 + n);
} else {
dire.push_back(d1 + n);
}
}
}
match radiant.is_empty() {
true => "Dire".to_string(),
false => "Radiant".to_string(),
}
}
}