返回介绍

solution / 0200-0299 / 0253.Meeting Rooms II / README_EN

发布于 2024-06-17 01:04:02 字数 3774 浏览 0 评论 0 收藏 0

253. Meeting Rooms II

中文文档

Description

Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return _the minimum number of conference rooms required_.

 

Example 1:

Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2

Example 2:

Input: intervals = [[7,10],[2,4]]
Output: 1

 

Constraints:

  • 1 <= intervals.length <= 104
  • 0 <= starti < endi <= 106

Solutions

Solution 1

class Solution:
  def minMeetingRooms(self, intervals: List[List[int]]) -> int:
    delta = [0] * 1000010
    for start, end in intervals:
      delta[start] += 1
      delta[end] -= 1
    return max(accumulate(delta))
class Solution {
  public int minMeetingRooms(int[][] intervals) {
    int n = 1000010;
    int[] delta = new int[n];
    for (int[] e : intervals) {
      ++delta[e[0]];
      --delta[e[1]];
    }
    int res = delta[0];
    for (int i = 1; i < n; ++i) {
      delta[i] += delta[i - 1];
      res = Math.max(res, delta[i]);
    }
    return res;
  }
}
class Solution {
public:
  int minMeetingRooms(vector<vector<int>>& intervals) {
    int n = 1000010;
    vector<int> delta(n);
    for (auto e : intervals) {
      ++delta[e[0]];
      --delta[e[1]];
    }
    for (int i = 0; i < n - 1; ++i) {
      delta[i + 1] += delta[i];
    }
    return *max_element(delta.begin(), delta.end());
  }
};
func minMeetingRooms(intervals [][]int) int {
  n := 1000010
  delta := make([]int, n)
  for _, e := range intervals {
    delta[e[0]]++
    delta[e[1]]--
  }
  for i := 1; i < n; i++ {
    delta[i] += delta[i-1]
  }
  return slices.Max(delta)
}
use std::{ collections::BinaryHeap, cmp::Reverse };

impl Solution {
  #[allow(dead_code)]
  pub fn min_meeting_rooms(intervals: Vec<Vec<i32>>) -> i32 {
    // The min heap that stores the earliest ending time among all meeting rooms
    let mut pq = BinaryHeap::new();
    let mut intervals = intervals;
    let n = intervals.len();

    // Let's first sort the intervals vector
    intervals.sort_by(|lhs, rhs| { lhs[0].cmp(&rhs[0]) });

    // Push the first end time to the heap
    pq.push(Reverse(intervals[0][1]));

    // Traverse the intervals vector
    for i in 1..n {
      // Get the current top element from the heap
      if let Some(Reverse(end_time)) = pq.pop() {
        if end_time <= intervals[i][0] {
          // If the end time is early than the current begin time
          let new_end_time = intervals[i][1];
          pq.push(Reverse(new_end_time));
        } else {
          // Otherwise, push the end time back and we also need a new room
          pq.push(Reverse(end_time));
          pq.push(Reverse(intervals[i][1]));
        }
      }
    }

    pq.len() as i32
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文