Find the maximum number of non-overlapping intervals
s= [[0,2], [1,4], [3,5]]
the result is 2
2. Implementation
dp[i] represents the max number of non-overlapping intervals for 0~ i with i selected
non-overlapping => a.end =< b.start && b.end > b.start
collections.sort(intervals, new EndComparator(){ @ Override public int compare (interval a, interval b) { return a.end - b.end; } }); int[] dp = new int[intervals.size()]; int max = 0; for (int i = 1 ; i < intervals.size(); i ++ ) { for ( int j = 1-1; j >=0 ; j--) { if (intervals[j].end <= intervals[i].start) dp[i] = Math.max( dp[j]+1, dp[i] ); } max = Math.max(max, dp[i]); } return max;3. Similar Ones
http://sidbai.github.io/2015/07/07/Maximum-Non-overlapping-Intervals/
No comments:
Post a Comment