# first-bad-version

## Problem

[First Bad Version](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3316/)

## Problem Description

```
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example:

Given n = 5, and version = 4 is the first bad version.

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true

Then 4 is the first bad version.
```

## Solution

### Solution 1 -- Brute force

Naive solution is to iterate \[0,n], and call api isFirstBadVersion(i), to check every version start from 0 to n, when isFirstBadVersion(i) is true, then found the first bad version, and return i.

![First Bad Version Brute Force](https://github.com/snowan/study-notes/tree/4efb1f4714ae75b4d8eec5368b430da97c64b0aa/assets/leetcode/first-bad-version-naive.png)

### Complexity Analysis

**Time Complexity:** `O(N)`

**Space Complexity:** `O(1)`

* N - input version number N&#x20;

### Code

```java
class Solution {
/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

    public int firstBadVersion(int n) {
      for (int i = 0; i <= n; i++) {
        if (isBadVersion(i)) {
            return i;
        }
      }
      return 0;
    }
}
```

### Solution 2 -- Binary Search

From solution 1, brute force observe that iterate number from 0 to n, and it is ordered.to minimize calling api, can do binary search first bad version.

![First Bad Version Binary Search](https://github.com/snowan/study-notes/tree/4efb1f4714ae75b4d8eec5368b430da97c64b0aa/assets/leetcode/first-bad-version-bs.png)

### Complexity Analysis

**Time Complexity:** `O(log(N))`

**Space Complexity:** `O(1)`

* N - input version number N

### Code -- Binary Search

```java
class Solution {
    public int firstBadVersion(int n) {
        int lo = 0;
        int hi = n;
        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            // if mid is bad versoin, continue search [0,mid] until find first bad version
            if (isBadVersion(mid)) {
                hi = mid;
            } else {
                lo = mid + 1;    
            }
        }
        return lo;
    }

}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://snowan.gitbook.io/study-notes/leetcode/may2020challenge/first-bad-version.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
