public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {

    ArrayList<Interval> result = new ArrayList<Interval>();

    int p = 0;
    while (p < intervals.size()) {
      if (intervals.get(p).end < newInterval.start) {
        result.add(intervals.get(p));
        p++;
      } else {
        newInterval.start = Math.min(newInterval.start, intervals.get(p).start);
        break;
      }
    }

    result.add(newInterval);

    while (p < intervals.size()) {
      if (intervals.get(p).start > newInterval.end) {
        result.add(intervals.get(p));
      } else if (intervals.get(p).end >= newInterval.end
          && intervals.get(p).start <= newInterval.end) {
        newInterval.end = Math.max(newInterval.end, intervals.get(p).end);
      }
      p++;
    }

    return result;
  }
Ejemplo n.º 2
0
  public void addNum(int val) {
    if (hash.contains(val)) return;
    hash.add(val);

    Interval tmp = new Interval(val, val);

    Interval greater = set.ceiling(tmp);
    Interval smaller = set.floor(tmp);

    if (greater != null) {
      // System.out.println("greater:"+greater.start+" "+greater.end+" val:"+val);
      if (greater.start == val + 1) {
        greater.start = val;
        merge(smaller, greater);

        return;
      }
    }
    if (smaller != null) {
      // System.out.println("smaller:"+smaller.start+" "+smaller.end+" val:"+val);
      if (smaller.end == val - 1) {
        smaller.end = val;
        merge(smaller, greater);
        return;
      }
    }
    set.add(tmp);
    // for(Interval I : set){
    //     System.out.print(I.start+" "+I.end+" ");
    // }
    // System.out.println();
  }
Ejemplo n.º 3
0
 public ArrayList<Interval> insert(ArrayList<Interval> inter, Interval newIn) {
   // Start typing your Java solution below
   // DO NOT write main() function
   ArrayList<Interval> a = new ArrayList<Interval>();
   boolean flag = false;
   for (Interval in : inter) {
     if (a.size() > 0 && in.start <= a.get(a.size() - 1).end) {
       Interval tail = a.remove(a.size() - 1);
       tail.end = max(in.end, tail.end);
       a.add(tail);
     } else a.add(in);
     if (!flag && newIn.start <= a.get(a.size() - 1).end) {
       flag = true;
       Interval tail = a.remove(a.size() - 1);
       if (newIn.end >= tail.start) {
         tail.start = min(tail.start, newIn.start);
         tail.end = max(tail.end, newIn.end);
         a.add(tail);
       } else {
         a.add(newIn);
         a.add(tail);
       }
     }
   }
   if (!flag) a.add(newIn);
   return a;
 }
Ejemplo n.º 4
0
 public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
   List<Interval> ans = new ArrayList<Interval>();
   // true for: the newInterval has been inserted.
   boolean flag = false;
   for (int i = 0; i < intervals.size(); ++i) {
     // case 1. current interval is totally "before" newInterval
     if (intervals.get(i).end < newInterval.start) {
       ans.add(intervals.get(i));
       continue;
     }
     // case 2. current interval is totally "behind" newInterval
     if (intervals.get(i).start > newInterval.end) {
       if (!flag) {
         ans.add(newInterval);
         flag = true;
       }
       ans.add(intervals.get(i));
       continue;
     }
     // case 3. overlap
     if (intervals.get(i).start < newInterval.start) {
       newInterval.start = intervals.get(i).start;
     }
     if (intervals.get(i).end > newInterval.end) {
       newInterval.end = intervals.get(i).end;
       // Since there is only one interval to be inserted and others are separated.
       // The insertion is already done here.
       ans.add(newInterval);
       flag = true;
     }
   }
   if (!flag) {
     ans.add(newInterval);
   }
   return ans;
 }
  public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
    List<Interval> result = new ArrayList<Interval>();
    int i = 0;

    while (i < intervals.size() && intervals.get(i).end < newInterval.start) {
      result.add(intervals.get(i));
      i++;
    }

    while (i < intervals.size() && intervals.get(i).start <= newInterval.end) {
      newInterval.start = Math.min(newInterval.start, intervals.get(i).start);
      newInterval.end = Math.max(newInterval.end, intervals.get(i).end);
      i++;
    }

    result.add(newInterval);

    while (i < intervals.size()) {
      result.add(intervals.get(i));
      i++;
    }

    return result;
  }
Ejemplo n.º 6
0
  public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
    if (newInterval == null || intervals == null) {
      return intervals;
    }

    ArrayList<Interval> results = new ArrayList<Interval>();
    int insertPos = 0;

    for (Interval interval : intervals) {
      if (interval.end < newInterval.start) {
        results.add(interval);
        insertPos++;
      } else if (interval.start > newInterval.end) {
        results.add(interval);
      } else {
        newInterval.start = Math.min(interval.start, newInterval.start);
        newInterval.end = Math.max(interval.end, newInterval.end);
      }
    }

    results.add(insertPos, newInterval);

    return results;
  }