二分查找總結及部分Lintcode題目分析 4

二分法不僅能像以前的記錄,能夠找到index~第二種類型是找到二分答案。有如下幾個例子,都是以前二分法的擴展,再也不贅述,只記錄下忽略的點,之後回顧多注意~python

1. wood cutide

class Solution:
    """
    @param L: Given n pieces of wood with length L[i]
    @param k: An integer
    return: The maximum length of the small pieces.
    """
    def pieces(self, L, length):
        p = 0
        for i in L:
            p += i / length
        return p

    def woodCut(self, L, k):
        # at first, this if check condition is ignored
        if sum(L) < k:
            return 0
        start = 1
# at first, use the average of sum of array. end = max(L) while start + 1 < end: mid = start + (end - start) / 2 if self.pieces(L, mid) >= k: start = mid else: end = mid if self.pieces(L, end) >= k: return end return start

 

2. First Bad Version:能夠當作找到第一個是false的位置this

#class SVNRepo:
#    @classmethod
#    def isBadVersion(cls, id)
#        # Run unit tests to check whether verison `id` is a bad version
#        # return true if unit tests passed else false.
# You can use SVNRepo.isBadVersion(10) to check whether version 10 is a 
# bad version.
class Solution:
    """
    @param n: An integers.
    @return: An integer which is the first bad version.
    """
    def findFirstBadVersion(self, n):
        start = 1
        end = n
        while(start + 1 < end):
            mid = start + (end - start) / 2
            if SVNRepo.isBadVersion(mid):
                end = mid
            else:
                start = mid
        if SVNRepo.isBadVersion(start):
            return start
        return end

 

3. Search for a range:找到first position和last position的結合體,不過須要兩次遍歷,尚未找到更好的方法code

class Solution:
    """
    @param A : a list of integers
    @param target : an integer to be searched
    @return : a list of length 2, [index1, index2]
    """
    def searchRange(self, A, target):
        # write your code here
        if A is None or len(A) == 0:
            return[-1,-1]
        start = 0
        end = len(A) - 1
        while(start + 1 < end):
            mid = start + (end - start) / 2
            if A[mid] == target:
                end = mid
            elif A[mid] < target:
                start = mid
            else:
                end = mid
        if A[start] == target:
            left = start
        elif A[end] == target:
            left = end
        else:
            return[-1,-1]
        
        start = left
        end = len(A) - 1
        while start + 1 < end:
            mid = start + (end - start) / 2
            if A[mid] == target:
                start = mid
            elif A[mid] < target:
                start = mid
            else:
                end = mid
        if A[end] == target:
            right = end 
        # one tip: cannot use another if, because there maybe two continuous 
        # same num. so A[start] and A[end] maybe the same and the value of 
        # A[start] may override the right value
        elif A[start] == target:
            right = start
        return [left,right]
相關文章
相關標籤/搜索