Python:第(前)K大(小)数问题

第(前)K大数问题

指的是在长度为n(n>=k)的乱序数组中找出从大到小顺序的第(前)k个数的问题。

算法思路

  1. 假设数组长度为N,首先取前K个数,构建最小堆
  2. 将剩余N-K个元素,依次与堆顶元素进行比较,若大于堆顶元素,则替换,并重新为最小堆。

代码

# 构建最小堆
    def min_heap(self, parent, heap):
        child = 2 * parent + 1
        while child < len(heap):
            if child+1 < len(heap) and heap[child+1] < heap[child]:
                child = child+1
            if heap[parent] <= heap[child]:
                break
            heap[parent],heap[child] = heap[child],heap[parent]
            parent, child = child, 2 * child + 1
        return heap
# 找到前k大数
def find_max_kth(self):
        if self.k > len(self.array):
            return None
        heap = self.array[:self.k]
        for i in range(self.k, -1, -1):
            self.min_heap(i, heap)
        for j in range (self.k, len(self.array)):
            if self.array[j] > heap[0]:
                heap[0] = self.array[j]
                self.min_heap(0,heap)
        return heap[0]

第(前)K小数问题

指的是在长度为n(n>=k)的乱序数组中找出从小到大顺序的第(前)k个数的问题。

算法思路

  1. 假设数组长度为N,首先取前K个数,构建最大堆
  2. 将剩余N-K个元素,依次与堆顶元素进行比较,若小于堆顶元素,则替换,并重新为最大堆。
# 构建最大堆
    def max_heap(self, parent, heap):
        child = 2 * parent + 1
        while child < len(heap):
            if child+1 < len(heap) and heap[child+1] > heap[child]:
                child = child+1
            if heap[parent] >= heap[child]:
                break
            heap[parent],heap[child] = heap[child],heap[parent]
            parent, child = child, 2 * child + 1
        return heap
# 找到前k小数        
def find_min_kth(self):
        if self.k > len(self.array):
            return None
        heap = self.array[:self.k]
        for i in range(self.k, -1, -1):
            self.max_heap(i, heap)
        for j in range (self.k, len(self.array)):
            if self.array[j] < heap[0]:
                heap[0] = self.array[j]
                self.max_heap(0,heap)
        return heap[0]

完整代码

class Find_Kth(object):

    def __init__(self, k, array):
        self.k = k
        self.array = array

# 构建最小堆
    def min_heap(self, parent, heap):
        child = 2 * parent + 1
        while child < len(heap):
            if child+1 < len(heap) and heap[child+1] < heap[child]:
                child = child+1
            if heap[parent] <= heap[child]:
                break
            heap[parent],heap[child] = heap[child],heap[parent]
            parent, child = child, 2 * child + 1
        return heap

# 构建最大堆
    def max_heap(self, parent, heap):
        child = 2 * parent + 1
        while child < len(heap):
            if child+1 < len(heap) and heap[child+1] > heap[child]:
                child = child+1
            if heap[parent] >= heap[child]:
                break
            heap[parent],heap[child] = heap[child],heap[parent]
            parent, child = child, 2 * child + 1
        return heap

    def find_max_kth(self):
        if self.k > len(self.array):
            return None
        heap = self.array[:self.k]
        for i in range(self.k, -1, -1):
            self.min_heap(i, heap)
        for j in range (self.k, len(self.array)):
            if self.array[j] > heap[0]:
                heap[0] = self.array[j]
                self.min_heap(0,heap)
        return heap[0]

    def find_min_kth(self):
        if self.k > len(self.array):
            return None
        heap = self.array[:self.k]
        for i in range(self.k, -1, -1):
            self.max_heap(i, heap)
        for j in range (self.k, len(self.array)):
            if self.array[j] < heap[0]:
                heap[0] = self.array[j]
                self.max_heap(0,heap)
        return heap[0]

def main():
     orignal_list =[12,4,6,33,54,9,3]
     k = 4
     obj = Find_Kth(k, orignal_list)
     print("第k大数为:", obj.find_max_kth())
     print("第k小数为:", obj.find_min_kth())


 if __name__ == "__main__":
     main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值