From cae5cd63b0a9e6c9262e322cf035c4fdefb98cac Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 15 Sep 2024 14:29:35 +0800 Subject: [PATCH] 20240915 finished. --- src/problem/mod.rs | 3 +- .../p2848_points_that_intersect_with_cars.rs | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2848_points_that_intersect_with_cars.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 0008c50..389f69b 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -235,4 +235,5 @@ mod p2552_count_increasing_quadruplets; mod p2555_maximize_win_from_two_segments; mod p2576_find_the_maximum_number_of_marked_indices; mod p2398_maximum_number_of_robots_within_budget; -mod p2390_removing_stars_from_a_string; \ No newline at end of file +mod p2390_removing_stars_from_a_string; +mod p2848_points_that_intersect_with_cars; \ No newline at end of file diff --git a/src/problem/p2848_points_that_intersect_with_cars.rs b/src/problem/p2848_points_that_intersect_with_cars.rs new file mode 100644 index 0000000..973b716 --- /dev/null +++ b/src/problem/p2848_points_that_intersect_with_cars.rs @@ -0,0 +1,43 @@ +/** + * [2848] Points That Intersect With Cars + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn number_of_points(nums: Vec>) -> i32 { + let mut nums: Vec<(i32, i32)> = nums.iter().map(|i| (i[0], i[1])).collect(); + nums.sort(); + + let mut result = 0; + let mut last_end = 0; + + for (start, end) in nums { + if last_end < start { + result += (end - start) + 1; + last_end = end; + } else if end > last_end { + result += (end - last_end); + last_end = end; + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2848() { + assert_eq!(7, Solution::number_of_points(vec![vec![3, 6], vec![1, 5], vec![4, 7]])); + assert_eq!(7, Solution::number_of_points(vec![vec![1, 3], vec![5, 8]])); + assert_eq!(8, Solution::number_of_points(vec![vec![4, 4], vec![9, 10], vec![9, 10], vec![3, 8]])); + } +}