---
# System prepended metadata

title: 278. First Bad Version
tags: [leetcode]

---

# 278. First Bad Version
#### Difficulty: Easy
link: https://leetcode.com/problems/first-bad-version/

### 1. Binary Search  
#### $O(log(N))$ runtime, $O(1)$ space
在一個連續的陣列中，找到滿足Bad version的最小index，可套用Binary Search Template。

##### python
```python=
# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:

class Solution:
    def firstBadVersion(self, n: int) -> int:
        left, right = 1, n
        while left < right:
            mid = left + (right - left) // 2
            if isBadVersion(mid):
                right = mid
            else:
                left = mid + 1
        return left
```
<font color="#00AB5F ">Accepted</font>  
Runtime: **58 ms**, faster than **18.36%** of Python3 online submissions for First Bad Version.
Memory Usage: **13.9 MB**, less than **62.26%** of Python3 online submissions for First Bad Version.

###### tags: `leetcode`