150 najlepszych wywiadówŁatwe

Nienakładające się interwały

Szczegółowy przewodnik i implementacja Python dla problemu „Nie nakładających się odstępów”.

Oświadczenie o problemie

Łatwe

Biorąc pod uwagę tablicę przedziałów interwałów, gdzie interwały[i] = [starti, endi], zwróć minimalną liczbę interwałów, które należy usunąć, aby pozostałe interwały nie nakładały się.

Napisz funkcję eraseOverlapIntervals(intervals: List[List[int]]) -> int.

Ograniczenia
  • 1 <= len(intervals) <= 10^5
  • intervals[i].length == 2
  • -5 * 10^4 <= starti < endi <= 5 * 10^4

Przykłady

Example 1
Input
intervals = [[1,2],[2,3],[3,4],[1,3]]
Output
1
Explanation

[1,3] can be removed and the rest are non-overlapping.

Example 2
Input
intervals = [[1,2],[1,2],[1,2]]
Output
2
Explanation

Remove two [1,2] to make the rest non-overlapping.

Example 3
Input
intervals = [[1,2],[2,3]]
Output
0
Explanation

Already non-overlapping.

Need a Hint?
Rozważ użycie struktur danych specyficznych dla interwałów, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Otwórz w Edytorze
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.