Search, greedy and DP

Binary search on the answer, greedy rules after sorting with an exchange argument, and the DP patterns that appear in contests.

About 7 hours of study

Learning objectives

Lessons

  1. 1

    Binary search on the answer

    When checking a guess is easy and the answer is a number, search for the best answer with binary search.

  2. 2

    Sorting first, then greedy

    Sort pairs, choose a comparator, and let the order make a greedy choice correct, with an exchange argument to check it.

  3. 3

    Dynamic programming in contests

    Counting ways modulo 10⁹+7, the longest increasing subsequence, and the 1D knapsack: the DP patterns that keep coming back.

Module practice

Module quiz

1. In binary search on the answer, works(mid) is false. What do you do?
2. To get the most non-overlapping meetings, sort them by:
3. In the DP for stairs, what must you do after each addition?
4. The answer to a binary-search problem can be up to 10¹². About how many checks are needed?
5. Why does the 0/1 knapsack loop the capacity downwards?
6. A greedy rule gives a wrong answer on a tiny test. What does that tell you?

‹ Array techniquesMath and bits ›