comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
Easy |
|
Given an array of meeting time intervals
where intervals[i] = [starti, endi]
, determine if a person could attend all meetings.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]] Output: false
Example 2:
Input: intervals = [[7,10],[2,4]] Output: true
Constraints:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti < endi <= 106
class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
intervals.sort()
return all(a[1] <= b[0] for a, b in pairwise(intervals))
class Solution {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
for (int i = 1; i < intervals.length; ++i) {
var a = intervals[i - 1];
var b = intervals[i];
if (a[1] > b[0]) {
return false;
}
}
return true;
}
}
class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
return a[0] < b[0];
});
for (int i = 1; i < intervals.size(); ++i) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false;
}
}
return true;
}
};
func canAttendMeetings(intervals [][]int) bool {
sort.Slice(intervals, func(i, j int) bool {
return intervals[i][0] < intervals[j][0]
})
for i := 1; i < len(intervals); i++ {
if intervals[i][0] < intervals[i-1][1] {
return false
}
}
return true
}
function canAttendMeetings(intervals: number[][]): boolean {
intervals.sort((a, b) => a[0] - b[0]);
for (let i = 1; i < intervals.length; ++i) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false;
}
}
return true;
}
impl Solution {
#[allow(dead_code)]
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
if intervals.len() == 1 {
return true;
}
let mut intervals = intervals;
// Sort the intervals vector
intervals.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
let mut end = -1;
// Begin traverse
for p in &intervals {
if end == -1 {
// This is the first pair
end = p[1];
continue;
}
if p[0] < end {
return false;
}
end = p[1];
}
true
}
}