Skip to content

Commit 338a690

Browse files
committed
Add problem 3175: Find The First Player to win K Games in a Row
1 parent 1388203 commit 338a690

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,7 @@ pub mod problem_3168_minimum_number_of_chairs_in_a_waiting_room;
21212121
pub mod problem_3169_count_days_without_meetings;
21222122
pub mod problem_3170_lexicographically_minimum_string_after_removing_stars;
21232123
pub mod problem_3174_clear_digits;
2124+
pub mod problem_3175_find_the_first_player_to_win_k_games_in_a_row;
21242125
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21252126

21262127
#[cfg(test)]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn find_winning_player(skills: Vec<i32>, k: i32) -> i32 {
7+
let skills = skills.into_iter().map(i32::cast_unsigned).collect::<Vec<_>>();
8+
let mut iter = skills.iter().copied();
9+
let k = k.cast_unsigned();
10+
let mut best_index = 0;
11+
let mut best_skill = iter.next().unwrap();
12+
let mut best_wins = 0;
13+
14+
for (index, skill) in (1..).zip(iter) {
15+
if skill > best_skill {
16+
best_index = index;
17+
best_skill = skill;
18+
best_wins = 1;
19+
} else {
20+
best_wins += 1;
21+
}
22+
23+
if best_wins >= k {
24+
break;
25+
}
26+
}
27+
28+
best_index
29+
}
30+
}
31+
32+
// ------------------------------------------------------ snip ------------------------------------------------------ //
33+
34+
impl super::Solution for Solution {
35+
fn find_winning_player(skills: Vec<i32>, k: i32) -> i32 {
36+
Self::find_winning_player(skills, k)
37+
}
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
#[test]
43+
fn test_solution() {
44+
super::super::tests::run::<super::Solution>();
45+
}
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn find_winning_player(skills: Vec<i32>, k: i32) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
((&[4, 2, 6, 3, 9] as &[_], 2), 2),
14+
((&[2, 5, 4], 3), 1),
15+
((&[11, 9, 12, 2, 20, 1, 8], 3), 4),
16+
];
17+
18+
for ((skills, k), expected) in test_cases {
19+
assert_eq!(S::find_winning_player(skills.to_vec(), k), expected);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)