class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
}
};
252. 会议室
给定一个会议时间安排的数组 intervals
,每个会议时间都会包括开始和结束的时间 intervals[i] = [starti, endi]
,请你判断一个人是否能够参加这里面的全部会议。
示例 1:
输入:intervals = [[0,30],[5,10],[15,20]] 输出:false
示例 2:
输入:intervals = [[7,10],[2,4]] 输出:true
提示:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti < endi <= 106
原站题解
golang 解法, 执行用时: 8 ms, 内存消耗: 4.1 MB, 提交时间: 2023-10-15 19:11:56
//其实就是判断两个区间是否相交,如果相交直接返回true func canAttendMeetings(intervals [][]int) bool { //对intervals进行排序:按照起点进行升序排序 sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] }) //之后遍历我们的intervals for i := 1; i < len(intervals); i++ { //如果当前区间与上一区间起点相同,或者当前区间的起点小于上一区间的末尾,直接返回false if intervals[i][0] == intervals[i-1][0] || intervals[i][0] < intervals[i-1][1]{ return false } } return true }
python3 解法, 执行用时: 48 ms, 内存消耗: 18.3 MB, 提交时间: 2023-10-15 19:11:14
class Solution: def canAttendMeetings1(self, intervals: List[List[int]]) -> bool: def overlap(interval1: List[int], interval2: List[int]) -> bool: return (interval1[0] >= interval2[0] and interval1[0] < interval2[1] or interval2[0] >= interval1[0] and interval2[0] < interval1[1]) for i in range(len(intervals)): for j in range(i + 1, len(intervals)): if overlap(intervals[i], intervals[j]): return False return True # 排序 def canAttendMeetings(self, intervals: List[List[int]]) -> bool: intervals.sort() for i in range(len(intervals) - 1): if intervals[i][1] > intervals[i + 1][0]: return False return True
java 解法, 执行用时: 4 ms, 内存消耗: 43.4 MB, 提交时间: 2023-10-15 19:10:42
class Solution { // 暴力 public boolean canAttendMeetings1(int[][] intervals) { for (int i = 0; i < intervals.length; i++) { for (int j = i + 1; j < intervals.length; j++) { if (overlap(intervals[i], intervals[j])) { return false; } } } return true; } private boolean overlap(int[] interval1, int[] interval2) { return (interval1[0] >= interval2[0] && interval1[0] < interval2[1]) || (interval2[0] >= interval1[0] && interval2[0] < interval1[1]); } // 排序 public boolean canAttendMeetings(int[][] intervals) { Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); for (int i = 0; i < intervals.length - 1; i++) { if (intervals[i][1] > intervals[i + 1][0]) { return false; } } return true; } }
cpp 解法, 执行用时: 12 ms, 内存消耗: 11.7 MB, 提交时间: 2023-10-15 19:10:10
class Solution { public: bool overlap(vector<int>& interval1, vector<int>& interval2) { return interval1[0] >= interval2[0] and interval1[0] < interval2[1] or interval2[0] >= interval1[0] and interval2[0] < interval1[1]; } // 暴力 bool canAttendMeetings1(vector<vector<int>>& intervals) { for (size_t i = 0; i < intervals.size(); i++) { for (size_t j = i + 1; j < intervals.size(); j++) { if (overlap(intervals[i], intervals[j])) { return false; } } } return true; } // 排序 bool canAttendMeetings(vector<vector<int>>& intervals) { if (intervals.empty()) { return true; } // 注意:C++ 排序函数会自动首先按第一个元素对向量排序,然后按第二个元素排序,依此类推。 sort(intervals.begin(), intervals.end()); for (size_t i = 0; i < intervals.size() - 1; i++) { if (intervals[i][1] > intervals[i + 1][0]) { return false; } } return true; } };