{"id":2088,"date":"2016-07-19T22:03:46","date_gmt":"2016-07-19T14:03:46","guid":{"rendered":"http:\/\/boweihe.me\/?p=2088"},"modified":"2016-07-19T22:03:46","modified_gmt":"2016-07-19T14:03:46","slug":"leetcode-278-first-bad-version","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2088","title":{"rendered":"LeetCode 278. First Bad Version"},"content":{"rendered":"<blockquote><p>\u505a\u70b9\u7b80\u5355\u7684\u9898\u76ee\u7ec3\u7ec3\u624b\uff0c\u6700\u8fd1\u5929\u5929\u5728\u5199Scope\u811a\u672c\uff0c\u90fd\u5feb\u5fd8\u8bb0\u600e\u4e48\u5199\u5176\u4ed6\u4ee3\u7801\u4e86\u3002<br \/>\n\u5410\u69fd\u4e00\u4e0b\u5783\u573e\u957f\u57ce\u5bbd\u5e26\uff0c\u4e0aLeetCode\u90fd\u8981\u81ea\u5df1\u5f00\u4ee3\u7406\uff0c\u4e0d\u7136\u6839\u672c\u52a0\u8f7d\u4e0d\u4e86<\/p><\/blockquote>\n<p>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.<br \/>\nSuppose you have <code>n<\/code> versions <code>[1, 2, ..., n]<\/code> and you want to find out the first bad one, which causes all the following ones to be bad.<br \/>\nYou are given an API <code>bool isBadVersion(version)<\/code> which will return whether <code>version<\/code> is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.<\/p>\n<h3>\u5206\u6790<\/h3>\n<p>\u8fd9\u5176\u5b9e\u5c31\u662f\u4e2a\u53d8\u79cd\u7684\u4e8c\u5206\u67e5\u627e\uff0c\u53ea\u8981\u6ce8\u610f\u627e\u5230\u4e00\u4e2abad version\u7684\u65f6\u5019\uff0c\u5f80\u524d\u591a\u770b\u4e00\u4e2a\u7248\u672c\u5c31\u80fd\u51b3\u5b9a\u662f\u5426\u8981\u7ee7\u7eed\u4e8c\u5206\u4e0b\u53bb\u4e86\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">\/\/ Forward declaration of isBadVersion API.\nbool isBadVersion(int version);\nclass Solution {\npublic:\n    int firstBadVersion(int n) {\n        int low = 1;\n        int high = n;\n        while(true){\n            int mid = low + (high - low)\/2;\n            if(isBadVersion(mid)){\n                \/\/Look for previous cases\n                bool isBad = isBadVersion(mid);\n                if(isBad){\n                    if(mid == 1)\n                        return mid;\n                    if(!isBadVersion(mid-1))\n                        return mid;\n                    high = mid - 1;\n                } else {\n                    low = mid + 1;\n                }\n            } else {\n                low = mid + 1;\n            }\n        }\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u505a\u70b9\u7b80\u5355\u7684\u9898\u76ee\u7ec3\u7ec3\u624b\uff0c\u6700\u8fd1\u5929\u5929\u5728\u5199Scope\u811a\u672c\uff0c\u90fd\u5feb\u5fd8\u8bb0\u600e\u4e48\u5199\u5176\u4ed6\u4ee3\u7801\u4e86\u3002 \u5410\u69fd\u4e00\u4e0b\u5783\u573e\u957f\u57ce\u5bbd\u5e26\uff0c\u4e0aLeetCode\u90fd\u8981\u81ea\u5df1\u5f00\u4ee3\u7406\uff0c\u4e0d\u7136\u6839\u672c\u52a0\u8f7d\u4e0d\u4e86 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, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[65],"class_list":["post-2088","post","type-post","status-publish","format-standard","hentry","category-technical","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2088"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2088\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}