First Bad Version in a Monotone Array
Problem statement
The Boolean array bad describes software versions in chronological order. Once a version is bad, every later version is also bad.
Return the zero-based index of the first bad version, or -1 when every version is good.
Function
firstBadVersion(bad: boolean[]) → intExamples
Example 1
bad = [false,false,true,true]return = 2Index 2 is the first true entry.
Example 2
bad = [true,true]return = 0The first version is already bad.
Example 3
bad = [false,false]return = -1No bad version exists.
Constraints
1 <= bad.length <= 10^6.- The array is monotone: no
falseappears after atrue.