Skip to content

Commit

Permalink
Update p2274
Browse files Browse the repository at this point in the history
  • Loading branch information
HuaHuaY committed Jan 5, 2025
1 parent d70b4c2 commit 1e29c9d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pub struct Solution;

impl Solution {
pub fn max_consecutive(bottom: i32, top: i32, mut special: Vec<i32>) -> 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);
}
}

0 comments on commit 1e29c9d

Please sign in to comment.