Skip to content

Commit 1dea942

Browse files
committed
Add problem 3111: Minimum Rectangles to Cover Points
1 parent 418b55a commit 1dea942

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,7 @@ pub mod problem_3105_longest_strictly_increasing_or_strictly_decreasing_subarray
20922092
pub mod problem_3106_lexicographically_smallest_string_after_operations_with_constraint;
20932093
pub mod problem_3107_minimum_operations_to_make_median_of_array_equal_to_k;
20942094
pub mod problem_3110_score_of_a_string;
2095+
pub mod problem_3111_minimum_rectangles_to_cover_points;
20952096
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
20962097

20972098
#[cfg(test)]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn min_rectangles_to_cover_points(points: Vec<Vec<i32>>, w: i32) -> i32 {
7+
let mut points = points.into_iter().map(|point| point[0]).collect::<Vec<_>>();
8+
9+
points.sort_unstable();
10+
11+
let mut result = 0;
12+
let mut covered = i32::MIN;
13+
14+
for &point in &points {
15+
if point > covered {
16+
result += 1;
17+
covered = point + w;
18+
}
19+
}
20+
21+
result
22+
}
23+
}
24+
25+
// ------------------------------------------------------ snip ------------------------------------------------------ //
26+
27+
impl super::Solution for Solution {
28+
fn min_rectangles_to_cover_points(points: Vec<Vec<i32>>, w: i32) -> i32 {
29+
Self::min_rectangles_to_cover_points(points, w)
30+
}
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
#[test]
36+
fn test_solution() {
37+
super::super::tests::run::<super::Solution>();
38+
}
39+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn min_rectangles_to_cover_points(points: Vec<Vec<i32>>, w: 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+
((&[[2, 1], [1, 0], [1, 4], [1, 8], [3, 5], [4, 6]] as &[_], 1), 2),
14+
((&[[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]], 2), 3),
15+
((&[[2, 3], [1, 2]], 0), 2),
16+
];
17+
18+
for ((points, w), expected) in test_cases {
19+
assert_eq!(
20+
S::min_rectangles_to_cover_points(points.iter().map(Vec::from).collect(), w),
21+
expected,
22+
);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)