-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeIntervals.java
More file actions
146 lines (120 loc) · 4.05 KB
/
MergeIntervals.java
File metadata and controls
146 lines (120 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.ArrayList;
class MergeIntervals {
// A utility function to swap two elements
/**
* The given array is n x 2 and we are sorting
* by the first element
* @param arr - double array (n x 2)
* @param i - index of first element to swap
* @param j - index of second eleemtn to swap with the first
*/
static void swap(int[][] arr, int i, int j) {
int temp0 = arr[i][0];
int temp1 = arr[i][1];
arr[i][0] = arr[j][0];
arr[i][1] = arr[j][1];
arr[j][0] = temp0;
arr[j][1] = temp1;
}
/*
* This function takes last element as pivot, places
* the pivot element at its correct position in sorted
* array, and places all smaller (smaller than pivot)
* to left of pivot and all greater elements to right
* of pivot
*/
static int partition(int[][] arr, int low, int high) {
// pivot
int pivot = arr[high][0];
// Index of smaller element and
// indicates the right position
// of pivot found so far
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
// If current element is smaller
// than the pivot
if (arr[j][0] < pivot) {
// Increment index of
// smaller element
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}
/*
* The main function that implements QuickSort
* arr[] --> Array to be sorted,
* low --> Starting index,
* high --> Ending index
*/
static void quickSort(int[][] arr, int low, int high) {
if (low < high) {
// pi is partitioning index, arr[p]
// is now at right place
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// Function to print an array
static void printArray(int[][] arr, int size) {
for (int i = 0; i < size; i++)
System.out.println(arr[i][0] + " " + arr[i][1]);
System.out.println();
}
public static int[][] merge(int[][] arr) {
// sort the array of intervals by the first element
quickSort(arr, 0, arr.length - 1);
// holds the intervals to be inserted into the array
// to return
ArrayList<Integer> ints = new ArrayList<Integer>();
// current interval
int a = arr[0][0];
int b = arr[0][1];
int k = 1; // loop variable
// this could be converted to a for loop
while (k < arr.length) {
// comparison interval
int c = arr[k][0];
int d = arr[k][1];
// compare [a,b] to [c,d]
if (c <= b) {
b = Math.max(b,d);
} else {
// no interval overlap
// so keep [a,b] and
// compare [c,d] to the next interval
ints.add(a);
ints.add(b);
a = c;
b = d;
}
k++; // compare to next interval in the array
}
// when down add the final (possibly merged) interval
ints.add(a);
ints.add(b);
// convert the elements a1,b1,a2,b2,a3,b3, ....
// in the array list to an nx2 array
// [a1, b1], [a2, b2], [a3, b3], ....
int[][] merged = new int[ints.size()/2][2];
for (int i = 0; i < ints.size()/2; i++) {
merged[i][0] = ints.get(2*i);
merged[i][1] = ints.get(2*i+1);
}
return merged;
}
// Driver Code
public static void main(String[] args) {
//int[][] intervals = {{1,3},{2,6},{8,10},{15,18}};
//int[][] intervals = {{4,5}, {1,4}};
int[][] intervals ={{1,4},{1,5}};
int[][] m = merge(intervals);
System.out.println();
printArray(m, m.length);
}
}