Skip to content

Commit 5ba4795

Browse files
committed
Update non overlapping interval problem
1 parent a1ddc8d commit 5ba4795

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

README.md

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

229229
### Interval
230230

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

233233
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

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

237237
4. Meeting rooms: [JavaScript](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/interval/meetingRooms.js)
238238

src/java1/algorithms/interval/NonOverlappingIntervals.java src/java1/algorithms/interval/nonOverlappingIntervals/NonOverlappingIntervals.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package java1.algorithms.interval;
1+
package java1.algorithms.interval.nonOverlappingIntervals;
22

33
import java.util.Arrays;
44

55
public class NonOverlappingIntervals {
6-
//Greedy approach: TC: O(n logn + n), SC: O(1)
6+
//Greedy approach: TC: O(n logn) + O(n), SC: O(1)
77
private static int eraseOverlappingIntervals(int[][] intervals) {
88
Arrays.sort(intervals, (a, b) -> a[0]-b[0]);
99
int minRemove = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of greedy approach. 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 minimum removal of intervals varaible `minRemove` to 0, which is used to hold the minimum number of intervals to be removed to make non-overlapping intervals.
7+
8+
3. Initialize previous end value of an interval `prevEnd` with first interval end value. i.e, Here, first interval is considered as non-overlapping interval by default.
9+
10+
4. Iterate through the rest of the intervals, starting from the second interval onwards. For each interval, keep the start(i.e, `start`) and end(i.e, `end`) values for comparing with other intervals.
11+
12+
5. If the start value is greater than the end value of the previous interval, then there is no overlap between the intervals. In this case, you can update the previous end value(`prevEnd`) with the current end value(`end`) of interval.
13+
14+
6. If the start value is less than to end value of the previous interval, then there is overlap between the intervals. In this case, you can increment `minRemove` counter by one. Also, the previous end value needs to be updated by minimum value between previous end value and current value. The intervals with the earliest end times are helpful to minimize the potential overlap with future intervals
15+
16+
7. Repeat steps 4-5 until all the intervals have been traversed.
17+
18+
8. Return `minRemove` as the minimum number of intervals to be removed to make a non overlapping interval list.
19+
20+
21+
**Time and Space complexity:**
22+
This algorithm has a time complexity of O(n logn) + O(n) ~= 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).
23+
24+
The space complexity of this algorithm is O(1) due to no additional data structure used.

src/javascript/algorithms/interval/nonOverlappingIntervals.js src/javascript/algorithms/interval/nonOverlappingIntervals/nonOverlappingIntervals.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//Greedy approach: TC: O(n logn + n), SC: O(1)
1+
//Greedy approach: TC: O(n logn) + O(n), SC: O(1)
22

33
function eraseOverlappingIntervals(intervals) {
4+
intervals.sort((a, b) => a[0]-b[0]);
45
let minRemoveCount = 0;
56
let prevEnd = intervals[0][1];
67

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of greedy approach. 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 minimum removal of intervals varaible `minRemove` to 0, which is used to hold the minimum number of intervals to be removed to make non-overlapping intervals.
7+
8+
3. Initialize previous end value of an interval `prevEnd` with first interval end value. i.e, Here, first interval is considered as non-overlapping interval by default.
9+
10+
4. Iterate through the rest of the intervals, starting from the second interval onwards. For each interval, keep the start(i.e, `start`) and end(i.e, `end`) values for comparing with other intervals.
11+
12+
5. If the start value is greater than the end value of the previous interval, then there is no overlap between the intervals. In this case, you can update the previous end value(`prevEnd`) with the current end value(`end`) of interval.
13+
14+
6. If the start value is less than to end value of the previous interval, then there is overlap between the intervals. In this case, you can increment `minRemove` counter by one. Also, the previous end value needs to be updated by minimum value between previous end value and current value. The intervals with the earliest end times are helpful to minimize the potential overlap with future intervals
15+
16+
7. Repeat steps 4-5 until all the intervals have been traversed.
17+
18+
8. Return `minRemove` as the minimum number of intervals to be removed to make a non overlapping interval list.
19+
20+
21+
**Time and Space complexity:**
22+
This algorithm has a time complexity of O(n logn) + O(n) ~= 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).
23+
24+
The space complexity of this algorithm is O(1) due to no additional data structure used.

0 commit comments

Comments
 (0)