奈學P7-Java架構師7期
class EraseOverlapIntervals {
? ?public static void main(String[] args) {
? ? ? ?System.out.println(new EraseOverlapIntervals().eraseOverlapIntervals(new int[][]{new int[]{1, 100}, new int[]{11, 22}, new int[]{1, 11}, new int[]{2, 12}}));
? ?}
? ?public int eraseOverlapIntervals(int[][] intervals) {
? ? ? ?Arrays.sort(intervals, Comparator.comparingInt(o -> o[1]));
? ? ? ?int result = 0;
? ? ? ?int[] current = intervals[0];
? ? ? ?for (int i = 1; i < intervals.length; i++) {
? ? ? ? ? ?if (intervals[i][0] < current[1]) {
? ? ? ? ? ? ? ?// 重疊了
? ? ? ? ? ? ? ?result += 1;
? ? ? ? ? ?} else {
? ? ? ? ? ? ? ?current = intervals[i];
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?return result;
? ?}}
標簽: