Skip to content

Commit be435e1

Browse files
committed
LC 2406. Divide Intervals Into Minimum Number of Groups (Rust)
1 parent ca54d12 commit be435e1

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ to the solution in this repository.
697697
| [2391. Minimum Amount of Time to Collect Garbage][lc2391] | 🟠 Medium | [![rust](res/rs.png)][lc2391rs] |
698698
| [2402. Meeting Rooms III][lc2402] | 🔴 Hard | [![rust](res/rs.png)][lc2402rs] |
699699
| [2405. Optimal Partition of String][lc2405] | 🟠 Medium | [![python](res/py.png)][lc2405py] [![rust](res/rs.png)][lc2405rs] |
700+
| [2406. Divide Intervals Into Minimum Number of Groups][lc2406] | 🟠 Medium | [![rust](res/rs.png)][lc2406rs] |
700701
| [2416. Sum of Prefix Scores of Strings][lc2416] | 🔴 Hard | [![rust](res/rs.png)][lc2416rs] |
701702
| [2421. Number of Good Paths][lc2421] | 🔴 Hard | [![python](res/py.png)][lc2421py] [![rust](res/rs.png)][lc2421rs] |
702703
| [2433. Find The Original Array of Prefix Xor][lc2433] | 🔴 Hard | [![rust](res/rs.png)][lc2433rs] |
@@ -2304,6 +2305,8 @@ to the solution in this repository.
23042305
[lc2405]: https://leetcode.com/problems/optimal-partition-of-string/
23052306
[lc2405py]: leetcode/optimal-partition-of-string.py
23062307
[lc2405rs]: leetcode/optimal-partition-of-string.rs
2308+
[lc2406]: https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/
2309+
[lc2406rs]: leetcode/divide-intervals-into-minimum-number-of-groups.rs
23072310
[lc2416]: https://leetcode.com/problems/sum-of-prefix-scores-of-strings/
23082311
[lc2416rs]: leetcode/sum-of-prefix-scores-of-strings.rs
23092312
[lc2421]: https://leetcode.com/problems/number-of-good-paths/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// 2406. Divide Intervals Into Minimum Number of Groups
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/
5+
//
6+
// Tags: Array - Two Pointers - Greedy - Sorting - Heap (Priority Queue) - Prefix Sum
7+
8+
use std::{cmp::Reverse, collections::BinaryHeap};
9+
10+
struct Solution;
11+
impl Solution {
12+
/// Sort the intervals to process them in order by start time. Use a heap to keep track of the
13+
/// earliest end time of any of the existing groups. For each interval, check if we can add it
14+
/// to the group at the top of the heap. This greedy solution works because we have guaranteed
15+
/// both that we have the group with the earliest end and the interval with the earliest start,
16+
/// if we cannot add it to that group, we cannot add any other later interval to that group and
17+
/// we cannot add this interval to any other groups, we need to create a new group.
18+
///
19+
/// Time complexity: O(nlog(n)) - Sorting the intervals and pushing/popping from the heap.
20+
/// Space complexity: O(n) - The sorted intervals are size n, the heap can grow to size n.
21+
///
22+
/// Runtime 37 ms Beats 100%
23+
/// Memory 9.78 MB Beats 33%
24+
pub fn min_groups(intervals: Vec<Vec<i32>>) -> i32 {
25+
let mut intervals = intervals.iter().map(|v| (v[0], v[1])).collect::<Vec<_>>();
26+
intervals.sort_unstable();
27+
let mut groups = BinaryHeap::<Reverse<i32>>::new();
28+
for (start, end) in intervals {
29+
if let Some(Reverse(group_end)) = groups.peek().copied() {
30+
if start > group_end {
31+
groups.pop();
32+
}
33+
}
34+
groups.push(Reverse(end));
35+
}
36+
groups.len() as _
37+
}
38+
}
39+
40+
// Tests.
41+
fn main() {
42+
let tests = [
43+
(vec![[5, 10], [6, 8], [1, 5], [2, 3], [1, 10]], 3),
44+
(vec![[1, 3], [5, 6], [8, 10], [11, 13]], 1),
45+
];
46+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
47+
let mut success = 0;
48+
for (i, t) in tests.iter().enumerate() {
49+
let res = Solution::min_groups(t.0.iter().map(|a| a.to_vec()).collect());
50+
if res == t.1 {
51+
success += 1;
52+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
53+
} else {
54+
println!(
55+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
56+
i, t.1, res
57+
);
58+
}
59+
}
60+
println!();
61+
if success == tests.len() {
62+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
63+
} else if success == 0 {
64+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
65+
} else {
66+
println!(
67+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
68+
tests.len() - success
69+
)
70+
}
71+
}

0 commit comments

Comments
 (0)