CoursesSearch & Priority Patterns

Monotonic Stack

Lessons
9
Exercises
55
Minutes
56
  1. 1

    The Stack That Stays Sorted

    Recognize next-greater and next-smaller questions and see why one sorted stack answers them all in a single pass.

    Multiple choiceTap
    5 exercises
    4 min
  2. 2

    Next Greater Element

    Scan once with a stack of unresolved indices — every pop hands a waiting element its answer.

    Complete codeMultiple choiceTrace
    6 exercises
    6 min
  3. 3

    Final Prices with Discount

    Flip the comparison: the same one-pass stack scan finds each item's discount the moment a smaller-or-equal price arrives.

    Complete codeMultiple choice
    5 exercises
    5 min
  4. 4

    Daily Temperatures

    Stack the indices of unresolved days — a warmer day pops them, and the answer is the index distance.

    Complete codeMultiple choiceTrace
    6 exercises
    6 min
  5. 5

    Online Stock Span

    Previous-greater on a stream: pop the prices you dominate and absorb their spans — never recount.

    Complete codeMultiple choiceTrace
    6 exercises
    6 min
  6. 6

    Remove K Digits

    Keep the kept digits climbing — while removals remain, pop any bigger digit the newcomer beats, then trim the tail and strip leading zeros.

    Complete codeMultiple choiceTrace
    6 exercises
    7 min
  7. 7

    Monotonic Stack CheckpointCheckpoint

    No new concepts — the island's pop-to-resolve rules under pressure: choosing the stack direction, tracing pops, the while condition, a duplicate-handling bug, and why one pass really is O(n).

    DebugFill blankMultiple choiceTrace
    6 exercises
    6 min
  8. 8

    Monotonic Stack ReviewReview

    Mixed recap of the island: spotting stack problems, pop mechanics, price discounts, greedy digit removal, spans, and what one-pass stacks really cost.

    Complete codeDebugMultiple choiceTapTrace
    7 exercises
    6 min
  9. 9

    Boss: Largest RectangleBoss

    The monotonic-stack finale: an increasing index stack hands every bar its previous-smaller and next-smaller walls at pop time, and a sentinel 0 flushes the survivors.

    Complete codeDebugCode orderMultiple choiceTrace
    8 exercises
    10 min
AlgoFox Pro

Search & Priority Patterns is part of AlgoFox Pro

You can read the whole course outline here. The exercises are in the app, along with your streak, your mistake review, and the progress that carries across your devices.

Pro starts with a 3 day free trial. The app store decides who is eligible for an introductory offer, and a lapsed subscription does not get another one.

Download AlgoFox on the App StoreGet AlgoFox on Google Play

Already subscribed in the app? Sign in with the same account and Pro works here too. Quick Play and Battle are free on the web, and so is the first course in every subject.