Non-overlapping Intervals Solutions in Java
Number 435
Difficulty Medium
Acceptance 43.5%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// https://leetcode.com/problems/non-overlapping-intervals/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;import java.util.Comparator;/// Dynamic Programming based on starting point/// Time Complexity: O(n^2)/// Space Complexity: O(n)public class Solution1 {// Definition for an interval.public static class Interval {int start;int end;Interval() { start = 0; end = 0; }Interval(int s, int e) { start = s; end = e; }}public int eraseOverlapIntervals(Interval[] intervals) {if(intervals.length == 0)return 0;Arrays.sort(intervals, new Comparator<Interval>() {@Overridepublic int compare(Interval o1, Interval o2) {if(o1.start != o2.start)return o1.start - o2.start;return o1.end - o2.end;}});int[] memo = new int[intervals.length];Arrays.fill(memo, 1);for(int i = 1 ; i < intervals.length ; i ++)// memo[i]for(int j = 0 ; j < i ; j ++)if(intervals[i].start >= intervals[j].end)memo[i] = Math.max(memo[i], 1 + memo[j]);int res = 0;for(int i = 0; i < memo.length ; i ++)res = Math.max(res, memo[i]);return intervals.length - res;}public static void main(String[] args) {Interval[] interval1 = {new Interval(1,2),new Interval(2,3),new Interval(3,4),new Interval(1,3)};System.out.println((new Solution1()).eraseOverlapIntervals(interval1));Interval[] interval2 = {new Interval(1,2),new Interval(1,2),new Interval(1,2)};System.out.println((new Solution1()).eraseOverlapIntervals(interval2));Interval[] interval3 = {new Interval(1,2),new Interval(2,3)};System.out.println((new Solution1()).eraseOverlapIntervals(interval3));}}
Java solution by liuyubobobo/Play-Leetcode
/// https://leetcode.com/problems/non-overlapping-intervals/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;import java.util.Comparator;/// Greedy Algorithm based on starting point/// Time Complexity: O(n)/// Space Complexity: O(n)public class Solution2 {// Definition for an interval.public static class Interval {int start;int end;Interval() { start = 0; end = 0; }Interval(int s, int e) { start = s; end = e; }}public int eraseOverlapIntervals(Interval[] intervals) {if(intervals.length == 0)return 0;Arrays.sort(intervals, new Comparator<Interval>() {@Overridepublic int compare(Interval o1, Interval o2) {if(o1.start != o2.start)return o1.start - o2.start;return o1.end - o2.end;}});int res = 1;int pre = 0;for(int i = 1 ; i < intervals.length ; i ++)if(intervals[i].start >= intervals[pre].end){res ++;pre = i;}else if(intervals[i].end < intervals[pre].end)pre = i;return intervals.length - res;}public static void main(String[] args) {Interval[] interval1 = {new Interval(1,2),new Interval(2,3),new Interval(3,4),new Interval(1,3)};System.out.println((new Solution2()).eraseOverlapIntervals(interval1));Interval[] interval2 = {new Interval(1,2),new Interval(1,2),new Interval(1,2)};System.out.println((new Solution2()).eraseOverlapIntervals(interval2));Interval[] interval3 = {new Interval(1,2),new Interval(2,3)};System.out.println((new Solution2()).eraseOverlapIntervals(interval3));}}
Java solution by liuyubobobo/Play-Leetcode
/// https://leetcode.com/problems/non-overlapping-intervals/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;import java.util.Comparator;/// Dynamic Programming based on ending point/// Time Complexity: O(n^2)/// Space Complexity: O(n)public class Solution3 {// Definition for an interval.public static class Interval {int start;int end;Interval() { start = 0; end = 0; }Interval(int s, int e) { start = s; end = e; }}public int eraseOverlapIntervals(Interval[] intervals) {if(intervals.length == 0)return 0;Arrays.sort(intervals, new Comparator<Interval>() {@Overridepublic int compare(Interval o1, Interval o2) {if(o1.end != o2.end)return o1.end - o2.end;return o1.start - o2.start;}});int[] memo = new int[intervals.length];Arrays.fill(memo, 1);for(int i = 1 ; i < intervals.length ; i ++)// memo[i]for(int j = 0 ; j < i ; j ++)if(intervals[i].start >= intervals[j].end)memo[i] = Math.max(memo[i], 1 + memo[j]);int res = 0;for(int i = 0; i < memo.length ; i ++)res = Math.max(res, memo[i]);return intervals.length - res;}public static void main(String[] args) {Interval[] interval1 = {new Interval(1,2),new Interval(2,3),new Interval(3,4),new Interval(1,3)};System.out.println((new Solution3()).eraseOverlapIntervals(interval1));Interval[] interval2 = {new Interval(1,2),new Interval(1,2),new Interval(1,2)};System.out.println((new Solution3()).eraseOverlapIntervals(interval2));Interval[] interval3 = {new Interval(1,2),new Interval(2,3)};System.out.println((new Solution3()).eraseOverlapIntervals(interval3));}}
Java solution by liuyubobobo/Play-Leetcode
/// https://leetcode.com/problems/non-overlapping-intervals/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;import java.util.Comparator;/// Greedy Algorithm based on ending point/// Time Complexity: O(n)/// Space Complexity: O(n)public class Solution4 {// Definition for an interval.public static class Interval {int start;int end;Interval() { start = 0; end = 0; }Interval(int s, int e) { start = s; end = e; }}public int eraseOverlapIntervals(Interval[] intervals) {if(intervals.length == 0)return 0;Arrays.sort(intervals, new Comparator<Interval>() {@Overridepublic int compare(Interval o1, Interval o2) {if(o1.end != o2.end)return o1.end - o2.end;return o1.start - o2.start;}});int res = 1;int pre = 0;for(int i = 1 ; i < intervals.length ; i ++)if(intervals[i].start >= intervals[pre].end){res ++;pre = i;}return intervals.length - res;}public static void main(String[] args) {Interval[] interval1 = {new Interval(1,2),new Interval(2,3),new Interval(3,4),new Interval(1,3)};System.out.println((new Solution4()).eraseOverlapIntervals(interval1));Interval[] interval2 = {new Interval(1,2),new Interval(1,2),new Interval(1,2)};System.out.println((new Solution4()).eraseOverlapIntervals(interval2));Interval[] interval3 = {new Interval(1,2),new Interval(2,3)};System.out.println((new Solution4()).eraseOverlapIntervals(interval3));}}