Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. class Interval {
  6. public int n;
  7. public int m;
  8.  
  9. public Interval(int n, int m){
  10. this.n = n;
  11. this.m = m;
  12.  
  13. }
  14.  
  15. }
  16.  
  17. class Solution {
  18.  
  19.  
  20. public static void main(String[] args) {
  21. //Original Test Cases
  22. Interval m = new Interval(7, 9);
  23. Interval n = new Interval(2, 4);
  24. Interval o = new Interval(1, 3);
  25. Interval p = new Interval(3, 5);
  26. Interval q = new Interval(2, 5);
  27. Interval r = new Interval(4, 6);
  28.  
  29. //Larger test case
  30. Interval a = new Interval(4, 8);
  31. Interval b = new Interval(5, 7);
  32. Interval c = new Interval(12, 19);
  33. Interval d = new Interval(9, 14);
  34. Interval e = new Interval(11, 15);
  35. Interval f = new Interval(19, 26);
  36. Interval g = new Interval(17, 23);
  37. Interval h = new Interval(10, 27);
  38. Interval i = new Interval(20, 22);
  39. Interval j = new Interval(21, 24);
  40. Interval k = new Interval(22, 23);
  41. Interval l = new Interval(25, 26);
  42.  
  43.  
  44. //Test case
  45. Interval s = new Interval(24, 34);
  46. Interval t = new Interval(13, 15);
  47. Interval u = new Interval(13, 16);
  48. Interval v = new Interval(45, 57);
  49. Interval w = new Interval(19, 25);
  50. Interval x = new Interval(12, 50);
  51.  
  52.  
  53. /*Uncomment to run with different data set */
  54.  
  55. //List<Interval> intervals = Arrays.asList(a, b, c, d, e, f,g,h,i,j,k,l);
  56. //List<Interval> intervals = Arrays.asList(m,n,o,p,q,r);
  57. //List<Interval> intervals = Arrays.asList(s,t,u,v,w,x);
  58. List<Interval> intervals = Arrays.asList(a, b, c, d, e, f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x);
  59.  
  60.  
  61.  
  62. List<Interval> mergedIntervals = mergeOverlappingIntervals(intervals);
  63.  
  64. //Prints result
  65. for(Interval interval : mergedIntervals){
  66. System.out.println(interval.n + "--->" + interval.m);
  67.  
  68. }
  69. }
  70.  
  71.  
  72. /**
  73. *
  74. * @param intervals List of interval objects.
  75. * @return Returns the merging of all of the intervals.
  76. */
  77. private static List<Interval> logic(List<Interval> intervals){
  78. //Makes a copy of the list passed
  79. ArrayList<Interval> result = new ArrayList<Interval>(intervals);
  80.  
  81. for(int i = 0; i < result.size(); i++){
  82. for(int j = i+1; j < result.size(); j++){
  83.  
  84. if(overlaps(result.get(i), result.get(j))){//Checks if the intervals overlap
  85. //merged variable now holds the merged intervals.
  86. Interval merged = merged(result.get(i), result.get(j));
  87.  
  88. //Sets the merged interval in place of the old one
  89. //Also removes the other interval because it's no longer needed.
  90. result.set(i, merged);
  91. result.remove(j);
  92. j--;//MUST DECREMENT J to move on to the correct next interval.
  93.  
  94.  
  95. }
  96. }
  97.  
  98. }
  99.  
  100. return result;
  101. }
  102.  
  103.  
  104. //Function used to call the logic.
  105. public static List<Interval> mergeOverlappingIntervals(List<Interval> intervals){
  106. List<Interval> result = logic(intervals);
  107.  
  108.  
  109.  
  110. result = logic(result);
  111.  
  112. return result;
  113.  
  114. }
  115.  
  116. /**
  117. *
  118. * @param one First interval object
  119. * @param two Second interval object
  120. * @return Returns true if they overlap and false if they don't
  121. */
  122. public static boolean overlaps(Interval one, Interval two){
  123. if(one.n == two.n && one.m == two.m) {
  124. return false;
  125. }
  126. if(one.n >= two.n && one.n <= two.m || two.n >= one.n && two.n <= one.m) {
  127. return true;
  128. }
  129. if(one.m <= two.m && one.m >= two.n ||
  130. two.m <= one.m && two.m >= one.n) {
  131. return true;
  132. }
  133.  
  134. return false;
  135.  
  136. }
  137.  
  138. /**
  139. *
  140. * @param one First interval
  141. * @param two Second interval
  142. * @return Returns the merged version of the intervals passed.
  143. */
  144. public static Interval merged(Interval one, Interval two){
  145. int min = Math.min(one.n, two.n);
  146. int max = Math.max(one.m, two.m);
  147.  
  148.  
  149. return new Interval(min, max);
  150.  
  151. }
  152.  
  153.  
  154.  
  155. //Equals method used to check if the previous list and the merged list are the same
  156. //This is used to know when to stop merging.
  157. private static boolean equals(List<Interval> one, List<Interval> two) {
  158. if(two.size() == 1) {
  159. return true;
  160. }
  161.  
  162. for(int i = 0; i < one.size(); i++) {
  163. for(int j = i+1; j < one.size(); j++) {
  164. if(one.get(i).n != two.get(j).n || one.get(i).m != two.get(j).m) {
  165. return false;
  166. }
  167. }
  168. }
  169. return true;
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement