zoukankan      html  css  js  c++  java
  • 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.

    Analyse: find a number k in an continuous set {1, 2, ..., n}. Binary search.

    Runtime: 0ms.

     1 // Forward declaration of isBadVersion API.
     2 bool isBadVersion(int version);
     3 
     4 class Solution {
     5 public:
     6     int firstBadVersion(int n) {
     7         if(n == 0) return 0;
     8         if(n == 1) return isBadVersion(1);
     9         
    10         long long low = 0, high = n;
    11         while(low < high){
    12             long long mid = (low + high) >> 1;
    13             if(isBadVersion(mid)) high = mid;
    14             else low = mid + 1;
    15         }
    16         return high;
    17     }
    18 };
  • 相关阅读:
    团队作业(七)
    8848小分队:个人最终总结
    团队作业(四)
    参与团队编程的讨论郝一的角度
    团队作业(五)
    团队作业(三)
    团队作业(二)
    团队题目的确立与小组分工
    xJuujMbXeB
    团队作业汇报
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4796678.html
Copyright © 2011-2022 走看看