29/40
Part 7 of 8 · The worked exampleLearn and do

The second answer counts: how many entries are at most x?

The second answer never merges. It picks a value x, counts the entries at most x, and binary-searches x until the count reaches k. The count comes first, and it takes O(n) with a staircase walk from the bottom-left corner:

  • If the cell is at most x, so is everything above it in that column, since columns are sorted. Add those cells, and step right.
  • Otherwise, step up.
print(count_at_most(M, 9))
Predict

How many values in M are at most 9?

Pick one to see what it prints.
Pick an answer above to see the rest