Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by p1602638 on 1/16/17.
- */
- public class Interval {
- private int low;
- private int high;
- public Interval(int l, int h){
- this.low = l;
- this.high = h;
- }
- @Override
- public String toString(){
- return "[" + low + "," + high + "]";
- }
- public Interval intersect(Interval other){
- if(this.high < other.low || this.low > other.high)
- return null;
- else
- return new Interval(other.low, this.high);
- }
- public Interval hull(Interval other){
- if(this == null || other == null)
- return this == null ? other : this;
- return this.low < other.low ? (this.high > other.high) ? this : new Interval(this.low, other.high) : (this.high > other.high) ? new Interval(other.low, this.high): new Interval(other.low, other.high);
- }
- public boolean isValid(){
- return this.low <= this.high;
- }
- }
- import java.util.ArrayList;
- /**
- * Created by p1602638 on 1/16/17.
- */
- public class IntervalUnion {
- ArrayList<Interval> list;
- public IntervalUnion(){
- list = new ArrayList<>();
- }
- @Override
- public String toString(){
- String out = "[";
- for (int i = 0; i < list.size(); i++) {
- if(i != 0)
- out += ",";
- out += list.get(i).toString() + "";
- }
- out += "]";
- return out;
- }
- public void addInterval(Interval i){
- if(list.size() == 0){
- list.add(i);
- return;
- }
- for (int j = 0; j < list.size(); j++) {
- if(!(list.get(j).intersect(i) == null)) {
- list.add(list.get(j).hull(i));
- list.remove(j);
- return;
- }
- }
- list.add(i);
- }
- public void add(IntervalUnion iu){
- for (int i = 0; i < iu.list.size(); i++) {
- this.addInterval(iu.list.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement