First Bad Version
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.
Analysis:
This is a classical Binary Search problem. Although the first paragraph of problem description seems useless, it still give us an important condition: all the versions after a bad version are also bad.
What does this mean? For example, since we have the API to return true/false for each position, now we have, say:
In the above 13 versions, the 4th version is bad and all the versions after neither. The problem now naturally becomes:
Given an array with only 0s and 1s (ture or false), find the first 1, as fast as you could.
OK, now we could easily apply the Binary Search to sovle it! Some of you might face another problem: binary search is to find one specific number, now there are many 1s in the array, what we should do now?
We know that every time in the binary search, we make decision according to the middle value, the "common" way is directly check the mid value and the target value. However, now if we met a "1", it is not certain that this "1" is the first "1" in the array. OK, what if we met a "0"? It is sure that 0 and all the numbers before this 0 are all 0s, and we don't care about them. Therefore, now the conditions are:
- If we met 0, search the right part.
- If we met 1, remember the position, and search the left part to see if there is any other 1.
A simple binary search will work well for this problem.
mau yang asik ? adu ayam
ReplyDeleteBagi bagi bonus www.s128.net
ReplyDeleteyoutube adu ayam saat ini sedang viral, mgkn ada sebgaian masyakarat yang tahu dengan video youtube sabung... BBM: Bolavita | WA: 081377055002
ReplyDeleteMau yang lebih ????? ayam tarung
ReplyDeletetonton di hp s128 taji ayam sabung online
ReplyDeleteAgen Online Terbaik
ReplyDeleteAgen Slot Terbaik
Situs Judi Slot
ReplyDeleteCSN88 | Berita Terkini | Sports | Agen Slot
Agen Slot | Agen Casino | Agen Poker | 88CSN
Agen Slot Terpercaya
Situs Agen Slot Terbaik
Situs Agen Casino Terpercaya
Agen Bola Terpercaya
Agen Togel Sidney
ReplyDeleteAgen Togel Singapura
Museumbola Slot IDN
Museumbola Slot Habanero
Museumbola Slot Pragmatic
AKSES SEGERA SITUS KAMI 1 ID BANYAK PERMAINAN
WA OFFICIAL : +6283157394921
ayo bergabung dengan bolavita khusus new member lgsg di berikan 10%
ReplyDeletetanpa ribet dan masih banyak bonus2 lain nya
semua di berikan tanpa ribet pelayanan terbaik 24 jam
depo wd secepat kilat ^^
sabung ayam online
info lbh lanjut :
WA: +628122222995