Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Non-overlapping intervals problem solution

YASH PAL, 31 July 2024

In this Leetcode Non-overlapping intervals problem solution we have Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Leetcode Non-overlapping intervals problem solution

Problem solution in Python.

class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        intervals.sort(key=lambda x:x[0])
        n=len(intervals)
        i=0
        j=1
        count=0
        while j<n:
            if intervals[i][1]<=intervals[j][0]: #Non overlapping
                i=j
                j+=1
            elif intervals[i][1]<=intervals[j][1]: #partial overlapping
                j+=1
                count+=1
            elif intervals[i][1]>=intervals[j][1]: #full overalapping
                i=j
                count+=1
                j+=1
        return count

Problem solution in Java.

class Solution {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
        Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1]));
        
        LinkedList<int[]> link = new LinkedList<>();
        
        for (int i = 0; i < intervals.length; i++) {
            if (link.isEmpty() || link.getLast()[1] <= intervals[i][0]) {
                link.add(intervals[i]);
            }
        }
        return intervals.length - link.size();
    }
}

Problem solution in C++.

class Solution {
public:
    int eraseOverlapIntervals(vector<Interval>& intervals) {
        std::sort(intervals.begin(), intervals.end(), 
                    [](const Interval& lhs, const Interval& rhs) {
                        if (lhs.end != rhs.end) {
                            return lhs.end < rhs.end;
                        } else {
                            return lhs.start > rhs.start;
                        }
                    });
                    
        int n = intervals.size(), lastIdx = 0; 
        int result = 0;
        for (int i = 1; i < n; i++) {
            if (intervals[i].start < intervals[lastIdx].end) {
                result++;
            } else {
                lastIdx = i;
            }
        }
        return result;
    }
};

Problem solution in C.

int eraseOverlapIntervals(struct Interval* intervals, int intervalsSize) {
    int cmp(const void*a,const void*b)
    {
        return (*(struct Interval *)a).end -(*(struct Interval *)b).end ;
    }
    qsort(intervals, intervalsSize, sizeof(intervals[0]), cmp);
    int i = 0;
    int j = i + 1;
    int num = 0;
    while(i < intervalsSize - 1 && j < intervalsSize){
        if(intervals[j].start<intervals[i].end){
            num++;
        }else{
            i = j;
        }
        j++;
    }
    return num;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes