Skip to content

Latest commit

 

History

History
21 lines (20 loc) · 639 Bytes

278. First Bad Version.md

File metadata and controls

21 lines (20 loc) · 639 Bytes

Solution1

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        int lo = 1, hi = n;
        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            if (isBadVersion(mid)) {
                hi = mid;
            }
            else {
                lo = mid + 1;
            }
        }
        return lo;
    }
}

note

  • For this problem, do not use lo + 1 < hi and examine once again after loop which will cause TLE, because we called isBadVersion an extra time. Instead, just use lo < hi condition, he loop ends when lo == hi and we can either return lo or hi.