Advertisement
Coriic

Untitled

Apr 27th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Solution {
  4.  
  5.     public int solution(int[] A, int[] B){
  6.         List<Interval> intervals = getSortedArrayList(A, B);
  7.         Interval firstInterval, secondInterval;
  8.         int iterator = 0;
  9.         while(iterator < intervals.size()-1){
  10.             firstInterval = intervals.get(iterator);
  11.             secondInterval = intervals.get(iterator+1);
  12.             //merging two intervals if overlapping
  13.             if(checkIfIntervalsOverlapping(firstInterval, secondInterval)){
  14.                 mergeTwoIntervalsInArrayList(iterator, intervals, firstInterval, secondInterval);
  15.             }
  16.             //moving to next interval if not overlapping
  17.             else{
  18.                 iterator++;
  19.             }
  20.         }
  21.         return intervals.size();
  22.     }
  23.  
  24.     //packing coordinates to Interval objects and sorting ArrayList
  25.     private List<Interval> getSortedArrayList(int[] beginnings, int[] endings){
  26.         List<Interval> sortedIntervals = new ArrayList<>();
  27.         for(int i = 0; i < beginnings.length; i++){
  28.             sortedIntervals.add(new Interval(beginnings[i], endings[i]));
  29.         }
  30.         Collections.sort(sortedIntervals,
  31.             (firstInterval, secondInterval) -> firstInterval.compareTo(secondInterval));
  32.         return sortedIntervals;
  33.     }
  34.    
  35.     private boolean checkIfArraysHaveSameLength(int[] beginnings, int[] endings){
  36.         if(beginnings.length != endings.length){
  37.             throw new NotMatchingLengths();
  38.         }
  39.         else{
  40.             return true;
  41.         }
  42.     }
  43.  
  44.     //if ending of first interval is greater than beginning of second interval then intervals are overlapping
  45.     private boolean checkIfIntervalsOverlapping(Interval first, Interval second){
  46.         return first.getEnding() >= second.getBeginning();
  47.     }
  48.    
  49.     private void mergeTwoIntervalsInArrayList(int index, List<Interval> intervals, Interval firstInterval, Interval secondInterval){
  50.         Integer newBeginning = firstInterval.getBeginning();
  51.         Integer newEnding = secondInterval.getEnding();
  52.         //merged interval starts with beginning of first interval and ends with ending of second interval
  53.         Interval mergedInterval = new Interval(newBeginning, newEnding);
  54.         //extending current interval
  55.         intervals.set(index, mergedInterval);
  56.         //removing interval used for extending
  57.         intervals.remove(index+1);
  58.     }
  59. }
  60.  
  61. //Class representing interval as it's beginning and ending points
  62. class Interval implements Comparable<Interval>{
  63.  
  64.     private Integer beginning;
  65.     private Integer ending;
  66.  
  67.     public Interval(Integer beginning, Integer ending){
  68.         this.beginning = beginning;
  69.         this.ending = ending;
  70.     }
  71.    
  72.     public Integer getBeginning() {
  73.         return beginning;
  74.     }
  75.  
  76.     public Integer getEnding(){
  77.         return ending;
  78.     }
  79.  
  80.     //comparing two Intervals basing on beginning's value
  81.     @Override
  82.     public int compareTo(Interval that){
  83.         Integer beginningsCompared = this.beginning.compareTo(that.beginning);
  84.         return beginningsCompared;
  85.     }
  86. }
  87.  
  88. //Exception indicating that matrixes have different lengths
  89. class NotMatchingLengths extends RuntimeException{
  90.  
  91.     public NotMatchingLengths(){
  92.         super("Arrays have different lengths");
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement