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 67485a0

Browse files
committed
Add more solutions
1 parent 793e06d commit 67485a0

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

‎src/leetcode/contest/biweekly_63.rs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
use std::collections::{HashMap, VecDeque};
2+
3+
// 5885. Minimum Number of Moves to Seat Everyone, Easy
4+
// https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/
5+
impl Solution1 {
6+
pub fn min_moves_to_seat(mut seats: Vec<i32>, mut students: Vec<i32>) -> i32 {
7+
seats.sort_unstable();
8+
students.sort_unstable();
9+
10+
let mut moves = 0;
11+
for i in 0..seats.len() {
12+
moves += i32::abs(seats[i] - students[i]);
13+
}
14+
15+
moves
16+
}
17+
}
18+
19+
// 5886. Remove Colored Pieces if Both Neighbors are the Same Color, Medium
20+
// https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/
21+
impl Solution2 {
22+
pub fn winner_of_game(colors: String) -> bool {
23+
let mut count_aaa = 0;
24+
let mut count_bbb = 0;
25+
26+
let chars = colors.chars().collect::<Vec<char>>();
27+
let mut i = 1;
28+
let mut prev = chars[0];
29+
for j in 1..chars.len() {
30+
if chars[j] == prev {
31+
i += 1;
32+
if i == 3 && prev == 'A' {
33+
count_aaa += 1;
34+
i = 2;
35+
} else if i == 3 && prev == 'B' {
36+
count_bbb += 1;
37+
i = 2;
38+
}
39+
} else {
40+
prev = chars[j];
41+
i = 1;
42+
}
43+
}
44+
45+
return count_aaa > count_bbb;
46+
}
47+
}
48+
49+
// 5888. The Time When the Network Becomes Idle, Medium
50+
// https://leetcode.com/problems/the-time-when-the-network-becomes-idle/
51+
impl Solution3 {
52+
pub fn network_becomes_idle(edges: Vec<Vec<i32>>, patience: Vec<i32>) -> i32 {
53+
let n = patience.len();
54+
let mut dist = vec![std::i32::MAX; n];
55+
let mut queue = VecDeque::new();
56+
let mut visited = vec![false; n];
57+
58+
let mut graph: HashMap<usize, Vec<usize>> = HashMap::new();
59+
for i in 0..n {
60+
graph.insert(i, vec![]);
61+
}
62+
for edge in edges {
63+
let u = edge[0] as usize;
64+
let v = edge[1] as usize;
65+
graph.get_mut(&u).unwrap().push(v);
66+
graph.get_mut(&v).unwrap().push(u);
67+
}
68+
69+
dist[0] = 0;
70+
queue.push_back(0);
71+
72+
while !queue.is_empty() {
73+
let u = queue.pop_front().unwrap();
74+
visited[u] = true;
75+
76+
for &v in graph[&u].iter() {
77+
let v = v as usize;
78+
if dist[v] > dist[u] + patience[u] {
79+
dist[v] = dist[u] + 1;
80+
if !visited[v] {
81+
queue.push_back(v);
82+
}
83+
}
84+
}
85+
}
86+
87+
let dist = dist.iter().map(|&f| f as f32 * 2.0).collect::<Vec<f32>>();
88+
let patience = patience.iter().map(|&f| f as f32).collect::<Vec<f32>>();
89+
90+
let mut ans = 0.0;
91+
for i in 1..n {
92+
let left = f32::ceil(dist[i] / patience[i]) - 1.0;
93+
let remain_start = dist[i] - left * patience[i];
94+
ans = f32::max(ans, dist[i] - remain_start + dist[i]);
95+
}
96+
97+
ans as i32 + 1
98+
}
99+
}
100+
101+
struct Solution1 {}
102+
struct Solution2 {}
103+
struct Solution3 {}
104+
105+
#[cfg(test)]
106+
mod tests {
107+
use super::*;
108+
use crate::{vec_string, vec_vec_i32};
109+
110+
#[test]
111+
fn test_min_moves_to_seat() {
112+
assert_eq!(Solution1::min_moves_to_seat(vec![3, 1, 5], vec![2, 7, 4]), 4);
113+
}
114+
115+
#[test]
116+
fn test_min_moves_to_seat2() {
117+
assert_eq!(Solution1::min_moves_to_seat(vec![4, 1, 5, 9], vec![1, 3, 2, 6]), 7);
118+
}
119+
120+
#[test]
121+
fn test_min_moves_to_seat3() {
122+
assert_eq!(Solution1::min_moves_to_seat(vec![2, 2, 6, 6], vec![1, 3, 2, 6]), 4);
123+
}
124+
125+
#[test]
126+
fn test_winner_of_game() {
127+
assert_eq!(Solution2::winner_of_game("AAABABB".to_string()), true);
128+
}
129+
130+
#[test]
131+
fn test_winner_of_game2() {
132+
assert_eq!(Solution2::winner_of_game("AA".to_string()), false);
133+
}
134+
135+
#[test]
136+
fn test_winner_of_game3() {
137+
assert_eq!(Solution2::winner_of_game("ABBBBBBBAAA".to_string()), false);
138+
}
139+
140+
#[test]
141+
fn test_winner_of_game4() {
142+
assert_eq!(Solution2::winner_of_game("BBBAAAABB".to_string()), true);
143+
}
144+
145+
#[test]
146+
fn test_network_becomes_idle() {
147+
assert_eq!(Solution3::network_becomes_idle(vec_vec_i32![[0, 1], [1, 2]], vec![0, 2, 1]), 8);
148+
}
149+
150+
#[test]
151+
fn test_network_becomes_idle2() {
152+
assert_eq!(Solution3::network_becomes_idle(vec_vec_i32![[0, 1], [0, 2], [1, 2]], vec![0, 10, 10]), 3);
153+
}
154+
155+
#[test]
156+
fn test_network_becomes_idle3() {
157+
assert_eq!(
158+
Solution3::network_becomes_idle(
159+
vec_vec_i32![
160+
[3, 8],
161+
[4, 13],
162+
[0, 7],
163+
[0, 4],
164+
[1, 8],
165+
[14, 1],
166+
[7, 2],
167+
[13, 10],
168+
[9, 11],
169+
[12, 14],
170+
[0, 6],
171+
[2, 12],
172+
[11, 5],
173+
[6, 9],
174+
[10, 3]
175+
],
176+
vec![0, 3, 2, 1, 5, 1, 5, 5, 3, 1, 2, 2, 2, 2, 1]
177+
),
178+
20
179+
);
180+
}
181+
}

‎src/leetcode/contest/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
mod biweekly_62;
2+
mod biweekly_63;
23
mod weekly_261;
34
mod weekly_262;

0 commit comments

Comments
(0)

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