diff --git a/README.md b/README.md index 40b937e..cb915ca 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ Call test by running `cargo test p[xxx]_`. - [3162. Find the Number of Good Pairs I](src/leetcode/p3162_find_the_number_of_good_pairs_i) - [3280. Convert Date to Binary](src/leetcode/p3280_convert_date_to_binary) -### Medium(207) +### Medium(208) - [2. Add Two Numbers](src/leetcode/p2_add_two_numbers) - [3. Longest Substring Without Repeating Characters](src/leetcode/p3_longest_substring_without_repeating_characters) @@ -289,6 +289,7 @@ Call test by running `cargo test p[xxx]_`. - [2187. Minimum Time to Complete Trips](src/leetcode/p2187_minimum_time_to_complete_trips) - [2192. All Ancestors of a Node in a Directed Acyclic Graph](src/leetcode/p2192_all_ancestors_of_a_node_in_a_directed_acyclic_graph) - [2241. Design an ATM Machine](src/leetcode/p2241_design_an_atm_machine) +- [2274. Maximum Consecutive Floors Without Special Floors](src/leetcode/p2274_maximum_consecutive_floors_without_special_floors) - [2368. Reachable Nodes With Restrictions](src/leetcode/p2368_reachable_nodes_with_restrictions) - [2369. Check if There is a Valid Partition For The Array](src/leetcode/p2369_check_if_there_is_a_valid_partition_for_the_array) - [2380. Time Needed to Rearrange a Binary String](src/leetcode/p2380_time_needed_to_rearrange_a_binary_string) diff --git a/src/leetcode/mod.rs b/src/leetcode/mod.rs index 687f505..2c7fc07 100644 --- a/src/leetcode/mod.rs +++ b/src/leetcode/mod.rs @@ -142,6 +142,7 @@ pub mod p221_maximal_square; pub mod p2241_design_an_atm_machine; pub mod p224_basic_calculator; pub mod p225_implement_stack_using_queues; +pub mod p2274_maximum_consecutive_floors_without_special_floors; pub mod p227_basic_calculator_ii; pub mod p2293_min_max_game; pub mod p2299_strong_password_checker_ii; diff --git a/src/leetcode/p2274_maximum_consecutive_floors_without_special_floors/mod.rs b/src/leetcode/p2274_maximum_consecutive_floors_without_special_floors/mod.rs new file mode 100644 index 0000000..243d266 --- /dev/null +++ b/src/leetcode/p2274_maximum_consecutive_floors_without_special_floors/mod.rs @@ -0,0 +1,32 @@ +pub struct Solution; + +impl Solution { + pub fn max_consecutive(bottom: i32, top: i32, mut special: Vec) -> i32 { + special.sort_unstable(); + special + .into_iter() + .chain(std::iter::once(top + 1)) + .fold((bottom - 1, 0), |(pre, max), i| (i, max.max(i - pre - 1))) + .1 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test1() { + assert_eq!(Solution::max_consecutive(2, 9, [4, 6].to_vec()), 3); + } + + #[test] + fn test2() { + assert_eq!(Solution::max_consecutive(6, 8, [7, 6, 8].to_vec()), 0); + } + + #[test] + fn test3() { + assert_eq!(Solution::max_consecutive(3, 15, [7, 9, 13].to_vec()), 4); + } +}