gitalk icon indicating copy to clipboard operation
gitalk copied to clipboard

一道面试题: Top K 问题

Open utterances-bot opened this issue 1 year ago • 3 comments

一道面试题: Top K 问题

最近在招一个Go开发工程师,面试中时候我会问一个Top K的问题,这个问题是一个经典的面试题。 有时候我不会要求面试者写出答案,首先我听一下他的思想,如果写代码困难的话我都允许可以上网查标准库的文档,看看heap的用法。 相对来说比Redis的作者anti

https://colobu.com/2024/03/04/top-k-problem/

utterances-bot avatar Mar 07 '24 08:03 utterances-bot

提供一个比较”省事“的Golang堆初始化

type hp struct{ sort.IntSlice }
func (h hp) Less(i, j int) bool  { return h.IntSlice[i] > h.IntSlice[j] }
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

Jun10ng avatar Mar 07 '24 08:03 Jun10ng

实现了一个泛型的通用的堆 https://pkg.go.dev/github.com/smallnest/[email protected]/container/heap#BinHeap

以及把一个已存在的slice转化成一个堆

smallnest avatar Mar 07 '24 09:03 smallnest

最近刚好再看 heap ,感觉还蛮好用的呀,比较灵活。

hxzhouh avatar Mar 08 '24 01:03 hxzhouh