{"id":2095,"date":"2018-03-15T09:15:29","date_gmt":"2018-03-15T16:15:29","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2095"},"modified":"2018-03-15T21:47:01","modified_gmt":"2018-03-16T04:47:01","slug":"leetcode-278-first-bad-version","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-278-first-bad-version\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 278. First Bad Version"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2aAPI\u67e5\u8be2\u7248\u672c\u662f\u5426\u574f\u4e86\uff0c\u8ba9\u4f60\u627e\u51fa\u7b2c\u4e00\u4e2a\u574f\u6389\u7684\u7248\u672c\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/first-bad-version\/description\/\">https:\/\/leetcode.com\/problems\/first-bad-version\/description\/<\/a><\/p>\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.<\/p>\n<p>Suppose you have\u00a0<code>n<\/code>\u00a0versions\u00a0<code>[1, 2, ..., n]<\/code>\u00a0and you want to find out the first bad one, which causes all the following ones to be bad.<\/p>\n<p>You are given an API\u00a0<code>bool isBadVersion(version)<\/code>\u00a0which will return whether\u00a0<code>version<\/code>\u00a0is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.<\/p>\n<p><strong>Solution 1: Brute Force<\/strong><\/p>\n<p>Time Complexity: O(n) TLE<\/p>\n<p>Space Complexity: O(1)<\/p>\n<p><strong>Solution 2: Binary Search<\/strong><\/p>\n<p>Time Complexity: O(logn)<\/p>\n<p>Space Complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Forward declaration of isBadVersion API.\r\nbool isBadVersion(int version);\r\n\r\n\/\/ Author: Huahua\r\n\/\/ Running time: 2 ms\r\nclass Solution {\r\npublic:\r\n  int firstBadVersion(int n) {\r\n    int l = 1;\r\n    int r = n;\r\n    while (l &lt; r) {\r\n      int m = l + (r - l) \/ 2;\r\n      if (isBadVersion(m))\r\n        r = m;\r\n      else\r\n        l = m + 1;\r\n    }\r\n    return l;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2aAPI\u67e5\u8be2\u7248\u672c\u662f\u5426\u574f\u4e86\uff0c\u8ba9\u4f60\u627e\u51fa\u7b2c\u4e00\u4e2a\u574f\u6389\u7684\u7248\u672c\u3002 Problem: https:\/\/leetcode.com\/problems\/first-bad-version\/description\/ You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,222,253],"class_list":["post-2095","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-easy","tag-interactive","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2095"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2095\/revisions"}],"predecessor-version":[{"id":2097,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2095\/revisions\/2097"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}