美文网首页
ZXAlgorithm - C8 Hash and Heap

ZXAlgorithm - C8 Hash and Heap

作者: 左心Chris | 来源:发表于2019-11-21 18:55 被阅读0次

Hash
Theorem
HashMap,HashSet,HashTable,
HashFunction 31
Open & close
D: LinkedList
D: null vs available
Rehashing
D: negative number process
Application
LRU
D: HashMap and Node(LinkedList)
Heap
PriorityQueue
TreeMap

0 Outline

这一章的脉络是什么?
介绍Queue和Stack的用处,引申到Hash,然后是介绍hash的基本性质,相关题目如LRU Cache,然后介绍Heap和相关性质,了解TreeMap和PriorityQueue和TreeSet

1 Data structure

Queue, stack, Hash是什么,有哪些操作,复杂度是?
Queue: BFS Push Pop Top都是O(1)
Stack: DFS with no recursion Push Pop Top都是O(1)
Hash: Insert Find Delete都是O(1)

2 Hash

Theorem

什么是Hash?

Application

LRU cache是什么?
可以实现两个操作,获取键值和设置键值,当获取不到的时候,返回-1,设置键值如果cache满了,要把最不常用的那个键值删了
什么是python defaultdict和orderedDict?
https://docs.python.org/zh-cn/3/library/collections.html
defaultdict提供一个默认值得dict,OrderedDict创建记住键值 最后 插入顺序的有序字典变体很简单。 如果新条目覆盖现有条目,则原始插入位置将更改并移至末尾
Python OrderDict()怎么简单实现?
使用move_to_end和popitem,根据last参数来决定先进先出
https://docs.python.org/zh-cn/3/library/functools.html#functools.lru_cache

LRU Cache
https://leetcode.com/problems/lru-cache/
https://leetcode.com/problemset/all/?search=LRU%20Cache
https://www.jiuzhang.com/solutions/?search=LRU%20Cache

3 Heap

Heap是什么?
支持操作O(log N) Add / O(log N) Remove / O(1) 查询Min or Max,本质是二叉树,
具体在各个语言的实现?
Java: PriorityQueue
C++: priority_queue
Python: heapq 父节点的只小于或等于子节点,具体使用其实是用列表实现的https://blog.csdn.net/jamfiy/article/details/88185512
Priority Queue是什么?
Priority Queue和Heap有什么区别吗?如果是JAVA自带的Priority Queue的话,答案是有的(C++的Priority Queue可能也类似,具体要确认一下)。
具体的区别是在remove操作中,Priority Queue的时间复杂度是O(n),而Heap是O(logn)。因为Priority Queue需要找到这个数据,需要O(n)的时间,而Heap借助了HashMap,所以只需要O(1)的时间就可以找到。那为什么Priority Queue不用HashMap呢?这个就是JAVA提供的函数里面没有加上这一块而已,如果自己编程就也可以是O(logn)。
https://blog.csdn.net/roufoo/article/details/80638476
构建一个heap的复杂度,和遍历一个heap的复杂度?
构建堆,自下而上,复杂度是O(n)
遍历是O(nlogn)
Python heapq有哪些操作?
heappush(heap, item) heappop(heap) heappushpop(heap, item) heapify(x)
heapreplace(heap, item)

PriorityQueue vs Heap

UglyNumber II
https://leetcode.com/problemset/all/?search=Ugly%20Number
https://www.jiuzhang.com/solutions/?search=Ugly%20Number
简单来说就是来一个导入一个,来一个导入一个,每次取个最小的,通过这个最小的再加入新的
S: p2, p3, p5, uglys
D: Use p2, p3, p5 on same queue O(n)
怎样得到最后一个数,应该是把所有之前的数乘以2,或3或5,找出最小
HashMap + heap(poll min and add 23*5)
Top K largest Number II
https://www.jiuzhang.com/solutions/?search=Top%20k%20Largest%20Number
简单来说就是来一个存一个,如果过量了就heappop出来
Use priorityQueue: add();peek();poll(); clear();
Queue is interface but stack is class
Queue is implemented by PriorityQueue andLinkedList
Merge K sorted Lists
https://leetcode.com/problemset/all/?search=Merge%20K%20Sorted%20Lists
https://www.jiuzhang.com/solutions/?search=Merge%20K%20Sorted%20Lists
Heap & PriorityQueue
Divide and conquer
Merge two by two

TreeMap

https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
https://leetcode.com/problems/merge-k-sorted-lists/discuss/10513/108ms-python-solution-with-heapq-and-avoid-changing-heap-size

TreeMap是什么?
与HashMap相比,TreeMap是一个能比较元素大小的Map集合,会对传入的key进行了大小排序。其中,可以使用元素的自然顺序,也可以使用集合中自定义的比较器来进行排序;基于红黑树实现
https://www.jianshu.com/p/2dcff3634326
Java document: Key value pairs(ascending order by key)
Get();set(key,value); ceilingEntry,ceilingKey

Related questions

The Skyline Problem
https://leetcode.com/problemset/all/?search=The%20Skyline%20Problem
https://www.jiuzhang.com/solutions/?search=building%20outline
Top K Frequent Words
https://leetcode.com/problemset/all/?search=Top%20K%20Frequent%20Words
https://www.jiuzhang.com/solutions/?search=Top%20K%20Frequent%20Words
用heapq实现,先把每一个的第一个载入,每载入一个取出下一个,思路还是比较多路的最小数

Python Queue模块里面的PriorityQueue是什么
A typical pattern for entries is a tuple in the form: (priority_number, data).

from Queue import PriorityQueue

class Solution(object):
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        head = point = ListNode(0)
        q = PriorityQueue()
        for l in lists:
            if l:
                q.put((l.val, l))
        while not q.empty():
            val, node = q.get()
            point.next = node
            point = point.next
            node = node.next
            if node:
                q.put((node.val, node))
        return head.next

相关文章

网友评论

      本文标题:ZXAlgorithm - C8 Hash and Heap

      本文链接:https://www.haomeiwen.com/subject/exeqictx.html