Multi-thread Simulation Solution


  • -1
    W

    If the websit can support multi thread,I think my code will be more effective

     //using std::thread for checkRange
    int firstBadVersion(int n) 
    {
        int version = checkRange(1,n);
        return (version > 0)?version:1;
    }
    
    int checkRange(int begin,int end) {
        if(!isBadVersion(begin) && !isBadVersion(end))
            return 0;
        
        if(end - begin + 1 < 10)
        {
            for(int i = begin;i <= end;i++)
            {
                if(isBadVersion(i))
                    return i;
            }
        }
    
       int step = static_cast<int>(std::sqrt(end - begin + 1));
       int goodVersion = 0;
       for(int i = 0;i < step;i ++ )
       {
           int tmp = checkRange(begin + i * step,begin + (i + 1) * step);
           if(tmp)
            {
                goodVersion = tmp;
                break;
            }
       }
       if(!goodVersion)
        goodVersion = checkRange(begin + step*step,end);
    
        return goodVersion;
    }

  • 0
    Y

    醉了,那很多题都可以啊,类似的还可以有换更好的机器,独占CPU,做成分布式。。。


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.