• Dylan
    2018-11-08
    把这个课程推荐给朋友,他今天跟我说,他去今日头条面试,3个算法题有2个在这里出现,一个是这道。一个是之前 两个数和 的问题,还要当场写代码~
    
     30
  • 王小李
    2018-10-15
    老师,这边第一种情况(使用数组的情况),因为数组本身是排好的,是不是只对新元素进行插排的效果会比使用快排的效果更好?
     2
     12
  • linxs
    2018-10-15
    后续会讲一下AVL嘛
    
     5
  • Fred
    2018-10-22
    对数的概念
    https://wenku.baidu.com/view/29e2e92bef06eff9aef8941ea76e58fafab04511.html
    时间复杂度的底数
    https://blog.csdn.net/bengxu/article/details/80320546
    我已经忘光了.....
    
     2
  • 王磊
    2018-10-16
    遇到过流式数据找中值的面试,最好能给讲解下。
     1
     2
  • firstblood
    2019-01-08
    上面那个Go语言的有问题,他不能保证一直都是满足小顶堆的限制的,因为push的时候调用的是自己的方法而不是堆的方法,所以不会检测。下面是我写的
    type IntHead []int
    func (i IntHead) Len() int { return len(i)}
    func (In IntHead) Less(i ,j int) bool {
        return In[i] < In[j]}
    func (In IntHead) Swap(i, j int) {
        In[i], In[j] = In[j], In[i]
    }
    func (In *IntHead) Push(x interface{}) {
        *In = append(*In, x.(int))
    }
    func (In *IntHead) Pop() interface{} {
        old := *In
        n := len(old)
        x := old[n -1]
        *In = old[0:n-1]
        return x
      
    }
    type KthLargest struct {
        K int
        H *IntHead
    }


    func Constructor(k int, nums []int) KthLargest {
        min := &IntHead{}
        
        heap.Init(min)
        kth := KthLargest{K:k, H:min}
        for _, num := range nums {
            kth.Add(num)
        }
        return kth
    }


    func (this *KthLargest) Add(val int) int {
        if this.H.Len() == 1 && (*this.H)[0] == 0 && val == -1 {
             min := &IntHead{-1,0}
             heap.Init(min)
            this.H = min
        }
        if this.H.Len() < this.K {
            heap.Push(this.H, val)
        } else if (*this.H)[0] < val {
            heap.Pop(this.H)
        heap.Push(this.H, val)
        }
        return (*this.H)[0]
    }

    }
    展开

    作者回复: 赞👍🏻

    
     1
  • niubility
    2018-10-30
    这个题面试真遇到过,当时回答的也是 维护一个 k 长度的数组,每次加入都进行排序,先跟最小的比,如果比最小的都小就直接抛弃.比最小的大就挨个与数组中元素比较. 时间复杂度是 n*k

    作者回复: 你提议的算法就是相对比较简单朴素的算法,O(n*k) 速度太慢,必须要 O(n) 的算法才能通过面试的。

    
     1
  • Quantum
    2018-10-21
    老师随机选择也可以介绍下? 会比堆更优的吧
    
     1
  • 简单爱
    2018-10-11
    小顶堆这个思路有点巧妙,前人是怎么想出来的
    
     1
  • 肥low
    2020-01-29
    老师,堆排序的时间复杂度不是O(k*logk)么?我查的不对?
    
    
  • Zereker
    2019-12-21
    Go 简单版本:
    package main

    import (
        "container/heap"
        "fmt"
    )

    type IntSlice []int

    func (s *IntSlice) Len() int {
        return len(*s)
    }

    func (s *IntSlice) Less(i, j int) bool {
        return (*s)[i] < (*s)[j]
    }

    func (s *IntSlice) Swap(i, j int) {
        (*s)[i], (*s)[j] = (*s)[j], (*s)[i]
    }

    func (s *IntSlice) Push(x interface{}) {
        *s = append(*s, x.(int))
    }

    func (s *IntSlice) Pop() interface{} {
        length := s.Len()

        last := (*s)[length-1]
        *s = (*s)[:length-1]
        return last
    }

    type KthLargest struct {
        minHeap IntSlice
        kth int
    }

    func Constructor(k int, nums []int) KthLargest {
        kthLargest := KthLargest{
            minHeap: make(IntSlice, 0),
            kth: k,
        }

        for _, v := range nums {
            kthLargest.Add(v)
        }

        return kthLargest
    }

    func (k *KthLargest) Add(val int) int {
        if k.minHeap.Len() < k.kth { // 如果还没有满, 无论如何插入元素
            heap.Push(&k.minHeap, val)
        } else {
            if val > k.minHeap[0] { // 判断插入元素 大于 当前元素里面的最小值, 则插入
                heap.Pop(&k.minHeap)
                heap.Push(&k.minHeap, val)
            }
        }

        return k.minHeap[0]
    }

    func main() {
        kthLargest := Constructor(3, []int{
            4, 5, 8, 2,
        })

        fmt.Println(kthLargest.Add(3))
        fmt.Println(kthLargest.Add(5))
        fmt.Println(kthLargest.Add(10))
        fmt.Println(kthLargest.Add(9))
        fmt.Println(kthLargest.Add(4))
    }
    展开
    
    
  • zhimin
    2019-11-09
    PHP 最小堆实现第K大元素
    class MinHeap extends SplHeap
    {
        //用最小堆存储第K大元素
        public function compare( $value1, $value2 ) {
            return ( $value2 - $value1 );
      }
    }
    class KthLargest {
        public $k;
        public $heap;
        /**
         * @param Integer $k
         * @param Integer[] $nums
         */
        function __construct($k, $nums) {
            $this->k = $k;
            $this->heap = new MinHeap();
            foreach($nums as $num)
            {
                $this->add($num);
            }
        }
      
        /**
         * @param Integer $val
         * @return Integer
         */
        function add($val) {
            if($this->heap->count()<$this->k){
                $this->heap->insert($val);
            }else{
                if($val>$this->heap->top()){
                    $this->heap->insert($val);
                    $this->heap->extract();
                }
            }
            return $this->heap->top();
        }
    }
    展开

    作者回复: nice

    
    
  • 未来已来
    2019-09-20
    老师那个构造函数有点问题,可改为(仅供参考):
    ```
    public KthLargest(int k, int[] nums) {
            this.k = k;
            heap = new PriorityQueue<Integer>();
            for (int i = 0; i < nums.length; i++) {
                if (i < k) {
                    heap.offer(nums[i]);
                    continue;
                }
                else {
                    if (nums[i] > heap.peek()) {
                        heap.poll();
                        heap.offer(nums[i]);
                        continue;
                    }
                }
            }
        }
    ```
    展开
    
    
  • 战五渣
    2019-05-24
    平时都用c#,居然不自带优先队列数据结构
    
    
  • Geek_maxwell
    2019-04-27
    老师,第一个使用排序的方案,效率不应该是nklogk,而应该是nk+klogk, 这了排序使用链表进行一次排序后,以后直接就是有序数组不断插入,并且删除头就行,不需要排序了
    
    
  • 坤
    2019-03-11
    https://docs.python.org/2/library/heapq.html python 原生的heapq.nlarges() 在leetcode 上运行时超时,请问是本身优化的不好吗?
    
    
  • Aliliin
    2019-03-03
    这连着几道题了。用PHP做了好几天,也没做出来。
    
    
  • 唐小曼要变凹凸曼
    2019-01-27
    用数组来做 每次新进一个数都是在已经排好序的K个数中插入一个新的数所以整体的时间复杂度应该是n*k?
    
    
  • 爱学习的杨轶🙉🙊...
    2019-01-24
    为何我用老师的Java代码copy到leetcode去通过不了……
    
    
  • 一弯年轮
    2019-01-18
    老师,这题python该怎么实现, Leecode中好像不支持import queue,该怎么办?
    
    
我们在线,来聊聊吧