import java.util.*;
class Interval {
public int start;
public int end;
public int cost;
public Interval(int start, int end, int cost){
this.start = start;
this.end = end;
this.cost = cost;
}
}
public class BestCombinationFinder {
public int getBestCombination(List intervals) {
if (intervals == null || intervals.size() == 0) {
return 0;
}
Collections.sort(intervals, new Comparator() {
public int compare(Interval i1, Interval i2) {
if (i1.end i2.end) {
return 1;
}
return 0;
}
});
return findBestCombination(intervals);
}
private int findBestCombination(List intervals) {
int[] dp = new int[intervals.size() + 1];
for (int i = 1; i intervals, int target, int left, int right) {
if (left > right) {
return right;
}
else {
int mid = (left + right) / 2;
if (intervals.get(mid).end == target) {
return mid;
}
else if (intervals.get(mid).end > target) {
return find(intervals, target, left, mid - 1);
}
else {
return find(intervals, target, mid + 1, right);
}
}
}
}
import java.util.*;
class Interval {
public int start;
public int end;
public int cost;
public Interval(int start, int end, int cost){
this.start = start;
this.end = end;
this.cost = cost;
}
}
public class BestCombinationFinder {
public int getBestCombination(List<Interval> intervals) {
if (intervals == null || intervals.size() == 0) {
return 0;
}
Collections.sort(intervals, new Comparator<Interval>() {
public int compare(Interval i1, Interval i2) {
if (i1.end < i2.end) {
return -1;
}
else if (i1.end > i2.end) {
return 1;
}
return 0;
}
});
return findBestCombination(intervals);
}
private int findBestCombination(List<Interval> intervals) {
int[] dp = new int[intervals.size() + 1];
for (int i = 1; i <= intervals.size(); i++) {
Interval currInt = intervals.get(i - 1);
int pIndex = find(intervals, currInt.start, 0, intervals.size() - 1);
dp[i] = Math.max(dp[pIndex+1] + currInt.cost, dp[i - 1]);
}
return dp[intervals.size()];
}
private int find(List<Interval> intervals, int target, int left, int right) {
if (left > right) {
return right;
}
else {
int mid = (left + right) / 2;
if (intervals.get(mid).end == target) {
return mid;
}
else if (intervals.get(mid).end > target) {
return find(intervals, target, left, mid - 1);
}
else {
return find(intervals, target, mid + 1, right);
}
}
}
}