|
| 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