由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 面试题:Data structure to find top 10 search strings
相关主题
leetcode 129An Old Question -- Top N Occurance Frequance
word ladder ii 谁给个大oj不超时的?菜鸟 贴一个 leetcode LRU Cache -- java代码,并求解疑惑。
一道电面题,分享下, 这个题应该用哪几个data structure?分别用LinkedList和HashMap构建字典树(Compact Trie)怎么做
在版上看到的G题Clone graph
LRU cache 超时, 大家帮忙看看求帮忙看看这个clone graph的解法。弄半天还是不对。 多谢!
【我自己写的LinkedList为什么总有错?】G家电面(已挂)
再问大牛们leetcode上面Linkedlist的题,Reverse Nodes in k-G问个google面试题(3)
leetcode 上的k way merge看一条L的面试题
相关话题的讨论汇总
话题: node话题: top话题: strings话题: search话题: data
进入JobHunting版参与讨论
1 (共1页)
z****3
发帖数: 782
1
Given a stream of search strings, write a function so that it returns the
top 10 in constant time.
应该怎么实现?我目前想到的是弄一个list来保存top 10,然后每次来新的就更新这个
list
求建议
U***A
发帖数: 849
2
heap
d**********6
发帖数: 4434
3
heap有问题,不pop的话只有top 1,pop的话pop出来的数据就丢失,而且pop不是O(1)的
应该要用hashmap+sorted list

【在 U***A 的大作中提到】
: heap
k*******q
发帖数: 5493
4
这是写LFU吗?

【在 z****3 的大作中提到】
: Given a stream of search strings, write a function so that it returns the
: top 10 in constant time.
: 应该怎么实现?我目前想到的是弄一个list来保存top 10,然后每次来新的就更新这个
: list
: 求建议

d**********6
发帖数: 4434
5
这样吧,一个hashmap,key是search string, value是一个node
structur ListNode{
int val
ListNode pre
ListNode next
}
node组成一个double link list,每次遇到一个search string,在hashmap里面找对应
node,更新node的val,同时都跟pre node的数值比较发现有变化的就swap
取top10的时候就从double link list的头开始取就行

)的

【在 d**********6 的大作中提到】
: heap有问题,不pop的话只有top 1,pop的话pop出来的数据就丢失,而且pop不是O(1)的
: 应该要用hashmap+sorted list

C****t
发帖数: 53
6
class Node:
def __init__(key, val)
self.key = key
self.val = val
maps[node.key] = node
class LinkedList:
# if node.key comes in again, then increase node.val by 1, delete the old
node
def remove(self, node)
# insert the updated node
def add(self,node)
# return Top K:

def topK():


【在 z****3 的大作中提到】
: Given a stream of search strings, write a function so that it returns the
: top 10 in constant time.
: 应该怎么实现?我目前想到的是弄一个list来保存top 10,然后每次来新的就更新这个
: list
: 求建议

n*****g
发帖数: 178
7

)的
top K 问题好像还是用heap比较好吧

【在 d**********6 的大作中提到】
: heap有问题,不pop的话只有top 1,pop的话pop出来的数据就丢失,而且pop不是O(1)的
: 应该要用hashmap+sorted list

d**********6
发帖数: 4434
8
你要看要求啊,你用heap
不pop的话只有top1
pop出来以后这个数值就丢失了。你后面还要继续计数的话咋办?而且pop又不是O(1)

【在 n*****g 的大作中提到】
:
: )的
: top K 问题好像还是用heap比较好吧

s*******u
发帖数: 89
9
size为10的min-heap吖,每次把最小pop的出去,最后剩在堆里的就是最大的10个
d**********6
发帖数: 4434
10
这样这个堆永远只有前10个了。第11个count是1,然后被踢出去。第11个又来,因为前
面被踢出去了,重置,count还是1,又被踢出去了。。。

【在 s*******u 的大作中提到】
: size为10的min-heap吖,每次把最小pop的出去,最后剩在堆里的就是最大的10个
s*******u
发帖数: 89
11
喵~我的意思是,hash先得到counts,counts再丢进heap....

【在 d**********6 的大作中提到】
: 这样这个堆永远只有前10个了。第11个count是1,然后被踢出去。第11个又来,因为前
: 面被踢出去了,重置,count还是1,又被踢出去了。。。

d**********6
发帖数: 4434
12
用heap始终有这个问题,pop完之后就丢失了
比如我读了100个key以后,读一次top 10,这时候heap就空了。你就没法重新追踪下去
,后面的再进来的会乱套
至少也要是一个sorted array,读取top 10的时候不需要pop出来

【在 s*******u 的大作中提到】
: 喵~我的意思是,hash先得到counts,counts再丢进heap....
a*****2
发帖数: 96
13
这不是挺好的solution吗,怎么没人顶呢

【在 d**********6 的大作中提到】
: 这样吧,一个hashmap,key是search string, value是一个node
: structur ListNode{
: int val
: ListNode pre
: ListNode next
: }
: node组成一个double link list,每次遇到一个search string,在hashmap里面找对应
: node,更新node的val,同时都跟pre node的数值比较发现有变化的就swap
: 取top10的时候就从double link list的头开始取就行
:

1 (共1页)
进入JobHunting版参与讨论
相关主题
看一条L的面试题LRU cache 超时, 大家帮忙看看
发个evernote的code challenge【我自己写的LinkedList为什么总有错?】
过去n小时的top search再问大牛们leetcode上面Linkedlist的题,Reverse Nodes in k-G
一道关于trie的题目leetcode 上的k way merge
leetcode 129An Old Question -- Top N Occurance Frequance
word ladder ii 谁给个大oj不超时的?菜鸟 贴一个 leetcode LRU Cache -- java代码,并求解疑惑。
一道电面题,分享下, 这个题应该用哪几个data structure?分别用LinkedList和HashMap构建字典树(Compact Trie)怎么做
在版上看到的G题Clone graph
相关话题的讨论汇总
话题: node话题: top话题: strings话题: search话题: data