Skip to content

Commit

Permalink
Update p732
Browse files Browse the repository at this point in the history
  • Loading branch information
HuaHuaY committed Jan 4, 2025
1 parent 59187f8 commit 5e5e352
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ Call test by running `cargo test p[xxx]_`.
- [3159. Find Occurrences of an Element in an Array](src/leetcode/p3159_find_occurrences_of_an_element_in_an_array)
- [3218. Minimum Cost for Cutting Cake I](src/leetcode/p3218_minimum_cost_for_cutting_cake_i)

### Hard(66)
### Hard(67)

- [4. Median of Two Sorted Arrays](src/leetcode/p4_median_of_two_sorted_arrays)
- [10. Regular Expression Matching](src/leetcode/p10_regular_expression_matching)
Expand Down Expand Up @@ -348,6 +348,7 @@ Call test by running `cargo test p[xxx]_`.
- [466. Count The Repetitions](src/leetcode/p466_count_the_repetitions)
- [514. Freedom Trail](src/leetcode/p514_freedom_trail)
- [600. Non-negative Integers without Consecutive Ones](src/leetcode/p600_non_negative_integers_without_consecutive_ones)
- [732. My Calendar III](src/leetcode/p732_my_calendar_iii)
- [871. Minimum Number of Refueling Stops](src/leetcode/p871_minimum_number_of_refueling_stops)
- [902. Numbers At Most N Given Digit Set](src/leetcode/p902_numbers_at_most_n_given_digit_set)
- [982. Triples with Bitwise AND Equal To Zero](src/leetcode/p982_triples_with_bitwise_and_equal_to_zero)
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ pub mod p71_simplify_path;
pub mod p729_my_calendar_i;
pub mod p72_edit_distance;
pub mod p731_my_calendar_ii;
pub mod p732_my_calendar_iii;
pub mod p73_set_matrix_zeroes;
pub mod p740_delete_and_earn;
pub mod p746_min_cost_climbing;
Expand Down
66 changes: 66 additions & 0 deletions src/leetcode/p732_my_calendar_iii/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::collections::BTreeMap;

#[allow(dead_code)]
struct MyCalendarThree {
map: BTreeMap<i32, (i32, i32)>,
}

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
#[allow(dead_code)]
impl MyCalendarThree {
fn new() -> Self {
Self {
map: BTreeMap::new(),
}
}

fn update(&mut self, start: i32, end: i32, value: i32, left: i32, right: i32, idx: i32) {
if end < left || start > right {
return;
}
self.map.entry(idx).or_default();
if start <= left && right <= end {
self.map.entry(idx).and_modify(|(a, b)| {
*a += value;
*b += value;
});
} else {
let mid = (right - left) / 2 + left;
self.update(start, end, value, left, mid, idx * 2);
self.update(start, end, value, mid + 1, right, idx * 2 + 1);
let l = self.map.entry(idx * 2).or_default().0;
let r = self.map.entry(idx * 2 + 1).or_default().0;
self.map.entry(idx).and_modify(|(a, b)| *a = *b + l.max(r));
}
}

fn book(&mut self, start_time: i32, end_time: i32) -> i32 {
self.update(start_time, end_time - 1, 1, 0, 1_000_000_000, 1);
self.map.get(&1).unwrap().0
}
}

/**
* Your MyCalendarTwo object will be instantiated and called as such:
* let obj = MyCalendarTwo::new();
* let ret_1: bool = obj.book(startTime, endTime);
*/

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test1() {
let mut my_calendar = MyCalendarThree::new();
assert_eq!(my_calendar.book(10, 20), 1);
assert_eq!(my_calendar.book(50, 60), 1);
assert_eq!(my_calendar.book(10, 40), 2);
assert_eq!(my_calendar.book(5, 15), 3);
assert_eq!(my_calendar.book(5, 10), 3);
assert_eq!(my_calendar.book(25, 55), 3);
}
}

0 comments on commit 5e5e352

Please sign in to comment.