dev-resources.site
for different kinds of informations.
Non overlapping intervals
Published at
12/7/2024
Categories
greedy
leetcode
dsa
algorithms
Author
Prashant Mishra
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
//same as N meetings/no. of activities
//sort the intervals based on end time of each pair
Queue<Pair<Integer,Integer>> q = new PriorityQueue<>((a,b)->a.getValue()-b.getValue());
for(int i = 0;i<intervals.length;i++){
q.add(new Pair(intervals[i][0], intervals[i][1]));
}
int previousEndTime = Integer.MIN_VALUE;
int count = 0;
while(!q.isEmpty()){
Pair<Integer,Integer> p = q.remove();
int start = p.getKey();
int end = p.getValue();
if(start>=previousEndTime){
count++;
previousEndTime = end;
}
}
return intervals.length-count;
}
}
Articles
12 articles in total
Minimize XOR
read article
Count prefix and suffix I and II
read article
Rabin Karp (hashing) String pattern matching
read article
Min no. of operations to move all balls to each box
read article
Find all anagrams in the string[Fixed Window pattern]
read article
No of ways to split Array
read article
Range sum query 2D - Immutable
read article
Count vowel strings in ranges
read article
Range Sum Query - Immutable
read article
Job Scheduling Problem with Max Profit
read article
Non overlapping intervals
currently reading
Minimum No. of arrows to burst balloons
read article
Featured ones: