class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
n = len(intervals)
intervals = sorted(intervals, key=lambda x:x[1])
ends = [end for start, end in intervals]
dp = [1 for interval in intervals]
for current_index in range(1, n):
start, end = intervals[current_index]
previous_index = bisect_right(ends, start, 0, current_index) - 1
if previous_index >= 0:
dp[current_index] += dp[previous_index]
dp[current_index] = max(dp[current_index], dp[current_index - 1])
return n - dp[-1]