Learn C++ STL

Lesson 6 of 9 · sort, Comparators and Lambdas: Tell the Algorithm What You Mean

Module 12 · sort, Comparators and Lambdas: Tell the Algorithm What You Mean

CP and Interview Pack: Sort Patterns, Questions and the Bug Gallery

ProReading

LeetCode problem 881, "Boats to Save People", is a favourite in interviews. Every boat carries at most two people, and their weights may add up to at most a limit. What is the fewest boats that carry everyone? Bob starts to try every way of pairing people up, and there are far too many. Amara sorts the weights first, and the answer falls out of one walk from both ends. Her solution is pattern 1.

Checking what this lesson needs for you…

What is inside CP and Interview Pack: Sort Patterns, Questions and the Bug Gallery

  1. Pattern 1: sort, then two pointers
  2. Pattern 2: sort, then walk the runs of equal values
  3. Pattern 3: sorting events for a sweep
  4. Twelve interview questions, with model answers