Meeting Rooms: Priority Queue Not 2D Array

Jay·2022년 5월 28일
0

Grind 120

목록 보기
24/38


First Thoughts:

My Solution:

class Solution {
    public boolean canAttendMeetings(int[][] intervals) {
        for (int i=0; i<intervals.length; i++) {
            int start = intervals[i][0];
            int end = intervals[i][1];
            for (int j=i+1; j<intervals.length; j++) {
                int startSub = intervals[j][0];
                int endSub = intervals[j][1];
                if (startSub > start && startSub < end ||
                   endSub > start && endSub < end || 
                   startSub > start && endSub <end ||
                   startSub <= start && endSub >= end) return false;
            }
        }
        return true;
    }
}

Improved Time Efficiency Solution:

public boolean canAttendMeetings(int[][] intervals) {
	PriorityQueue<Integer> times = new PriorityQueue<>((a, b) => a.length()-b.length())
    for (int[] t : intervals) times.add(t);
    int[] top = times.poll();
    while(!times.isEmpty()) {
    	int[] next = times.pop();
        if (top[1] > next[0]) return false;
        top = next;
    }
    return true;
}

0개의 댓글