Non-Overlapping Intervals — control flow
flowchart TD
S["<b>i · sort by end</b><br/>intervals.sort(key=x[1])"]
--> I["<b>ii · init</b><br/>count = 0; last_end = intervals[0][1]"]
--> L{"<b>iii · next interval?</b>"}
L -->|"yes"| C{"<b>iv · overlap?</b><br/>s < last_end?"}
C -->|"yes"| R["<b>v · remove</b><br/>count += 1"]
C -->|"no"| K["<b>vi · keep</b><br/>last_end = e"]
R --> L
K --> L
L -->|"done"| O["<b>return</b> count"]
classDef start fill:#f5efe1,stroke:#6a8a4f,stroke-width:2px,color:#1a1915;
classDef decision fill:#fdecd3,stroke:#c2410c,stroke-width:2px,color:#1a1915;
classDef action fill:#e7efd9,stroke:#587640,stroke-width:2px,color:#1a1915;
class S,I start
class L,C decision
class R,K,O action-
Sort by end
Greedy correctness depends on this — process the earliest-finishing interval first.
-
Initialise
`count = 0`; `last_end` = end of the first (earliest-finishing) interval.
-
Iterate
Walk the remaining intervals from index 1.
-
Overlap check
`s < last_end` — the current interval starts inside the kept one.
-
Remove
Increment `count`; do NOT update `last_end` (we are discarding this interval).
-
Keep
No overlap — move `last_end` to this interval’s end and continue.
Comments
Comments are disabled in this environment. Set
PUBLIC_GISCUS_REPO,PUBLIC_GISCUS_REPO_ID, andPUBLIC_GISCUS_CATEGORY_IDto enable.