Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Solution {
- public int solution(int[] A, int[] B){
- List<Interval> intervals = getSortedArrayList(A, B);
- Interval firstInterval, secondInterval;
- int iterator = 0;
- while(iterator < intervals.size()-1){
- firstInterval = intervals.get(iterator);
- secondInterval = intervals.get(iterator+1);
- //merging two intervals if overlapping
- if(checkIfIntervalsOverlapping(firstInterval, secondInterval)){
- mergeTwoIntervalsInArrayList(iterator, intervals, firstInterval, secondInterval);
- }
- //moving to next interval if not overlapping
- else{
- iterator++;
- }
- }
- return intervals.size();
- }
- //packing coordinates to Interval objects and sorting ArrayList
- private List<Interval> getSortedArrayList(int[] beginnings, int[] endings){
- List<Interval> sortedIntervals = new ArrayList<>();
- for(int i = 0; i < beginnings.length; i++){
- sortedIntervals.add(new Interval(beginnings[i], endings[i]));
- }
- Collections.sort(sortedIntervals,
- (firstInterval, secondInterval) -> firstInterval.compareTo(secondInterval));
- return sortedIntervals;
- }
- private boolean checkIfArraysHaveSameLength(int[] beginnings, int[] endings){
- if(beginnings.length != endings.length){
- throw new NotMatchingLengths();
- }
- else{
- return true;
- }
- }
- //if ending of first interval is greater than beginning of second interval then intervals are overlapping
- private boolean checkIfIntervalsOverlapping(Interval first, Interval second){
- return first.getEnding() >= second.getBeginning();
- }
- private void mergeTwoIntervalsInArrayList(int index, List<Interval> intervals, Interval firstInterval, Interval secondInterval){
- Integer newBeginning = firstInterval.getBeginning();
- Integer newEnding = secondInterval.getEnding();
- //merged interval starts with beginning of first interval and ends with ending of second interval
- Interval mergedInterval = new Interval(newBeginning, newEnding);
- //extending current interval
- intervals.set(index, mergedInterval);
- //removing interval used for extending
- intervals.remove(index+1);
- }
- }
- //Class representing interval as it's beginning and ending points
- class Interval implements Comparable<Interval>{
- private Integer beginning;
- private Integer ending;
- public Interval(Integer beginning, Integer ending){
- this.beginning = beginning;
- this.ending = ending;
- }
- public Integer getBeginning() {
- return beginning;
- }
- public Integer getEnding(){
- return ending;
- }
- //comparing two Intervals basing on beginning's value
- @Override
- public int compareTo(Interval that){
- Integer beginningsCompared = this.beginning.compareTo(that.beginning);
- return beginningsCompared;
- }
- }
- //Exception indicating that matrixes have different lengths
- class NotMatchingLengths extends RuntimeException{
- public NotMatchingLengths(){
- super("Arrays have different lengths");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement