TopK code in question (Python, Java)

There are two main methods of it, call the function would not talk about the interview might be playing. . . Mainly two classic algorithms, a heap sort, is a row of quick upgrade version: fast method of choice.

Quick selection method

The first is a rapid method of selecting, using the Quicksort will partition into two portions, so if the length is greater than the left partition K, can be narrow, if the left side of the partition is less than K, then simply find the right side of the partition the remaining amount on it. If the fast row familiar with the case, but it is still quite simple, you need to point to note is the need for random selection.

import random
def partition(nums,left,right):
    target = nums[left]
    raw = left
    left=left+1
    while left<= right:
        while left<=right and nums[right] > target:
            right -= 1
        while left<= right and nums[left] < target:
            left += 1
        if left <= right:#这个需要注意。
            nums[left],nums[right] = nums[right],nums[left]
    nums[raw],nums[left-1] = nums[left-1],nums[raw]
    return left -1

def randomPartition(nums,left,right):
    target = the random.randint (left, right)
     # randomly switching a number and their location! ! Or will the bugs 
    the nums [target], the nums [right] = the nums [right], the nums [target]
    pivo = partition(nums,left,right)
    return  pivo

def selectMinNum(nums,left,right,k):
    pivo = randomPartition(nums,left,right)
    if k == pivo - left + 1:
        return ;
    if k < pivo - left+1:
        selectMinNum(nums,left,pivo-1,k)
    if k > pivo - left+ 1:
        selectMinNum(nums,pivo+1,right,k-pivo+left-1)

def selectMaxNum(nums,left,right,k):
    pivo = randomPartition(nums,left,right)
    if k ==right-pivo+1:
        return;
    if k < right-pivo+1:
        selectMinNum(nums,pivo+1,right,k)
    if k > right-pivo+1:
        selectMinNum(nums,left,pivo-1,k-right+pivo-1)

This was quite easy to understand, look at the code, it should be able to understand, this is the code can be used directly, the core is that the use of fast-row and half thought, if desired complexity is O (N) Is not it amazing what For details, refer to Introduction to algorithms Chapter IX oh.

Which includes two, find the minimum and maximum N N. He is to sign a change.

stack

Maximum and minimum heap heap of ideas, each with a capacity of k maintain the heap, each come in a number of transformation heap. The trees come in on the last, then the heap transformation from bottom to top.

 

# This function is to adjust the stack from top to bottom 
DEF maxHeapOne (List2, index1,):
     IF index1,. 1 + 2 * <= len (List2) -1 :
        leftChild = index1*2+1
        rightChild = index1*2+2
        if rightChild > len(list2)-1:
            if list2[index1] < list2[leftChild]:
                list2[index1],list2[leftChild] = list2[leftChild],list2[index1]
            return
        if list2[index1] > list2[rightChild] and list2[index1] > list2[leftChild]:
            return
        if list2[leftChild]> list2[rightChild]:
            if list2[index1] < list2[leftChild]:
                list2[index1],list2[leftChild] = list2[leftChild],list2[index1]
                maxHeapOne(list2,leftChild)
        if list2[leftChild] < list2[rightChild]:
            if list2[index1] < list2[rightChild]:
                list2[index1],list2[rightChild] = list2[rightChild],list2[index1]
                maxHeapOne(list2,rightChild)

# This function is to adjust the stack from bottom to top, which is provided before the n-1 entry for the maximum heap 
DEF heapUpdate (List2):
    N = len (list2)
    cur = N-1
    faNode = cur>>1
    while faNode >=0:
        if list2[faNode] > list2[cur]:
            return
        if list2[faNode] < list2[cur]:
            list2[faNode],list2[cur] = list2[cur],list2[faNode]
        cur = faNode
        faNode = (faNode-1)>>1


# Build heap 
DEF createHeap (list2):
    N = len (list2)
     # cur = N-1
    start = (N-1)>>1
    the while Start> = 0: # parent node of the last traversing a child node. For each node to build the biggest pile. 
        # Print (Start) 
        maxHeapOne (list2, Start)
        Start -. 1 =
 # obtain maximum 
DEF getMax (List2):
    createHeap(list2)
    list2[0],list2[-1] = list2[-1],list2[0]
    res = list2.pop()
    maxHeapOne (list2,0)    # After removing the maximum value of the stack is adjusted, from top to bottom to adjust 
    return RES

# minTOPN

def getminTop(list2,k):
    list3 = list2 [: k] # k to establish a maximum heap
    createHeap(list3) 
    for I in List2 [K:]: # on the back of this insert element is adjusted stack
        list3 +=[i]
        heapUpdate(list3)
        list3 = list3[-k:]
    return list3

 

Most importantly, the first two functions, one is to transform from top to bottom of the heap, and the second is the transformation of the heap from bottom to top. The remaining functions are also relatively easy to understand, of course, for the last function getminTop this function, the optimization is still a lot of points, I would not bother to write. . . It all goes to optimize it.

Guess you like

Origin www.cnblogs.com/tjpeng/p/12534804.html