C# Solution


  • 0
    S

    Key here is to sort by end time to get least number count

    public int EraseOverlapIntervals(Interval[] intervals)
            {
                int result = 0;
    
                if (intervals == null || intervals.Count() == 1)
                {
                    return 0;
                }
    
                var items = intervals.ToList();
                items.Sort(delegate (Interval c1, Interval c2) { return c1.end.CompareTo(c2.end); });
    
                int i = 0;
                int j = 1;
    
                while(j < items.Count())
                {
                    if(items[j].start < items[i].end)
                    {
                        result++;
                        j++;
                    }
                    else
                    {
                        i = j;
                        j++;
                    }
                }
    
                return result;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.