Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Greedy/Greedy

Technique

Interval Scheduling (sort by end)

To pick the most non-overlapping intervals (or fewest points stabbing all), sort by END and greedily take the earliest-finishing compatible one.

Learn firstGreedyMerge Intervals
0%
mastery
0/10 solved
Practise nextNon-overlapping Intervals
IdeaPracticePitfalls

Recognise it

The signal

maximum number of non-overlapping intervals

  1. 1Brute force

    Try all subsets or DP over sorted intervals.

  2. 2The observation

    Exponential choice: an exchange argument shows the earliest finisher can always be in an optimal set.

  3. 3Why it works

    The greedy schedule's k-th finish time is <= the k-th finish time of any feasible schedule (stays ahead).

Reach for it when

  • +minimum removals to make intervals disjoint
  • +minimum arrows/points hitting all intervals

Not when

  • ×intervals have weights (weighted interval scheduling = dp + binary search)
  • ×need to merge rather than select (merge-intervals)

Practice

Next

Non-overlapping Intervals

0/10 solved
  • Non-overlapping IntervalsAmazon, GoogleMedium
  • Minimum Number of Arrows to Burst BalloonsAmazon, UberMedium
  • ProjectsCSESMedium
  • Movie FestivalCSESMedium
  • Maximum Length of Pair ChainMedium
  • Movie Festival IICSESMedium
  • Movie Festival QueriesCSESHard
  • Maximum Profit in Job SchedulingHard
  • Maximum Number of Events That Can Be Attended IIHard
  • Maximum Order Volume (Weighted Interval Scheduling)SnowflakeOAHard

Pitfalls

Misconceptions

  • !sorting by start for selection (fails on one long early interval)

Looks like it, isn't

  • Maximum Number of Events That Can Be Attended II needs 1D / Fibonacci-style
    Why
    The values break the earliest-end greedy that solves part I. The editorial uses DP with binary search for the next compatible event. source ↗
  • Maximum Profit in Job Scheduling needs 1D / Fibonacci-style
    Why
    Weighted interval scheduling: sort by end, dp with binary search for the last compatible job. Greedy by end time fails with weights. source ↗
Go deeper2 combinations · resources

Combinations

  • Interval Scheduling (sort by end)+1D / Fibonacci-style+Binary Search

    choose non-overlapping intervals maximising total weight (or at most k of them)

    Interval Scheduling (sort by end):
    sort by end so that compatible predecessors form a prefix
    1D / Fibonacci-style:
    dp[i] = max(dp[i-1], w_i + dp[p(i)])
    Binary Search:
    find the last job ending at or before the current start

    Maximum Profit in Job SchedulingMaximum Number of Events That Can Be Attended IIProjects

  • Binary Lifting & LCA+Interval Scheduling (sort by end)

    many queries: how many non-overlapping intervals fit inside [a, b]

    Binary Lifting & LCA:
    jump 2^k greedy steps at once
    Interval Scheduling (sort by end):
    from time t, the greedy next movie is the one ending earliest among those starting >= t

    Movie Festival Queries

Resources

  • Greedy Algorithms with SortingUSACO Guide · course
  • Greedy Algorithms (chapter)Jeff Erickson · book
  • Exchange arguments (sorting with dp)Errichto · blog
  • Algorithms playlistAbdul Bari · video
  • Competitive Programmer's HandbookAntti Laaksonen · book
TodayReviewPatternsProblems