Arrays & Hashing · Pattern 17 of 26

Greedy: make the safe choice now

Make the choice that looks best right now and never undo it. It works when you can argue that some best answer always starts with that choice; usually after sorting.

6 variations · 28 problems · 2 with animated walkthroughs

Watch the lesson

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

When to use it

  • "Minimum number of jumps / taps / coins to cover"
  • Pick items by value per unit, or pair the largest with the smallest
  • A local rule (place a flower if both neighbours are empty) is clearly safe

The template

Greedy
// can you reach the last index? keep the farthest reachable index
boolean canJump(int[] nums) {
    int far = 0;
    for (int i = 0; i < nums.length; i++) {
        if (i > far) return false;             // stuck before i
        far = Math.max(far, i + nums[i]);
    }
    return true;
}

// minimum jumps: BFS by levels, each level is the range one more jump can reach
int jump(int[] nums) {
    int jumps = 0, end = 0, far = 0;
    for (int i = 0; i < nums.length - 1; i++) {
        far = Math.max(far, i + nums[i]);
        if (i == end) { jumps++; end = far; }  // must jump again to go further
    }
    return jumps;
}

The variations

  1. 1Reach as far as you can

    Track the farthest point reachable so far. Jumps, taps and "coins needed to cover 1..target" all extend a covered range greedily.

    Worked example Jump Game Medium Read the solution
  2. 2Sort, then decide in order

    After sorting by the right key, the best choice at each step is obvious: most units per box, biggest cost difference, tallest first, match the strongest opponent you can beat.

  3. 3Most frequent first

    The most frequent item decides the layout: place it with gaps, then fill the gaps. (max count − 1) × (n + 1) + ties is the task scheduler formula.

    Worked example Task Scheduler Medium Read the solution
    Now you solve
  4. 4Two passes, left then right

    Satisfy the rule with the left neighbour in a forward pass, then with the right neighbour in a backward pass, keeping the max of both.

    Worked example Candy Hard Read the solution
  5. 5Local choices that never need undoing

    Each step has a rule that is clearly safe: plant if both neighbours are empty, take the largest Roman symbol that fits, the answer is the largest digit.

  6. 6More practice

    More problems that use this pattern. Try them once the variations above feel easy.

Mistakes to avoid

  • Trusting a greedy rule without a reason. Try to break it with a small counterexample first; if it breaks, it is DP.
  • Sorting by the wrong key (by cost instead of by the difference of costs, for example).
  • Custom comparators that are not consistent (largest number: compare a + b with b + a).
Next: Hash set lookups →

Mark it done once you can write the template from memory and you have solved the worked examples yourself.