Skip to content

Commit 073d383

Browse files
committed
Algo steps for merge intervals
1 parent fed19b0 commit 073d383

File tree

12 files changed

+104
-55
lines changed

12 files changed

+104
-55
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ List of Programs related to data structures and algorithms
228228

229229
### Interval
230230

231-
1. Insert interval: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/insertInterval.js)
231+
1. Insert interval: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/insertInterval.js)
232232

233-
2. Merge interval: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/mergeInterval.js)
233+
2. Merge interval: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/mergeInterval/mergeInterval.js) [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/mergeInterval/mergeInterval.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/mergeInterval/mergeInterval.md)
234234

235235
3. Non-overlapping intervals: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/nonOverlappingIntervals.js)
236236

src/java1/algorithms/array/buySellStock/BuySellStock.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
**Algorithmic Steps**
22
This problem is solved with the help of sliding window approach to calculate the maximum profit(as known as **greedy algorithm**). The algorithmic approach can be summarized as follows:
33

4-
1. Write a preliminary case by returning profit as `0` if the number of stock prices are less than 2.
4+
1. Write a preliminary case by returning profit as `0` if the number of stock prices are less than 2.
55

66
2. Initialize `maxProfit` variable as 0 to indicate maximum profit for buying and selling the stock at different days.
77

src/java1/algorithms/interval/MergeInterval.java

-29
This file was deleted.

src/java1/algorithms/interval/InsertInterval.java src/java1/algorithms/interval/insertInterval/InsertInterval.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package java1.algorithms.interval;
1+
package java1.algorithms.interval.insertInterval;
22
import java.util.*;
33

44
public class InsertInterval {

src/java1/algorithms/interval/insertInterval/InsertInterval.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package java1.algorithms.interval.mergeInterval;
2+
3+
import java.util.*;
4+
5+
public class MergeInterval {
6+
// TC: O(n log n) + O(n) => O(n log n) SC: O(n)
7+
private static int[][] mergeInterval(int[][] intervals) {
8+
Arrays.sort(intervals, (a, b) -> a[0]-b[0]);
9+
ArrayList<int[]> mergedIntervals = new ArrayList<>();
10+
mergedIntervals.add(intervals[0]);
11+
12+
for(int i=1; i< intervals.length; i++) {
13+
int[] currInterval = intervals[i];
14+
int[] lastMergedInterval = mergedIntervals.get(mergedIntervals.size()-1);
15+
if(lastMergedInterval[1] < currInterval[0]) {
16+
mergedIntervals.add(currInterval);
17+
} else {
18+
lastMergedInterval[1] = Math.max(lastMergedInterval[1], currInterval[1]);
19+
}
20+
}
21+
22+
return mergedIntervals.toArray(new int[mergedIntervals.size()][]);
23+
}
24+
25+
public static void main(String[] args) {
26+
int[][] intervals1 = {{1,3},{2,6},{8,10},{15,18}};
27+
int[][] intervals2 = {{1,5},{5,8}};
28+
29+
System.out.println(Arrays.deepToString(mergeInterval(intervals1)));
30+
System.out.println(Arrays.deepToString(mergeInterval(intervals2)));
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of sorting intervals followed by merging the intervals through iteration. The algorithmic approach can be summarized as follows:
3+
4+
1. First, sort the list of intervals based on their start times. This step helps to keep the overlapping intervals next to each other in the list.
5+
6+
2. Initialize a new list `mergedIntervals` to hold the merged intervals and add the first interval to it.
7+
8+
3. Iterate through the rest of the intervals, starting from the second interval onwards. For each interval, compare its start value with the end value of the last interval in the merged list.
9+
10+
4. If the start value is greater than the end value of the last interval, then there is no overlap between the intervals. In this case, you can add the current interval as a new entry to the merged list.
11+
12+
5. If the start value is less than or equal to the end value of the last interval, then there is overlap between the intervals. In this case, you can update the end value of the last interval in the merged list to the maximum of the end value of the last interval and the start value of current interval.
13+
14+
6. Repeat steps 3-5 until all the intervals have been traversed.
15+
16+
7. Return `mergedIntervals` as the final merged intervals.
17+
18+
19+
**Time and Space complexity:**
20+
This algorithm has a time complexity of O(n logn) + O(n), where n is the number of intervals. This is because sort operation typically takes a time complexity of O(n log n) for sorting the list. Thereafter, iteration over the intervals take a time complexity of O(n). So the total time complexity simplifies to O(n log n).
21+
22+
The space complexity of this algorithm is O(n) as we need to store a list containing all the merged intervals.

src/javascript/algorithms/interval/insertInterval/insertInterval.md

Whitespace-only changes.

src/javascript/algorithms/interval/mergeInterval.js

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// TC: O(n log n) + O(n)=> O(n log n) SC: O(n)
2+
3+
function mergeInterval(intervals) {
4+
let mergedIntervals = [];
5+
intervals.sort((a, b) => a[0]-b[0]);
6+
mergedIntervals.push(intervals[0]);
7+
8+
for(let i=1; i< intervals.length; i++) {
9+
let currInterval = intervals[i];
10+
let latestMergeInterval = mergedIntervals[mergedIntervals.length-1];
11+
if(latestMergeInterval[1] < currInterval[0]) {
12+
mergedIntervals.push(currInterval);
13+
} else {
14+
mergedIntervals[mergedIntervals.length-1][1] = Math.max(latestMergeInterval[1], currInterval[1]);
15+
}
16+
}
17+
18+
return mergedIntervals;
19+
}
20+
21+
let intervals1 = [[1,3],[2,6],[8,10],[15,18]];
22+
let intervals2 = [[1,5],[5,8]];
23+
console.log(mergeInterval(intervals1));
24+
console.log(mergeInterval(intervals2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of sorting intervals followed by merging the intervals through iteration. The algorithmic approach can be summarized as follows:
3+
4+
1. First, sort the list of intervals based on their start times. This step helps to keep the overlapping intervals next to each other in the list.
5+
6+
2. Initialize a new list `mergedIntervals` to hold the merged intervals and add the first interval to it.
7+
8+
3. Iterate through the rest of the intervals, starting from the second interval onwards. For each interval, compare its start value with the end value of the last interval in the merged list.
9+
10+
4. If the start value is greater than the end value of the last interval, then there is no overlap between the intervals. In this case, you can add the current interval as a new entry to the merged list.
11+
12+
5. If the start value is less than or equal to the end value of the last interval, then there is overlap between the intervals. In this case, you can update the end value of the last interval in the merged list to the maximum of the end value of the last interval and the start value of current interval.
13+
14+
6. Repeat steps 3-5 until all the intervals have been traversed.
15+
16+
7. Return `mergedIntervals` as the final merged intervals.
17+
18+
19+
**Time and Space complexity:**
20+
This algorithm has a time complexity of O(n logn) + O(n), where n is the number of intervals. This is because sort operation typically takes a time complexity of O(n log n) for sorting the list. Thereafter, iteration over the intervals take a time complexity of O(n). So the total time complexity simplifies to O(n log n).
21+
22+
The space complexity of this algorithm is O(n) as we need to store a list containing all the merged intervals.

0 commit comments

Comments
 (0)