First Bad Version
You manage n versions [1..n]. After some version every later version is bad. Given an API isBadVersion(version) that returns whether a version is bad, find the first bad version while minimizing the number of API calls.
Open official problem prompt ↗Find the exact version where quality flips from good to bad using as few probes as possible.
A row of light bulbs off then on. You want the first lit bulb. Instead of checking each, you test the middle: if it is on the switch is at or before it, if off the switch is later.
- Input
- n = 5, first bad version = 4
- Output
- 4
- Why
- Versions 1-3 are good and versions 4-5 are bad, so 4 is the first bad one.
1 <= bad <= n <= 2^31 - 1isBadVersion returns a booleanOnce a version is bad, all following versions are badMinimize calls to the API