Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class Interval {
- public int n;
- public int m;
- public Interval(int n, int m){
- this.n = n;
- this.m = m;
- }
- }
- class Solution {
- public static void main(String[] args) {
- //Original Test Cases
- Interval m = new Interval(7, 9);
- Interval n = new Interval(2, 4);
- Interval o = new Interval(1, 3);
- Interval p = new Interval(3, 5);
- Interval q = new Interval(2, 5);
- Interval r = new Interval(4, 6);
- //Larger test case
- Interval a = new Interval(4, 8);
- Interval b = new Interval(5, 7);
- Interval c = new Interval(12, 19);
- Interval d = new Interval(9, 14);
- Interval e = new Interval(11, 15);
- Interval f = new Interval(19, 26);
- Interval g = new Interval(17, 23);
- Interval h = new Interval(10, 27);
- Interval i = new Interval(20, 22);
- Interval j = new Interval(21, 24);
- Interval k = new Interval(22, 23);
- Interval l = new Interval(25, 26);
- //Test case
- Interval s = new Interval(24, 34);
- Interval t = new Interval(13, 15);
- Interval u = new Interval(13, 16);
- Interval v = new Interval(45, 57);
- Interval w = new Interval(19, 25);
- Interval x = new Interval(12, 50);
- /*Uncomment to run with different data set */
- //List<Interval> intervals = Arrays.asList(a, b, c, d, e, f,g,h,i,j,k,l);
- //List<Interval> intervals = Arrays.asList(m,n,o,p,q,r);
- //List<Interval> intervals = Arrays.asList(s,t,u,v,w,x);
- 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);
- List<Interval> mergedIntervals = mergeOverlappingIntervals(intervals);
- //Prints result
- for(Interval interval : mergedIntervals){
- System.out.println(interval.n + "--->" + interval.m);
- }
- }
- /**
- *
- * @param intervals List of interval objects.
- * @return Returns the merging of all of the intervals.
- */
- private static List<Interval> logic(List<Interval> intervals){
- //Makes a copy of the list passed
- ArrayList<Interval> result = new ArrayList<Interval>(intervals);
- for(int i = 0; i < result.size(); i++){
- for(int j = i+1; j < result.size(); j++){
- if(overlaps(result.get(i), result.get(j))){//Checks if the intervals overlap
- //merged variable now holds the merged intervals.
- Interval merged = merged(result.get(i), result.get(j));
- //Sets the merged interval in place of the old one
- //Also removes the other interval because it's no longer needed.
- result.set(i, merged);
- result.remove(j);
- j--;//MUST DECREMENT J to move on to the correct next interval.
- }
- }
- }
- return result;
- }
- //Function used to call the logic.
- public static List<Interval> mergeOverlappingIntervals(List<Interval> intervals){
- List<Interval> result = logic(intervals);
- result = logic(result);
- return result;
- }
- /**
- *
- * @param one First interval object
- * @param two Second interval object
- * @return Returns true if they overlap and false if they don't
- */
- public static boolean overlaps(Interval one, Interval two){
- if(one.n == two.n && one.m == two.m) {
- return false;
- }
- if(one.n >= two.n && one.n <= two.m || two.n >= one.n && two.n <= one.m) {
- return true;
- }
- if(one.m <= two.m && one.m >= two.n ||
- two.m <= one.m && two.m >= one.n) {
- return true;
- }
- return false;
- }
- /**
- *
- * @param one First interval
- * @param two Second interval
- * @return Returns the merged version of the intervals passed.
- */
- public static Interval merged(Interval one, Interval two){
- int min = Math.min(one.n, two.n);
- int max = Math.max(one.m, two.m);
- return new Interval(min, max);
- }
- //Equals method used to check if the previous list and the merged list are the same
- //This is used to know when to stop merging.
- private static boolean equals(List<Interval> one, List<Interval> two) {
- if(two.size() == 1) {
- return true;
- }
- for(int i = 0; i < one.size(); i++) {
- for(int j = i+1; j < one.size(); j++) {
- if(one.get(i).n != two.get(j).n || one.get(i).m != two.get(j).m) {
- return false;
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement