Identifying the minimum predicate search pattern
The minimum predicate search algorithm is a generalisation of the binary search algorithm used to find the first (minimum) point in a sorted search space at which a monotonic boolean function (predicate) changes its value, assuming it changes only once. It is a very powerful technique that can solve many optimization and feasibility problems in logarithmic time.
These are generally medium or hard problems where we have a large sorted search space, and we need to define a predicate function that verifies if certain constraints are met. The goal is to find the first (minimum) value in the search space where the result of the predicate function flips.
If the problem statement or its solution follows the generic template below, it can be solved using the minimum predicate search algorithm.
Given a predicate function that results in a monotonic sequence that flips once from
true to false or false to true over a sorted search space, find the minimum value in the search space where the result flips.Example
Let's consider the following problem as an example to better understand how to identify and solve a problem using the minimum predicate search algorithm.
Problem statement: Given an array `distance` of size `n`, where `distance[i]` denotes the distance of the `ith` bus ride. You are also given a decimal number of `hours`, representing the minimum time you must reach your house. To reach home, you must take sequential bus rides. Write a function to find and return the minimum speed that all buses must travel at so you can reach home on time. Return `-1` if it's not possible to reach home on time.
Note: Each bus can only depart at an integer time, so for e.g. if the 1st bus takes 2.3 hours, you must wait 0.7 hours to take the second bus.
Find the minimum speed at which we can reach home within 5.50 hours.
Liking the course? Check our discounted plans to continue learning.