zoukankan      html  css  js  c++  java
  • 703. Kth Largest Element in a Stream(heapq)

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

    Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.

    Example:

    int k = 3;
    int[] arr = [4,5,8,2];
    KthLargest kthLargest = new KthLargest(3, arr);
    kthLargest.add(3); // returns 4
    kthLargest.add(5); // returns 5
    kthLargest.add(10); // returns 5
    kthLargest.add(9); // returns 8
    kthLargest.add(4); // returns 8

    Note:
    You may assume that nums' length ≥ k-1 and k ≥ 1.

    Solution1:(TLE)

    class KthLargest:
    
        def __init__(self, k, nums):
            """
            :type k: int
            :type nums: List[int]
            """
            self.nums = nums
            self.k = k
    
        def add(self, val):
            """
            :type val: int
            :rtype: int
            """
            self.nums.append(val)
            return sorted(self.nums)[-self.k]
    
    # Your KthLargest object will be instantiated and called as such:
    # obj = KthLargest(k, nums)
    # param_1 = obj.add(val)
    

    Solution2:(TLE)

    class KthLargest:
    
        def __init__(self, k, nums):
            """
            :type k: int
            :type nums: List[int]
            """
            self.nums = sorted(nums)[-k:]
            self.k = k
            # print(self.nums)
    
        def add(self, val):
            """
            :type val: int
            :rtype: int
            """
            if len(self.nums)<k:
                self.nums.append(val)
                self.nums.sort()
            else:
                for i in range(len(self.nums)-1,-1,-1):
                    if val>self.nums[i]:
                        for j in range(0,i):
                            self.nums[j] = self.nums[j+1]
                        self.nums[i] = val
                        break
            # print(self.nums)
            return self.nums[0]
    

    这个也TLE过分了

    Solution3:

    import heapq
    class KthLargest(object):
    
        def __init__(self, k, nums):
            self.pool = nums
            self.k = k
            heapq.heapify(self.pool)
            while len(self.pool) > k:
                heapq.heappop(self.pool)
    
        def add(self, val):
            if len(self.pool) < self.k:
                heapq.heappush(self.pool, val)
            elif val > self.pool[0]:
                heapq.heapreplace(self.pool, val)
            return self.pool[0]
    

    python的headp为小根堆

    heapq.heappush(heap, item)

    heapq.heappop(heap):返回 root 节点,即 heap 中最小的元素。

    heapq.heapreplace(heap,item): 替换第一个的值。

    heapq.heappushpop(heap, item):向 heap 中加入 item 元素,并返回 heap 中最小元素。

    heapq.heapify(x):Transform list into a heap, in-place, in O(len(x)) time

    heapq.merge(*iterables, key=None, reverse=False)

    heapq.nlargest(n, iterable, key=None):返回可枚举对象中的 n 个最大值,并返回一个结果集 list,key 为对该结果集的操作。

    heapq.nsmallest(n, iterable, key=None):同上相反

  • 相关阅读:
    Jenkins配置:添加用户和管理权限
    Jenkins安装与配置
    jenkins配置邮件通知
    Jenkins 配置邮件通知
    jenkins+SVN配置
    第九周学习进度
    梦断代码阅读笔记 01
    第八周学习进度
    “理了么”软件特点NABCD个人分析
    第七周学习进度
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9774947.html
Copyright © 2011-2022 走看看