Java, Concise, easy understand, override comparator and using bit map


  • 0
    B
    public class Solution {
    public boolean canAttendMeetings(Interval[] intervals) {
        if(intervals == null || intervals.length == 0) {
        	return true;
        }
        IntervalCom cmp = new IntervalCom();
        Arrays.sort(intervals, cmp);
        for(int i = 0; i < intervals.length - 1; i++) {
        	if(intervals[i].end > intervals[i + 1].start) {
        		return false;
        	}
        }
        return true;
    }
    private class IntervalCom implements Comparator<Interval> {
        public int compare(Interval o1, Interval o2) {
            if(o1.start > o2.start) {
                return 1;
            }
            else if(o1.start < o2.start) {
                return -1;
            }
            else {
                return 0;
            }
        }
    }
    

    }

    public class Solution {
    public boolean canAttendMeetings(Interval[] intervals) {
        if(intervals == null || intervals.length == 0) {
            return true;
        }
        int max = Integer.MIN_VALUE;
        for(int i = 0; i < intervals.length; i++) {
            max = Math.max(max, intervals[i].end);
        }
        int[] bit = new int[max + 1];
        for(int i = 0; i < intervals.length; i++) {
            for(int j = intervals[i].start; j < intervals[i].end; j++) {
                if(bit[j] != 0) {
                    return false;
                }
                bit[j] = 1;
            }
        }
        return true;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.