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;
 }
  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;
  }
  public int minMeetingRooms(Interval[] intervals) {
    if (intervals == null || intervals.length == 0) return 0;
    Arrays.sort(
        intervals,
        new Comparator<Interval>() {
          public int compare(Interval a, Interval b) {
            return a.start - b.start;
          }
        });
    PriorityQueue<Interval> heap =
        new PriorityQueue<Interval>(
            intervals.length,
            new Comparator<Interval>() {
              public int compare(Interval a, Interval b) {
                return a.end - b.end;
              }
            });
    heap.offer(intervals[0]);
    for (int i = 1; i < intervals.length; i++) {
      Interval poll = heap.poll();

      if (intervals[i].start >= poll.end) poll.end = intervals[i].end;
      else heap.offer(intervals[i]);
      heap.offer(poll);
    }
    return heap.size();
  }
Exemple #4
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();
  }
 public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
   // Start typing your Java solution below
   // DO NOT write main() function
   Collections.sort(
       intervals,
       new Comparator<Interval>() {
         public int compare(Interval a, Interval b) {
           return new Integer(a.start).compareTo(new Integer(b.start));
         }
       });
   ArrayList<Interval> res = new ArrayList<Interval>();
   if (intervals.size() == 0) return res;
   Interval first = new Interval(intervals.get(0).start, intervals.get(0).end);
   if (intervals.size() == 1) {
     res.add(first);
     return res;
   }
   int i = 1;
   while (i < intervals.size()) {
     if (intervals.get(i).start > first.end) {
       res.add(first);
       first = intervals.get(i);
     } else if (intervals.get(i).end > first.end) {
       first.end = intervals.get(i).end;
     }
     i++;
   }
   res.add(first);
   return res;
 }
Exemple #6
0
 public List<Interval> merge(List<Interval> intervals) {
   List<Interval> res = new ArrayList<>();
   Collections.sort(intervals, (Interval i1, Interval i2) -> i1.start - i2.start);
   Interval pre = null;
   for (Interval i : intervals) {
     if (res.isEmpty() || i.start > pre.end) {
       res.add(i);
       pre = i;
     } else {
       pre.end = Math.max(pre.end, i.end);
     }
   }
   return res;
 }
  public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
    if (intervals == null || intervals.size() <= 1) return intervals;

    Collections.sort(intervals, new IntervalComparator());
    ArrayList<Interval> res = new ArrayList<Interval>();
    Interval last = intervals.get(0);
    for (int i = 1; i < intervals.size(); i++) {
      Interval curt = intervals.get(i);
      if (last.end < curt.start) {
        res.add(last);
        last = curt;
      } else {
        last.end = Math.max(last.end, curt.end);
      }
    }
    res.add(last);
    return res;
  }
  public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
    Collections.sort(intervals, new IntervalComparator());
    ArrayList<Interval> result = new ArrayList<Interval>();

    if (intervals.size() <= 1) {
      return intervals;
    }

    result.add(intervals.get(0));
    for (int i = 1; i < intervals.size(); i++) {
      Interval top = result.get(result.size() - 1);
      if (top.end >= intervals.get(i).start) {
        // pick the larger end
        top.end = Math.max(top.end, intervals.get(i).end);
      } else {
        result.add(intervals.get(i));
      }
    }

    return result;
  }
 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;
  }
Exemple #11
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;
  }
Exemple #12
0
 private void merge(Interval p, Interval n) {
   if (p != null && n != null && (p.end == n.start || p.end + 1 == n.start)) {
     p.end = n.end;
     set.remove(n);
   }
 }