Created
March 24, 2020 18:48
-
-
Save wushbin/b81170a7406e39c1ff08a39dd5bf539c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int removeCoveredIntervals(int[][] intervals) { | |
| Arrays.sort(intervals, (a, b) -> (a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(b[0], a[0]))); | |
| int right = -1; | |
| int count = 0; | |
| for (int[] inter : intervals) { | |
| if (inter[1] > right) { | |
| count ++; | |
| right = inter[1]; | |
| } | |
| } | |
| return count; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int removeCoveredIntervals(int[][] intervals) { | |
| Arrays.sort(intervals, (a, b) -> (Integer.compare(a[0], b[0]))); | |
| int left = -1; | |
| int right = -1; | |
| int count = 0; | |
| for (int[] inter : intervals) { | |
| if (inter[0] > left && inter[1] > right) { | |
| left = inter[0]; | |
| count += 1; | |
| } | |
| right = Math.max(right, inter[1]); | |
| } | |
| return count; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| class Point implements Comparable<Point>{ | |
| int[] interval; | |
| int flag; | |
| public Point(int[] interval, int flag) { | |
| this.interval = interval; | |
| this.flag = flag; | |
| } | |
| @Override | |
| public int compareTo(Point other) { | |
| if (this.interval[this.flag] != other.interval[other.flag]) { | |
| return Integer.compare(this.interval[this.flag], other.interval[other.flag]); | |
| } else { | |
| return Integer.compare(other.interval[1- other.flag], this.interval[1- this.flag]); | |
| } | |
| } | |
| } | |
| public int removeCoveredIntervals(int[][] intervals) { | |
| List<Point> pList = new ArrayList<>(); | |
| for (int[] inter : intervals) { | |
| pList.add(new Point(inter, 0)); // start | |
| pList.add(new Point(inter, 1)); // end | |
| } | |
| Collections.sort(pList); | |
| Deque<Point> deque = new ArrayDeque<>(); | |
| int n = intervals.length; | |
| for (Point p : pList) { | |
| if (p.flag == 0) { // start | |
| deque.addLast(p); | |
| } else { // end | |
| Point first = deque.peekFirst(); | |
| if (p.interval[0] == first.interval[0] && p.interval[1] == first.interval[1]) { | |
| while(!deque.isEmpty() && deque.peekFirst().interval[1] <= p.interval[1]) { | |
| Point polled = deque.pollFirst(); | |
| } | |
| } else if (p.interval[0] >= first.interval[0] && p.interval[1] <= first.interval[1]) { | |
| n -= 1; | |
| } else { | |
| // do nothing | |
| } | |
| } | |
| } | |
| return n; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment