Hey everyone! Starting from this video, the CPP Solution will also be provided. Please find the CPP solution with the same logic in the comments below.
Python Solution: class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: # Sort intervals by their end value intervals.sort(key=lambda x: x[1])
erase, end = 0, -float('inf') # Initialize counters
for interval in intervals: if interval[0] < end: # Overlap detected erase += 1 else: end = interval[1] # Update end to current interval's end
return erase # Return count of intervals to remove
CPP Solution: class Solution { public: int eraseOverlapIntervals(vector& intervals) { // Sort the intervals based on their end values in ascending order sort(intervals.begin(), intervals.end(), [](const vector& a, const vector& b) { return a[1] < b[1]; });
int erase = 0; // Variable to count the number of overlapping intervals to be erased int end = INT_MIN; // Variable to track the end value of the last non-overlapping interval
// Iterate through each interval in the sorted list for (const auto& interval : intervals) { // If the start value of the current interval is less than the end of the last interval, // it indicates an overlap if (interval[0] < end) { erase++; // Increment the count of intervals to erase } else { // Update the end value to the current interval's end // This marks the current interval as part of the non-overlapping set end = interval[1]; } }
// Return the total number of intervals that need to be removed to eliminate overlaps return erase; } };
Java Solution: class Solution { public int eraseOverlapIntervals(int[][] intervals) { // Sort the intervals based on their end values in ascending order // This ensures we process the interval that ends the earliest first Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1]));
int erase = 0; // Variable to count the number of overlapping intervals to be erased int end = Integer.MIN_VALUE; // Variable to track the end value of the last non-overlapping interval
// Iterate through each interval in the sorted list for (int[] interval : intervals) { // If the start value of the current interval is less than the end of the last interval, // it indicates an overlap if (interval[0] < end) { erase++; // Increment the count of intervals to erase } else { // Update the end value to the current interval's end // This marks the current interval as part of the non-overlapping set end = interval[1]; } }
// Return the total number of intervals that need to be removed to eliminate overlaps return erase; } }
Hey everyone! Starting from this video, the CPP Solution will also be provided. Please find the CPP solution with the same logic in the comments below.
Python Solution:
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
# Sort intervals by their end value
intervals.sort(key=lambda x: x[1])
erase, end = 0, -float('inf') # Initialize counters
for interval in intervals:
if interval[0] < end: # Overlap detected
erase += 1
else:
end = interval[1] # Update end to current interval's end
return erase # Return count of intervals to remove
CPP Solution:
class Solution {
public:
int eraseOverlapIntervals(vector& intervals) {
// Sort the intervals based on their end values in ascending order
sort(intervals.begin(), intervals.end(), [](const vector& a, const vector& b) {
return a[1] < b[1];
});
int erase = 0; // Variable to count the number of overlapping intervals to be erased
int end = INT_MIN; // Variable to track the end value of the last non-overlapping interval
// Iterate through each interval in the sorted list
for (const auto& interval : intervals) {
// If the start value of the current interval is less than the end of the last interval,
// it indicates an overlap
if (interval[0] < end) {
erase++; // Increment the count of intervals to erase
} else {
// Update the end value to the current interval's end
// This marks the current interval as part of the non-overlapping set
end = interval[1];
}
}
// Return the total number of intervals that need to be removed to eliminate overlaps
return erase;
}
};
Java Solution:
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
// Sort the intervals based on their end values in ascending order
// This ensures we process the interval that ends the earliest first
Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1]));
int erase = 0; // Variable to count the number of overlapping intervals to be erased
int end = Integer.MIN_VALUE; // Variable to track the end value of the last non-overlapping interval
// Iterate through each interval in the sorted list
for (int[] interval : intervals) {
// If the start value of the current interval is less than the end of the last interval,
// it indicates an overlap
if (interval[0] < end) {
erase++; // Increment the count of intervals to erase
} else {
// Update the end value to the current interval's end
// This marks the current interval as part of the non-overlapping set
end = interval[1];
}
}
// Return the total number of intervals that need to be removed to eliminate overlaps
return erase;
}
}