Advertisement
Coriic

Untitled

Apr 27th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 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. if(checkIfIntervalsOverlapping(firstInterval, secondInterval)){
  13. mergeTwoIntervalsInArrayList(iterator, intervals);
  14. }
  15. else{
  16. iterator++;
  17. }
  18. }
  19. return intervals.size();
  20. }
  21.  
  22. private List<Interval> getSortedArrayList(int[] beginnings, int[] endings){
  23. List<Interval> sortedIntervals = new ArrayList<>();
  24. checkIfArraysHaveSameLength(beginnings, endings);
  25. for(int i = 0; i < beginnings.length; i++){
  26. sortedIntervals.add(new Interval(beginnings[i], endings[i]));
  27. }
  28. Collections.sort(sortedIntervals, (firstInterval, secondInterval) -> firstInterval.compareTo(secondInterval));
  29. return sortedIntervals;
  30. }
  31.  
  32. private boolean checkIfArraysHaveSameLength(int[] beginnings, int[] endings){
  33. if(beginnings.length != endings.length){
  34. throw new NotMatchingLengths();
  35. }
  36. else{
  37. return true;
  38. }
  39. }
  40.  
  41. private boolean checkIfIntervalsOverlapping(Interval first, Interval second){
  42. return first.getEnding() >= second.getBeginning();
  43. }
  44.  
  45. private void mergeTwoIntervalsInArrayList(int index, List<Interval> intervals){
  46. Integer newBeginning = intervals.get(index).getBeginning();
  47. Integer newEnding = intervals.get(index+1).getEnding();
  48. Interval mergedInterval = new Interval(newBeginning, newEnding);
  49. intervals.set(index, mergedInterval);
  50. intervals.remove(index+1);
  51. }
  52.  
  53. public static void main(String[] args){
  54. Solution solution = new Solution();
  55. int result = solution.solution(new int[]{1, 12, 42, 70, 36, -4, 43, 15}, new int[]{5, 15, 44, 72, 36, 2, 69, 24});
  56. System.out.println(result);
  57. }
  58. }
  59.  
  60. class Interval implements Comparable<Interval>{
  61.  
  62. private Integer beginning;
  63. private Integer ending;
  64.  
  65. public Integer getBeginning() {
  66. return beginning;
  67. }
  68.  
  69. public Integer getEnding(){
  70. return ending;
  71. }
  72.  
  73. public Interval(Integer beginning, Integer ending){
  74. this.beginning = beginning;
  75. this.ending = ending;
  76. }
  77.  
  78. @Override
  79. public int compareTo(Interval that){
  80. Integer beginningsCompared = this.beginning.compareTo(that.beginning);
  81. return beginningsCompared;
  82. }
  83. }
  84.  
  85. class NotMatchingLengths extends RuntimeException{
  86.  
  87. public NotMatchingLengths(){
  88. super("Arrays have different lengths");
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement