二刷
100.00 %
sort start and end points separately
class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if (intervals == null || intervals.length == 0) {
return true;
}
int size = intervals.length;
// s s e e
int[] starts = new int[size];
int[] ends = new int[size];
for (int i = 0; i < size; i++) {
starts[i] = intervals[i].start;
ends[i] = intervals[i].end;
}
Arrays.sort(starts);
Arrays.sort(ends);
for (int i = 0; i < size; i++) {
if (i != 0 && starts[i] < ends[i - 1]) {
return false;
}
}
return true;
}
}
一刷
41.11 %
class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if (intervals == null || intervals.length <= 1) return true;
Arrays.sort(intervals, (i1, i2) -> i1.start - i2.start);
Interval prev = null;
for (Interval interval : intervals) {
if (prev == null) {
prev = interval;
continue;
}
if (interval.start < prev.end) return false;
prev = interval;
}
return true;
}
}
No comments:
Post a Comment