Skip to content

LeetCode 讀書會第 44 次聚會 #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2022
Merged

LeetCode 讀書會第 44 次聚會 #37

merged 3 commits into from
Jun 13, 2022

Conversation

ytyubox
Copy link
Member

@ytyubox ytyubox commented May 30, 2022

  1. 項目: 第 44 次聚會
  2. 目的: 線上一起寫題目, 由有想法的人帶領, 先解題, 再看該題有趣的解法
  3. 時間: 05/31 (二) 20:00 ~ 21:00
  4. 地點: google meet 線上 (前 10 分鐘預備鏈接)
  5. 解題項目: Heap
  6. 共筆: GitHub https://github.com/programmingbookclub/Leetcode-club
  7. 備註: 這次做 MEDIUM 1642:Furthest Building You Can Reach,如果提前講完,可以提前看 295 (hard)

@ytyubox
Copy link
Member Author

ytyubox commented May 31, 2022

今天再次點開了 https://github.com/azl397985856/leetcode/blob/master/thinkings/heap-2.md#%E6%8A%80%E5%B7%A7%E4%B8%89---%E4%BA%8B%E5%90%8E%E5%B0%8F%E8%AF%B8%E8%91%9B
講到,技巧三 - 事后小诸葛

这个技巧指的是:当从左到右遍历的时候,我们是不知道右边是什么的,需要等到你到了右边之后才知道。

如果想知道右边是什么,一种简单的方式是遍历两次,第一次遍历将数据记录下来,当第二次遍历的时候,用上次遍历记录的数据。这是我们使用最多的方式。不过有时候,我们也可以在遍历到指定元素后,往前回溯,这样就可以边遍历边存储,使用一次遍历即可。具体来说就是将从左到右的数据全部收集起来,等到需要用的时候,从里面挑一个用。如果我们都要取最大值或者最小值且极值会发生变动, 就可使用堆加速。直观上就是使用了时光机回到之前,达到了事后诸葛亮的目的。

@ytyubox
Copy link
Member Author

ytyubox commented May 31, 2022

Heap?
A heap is really good at basic ordering and keeping track of max and mins. This is actually interesting- if you had two heaps, you could keep track of the bigger half and the smaller half of the elements. The bigger half is kept in a min heap, such that the smallest element in the bigger half is atthe root. The smaller half is kept in a max heap, such that the biggest element of the smaller half is at the root. Now, with these data structures, you have the potential median elements at the roots. If the heaps are no longer the same size, you can quickly "rebalance" the heaps by popping an element off the one
heap and pushing it onto the other.

314776

@ytyubox ytyubox merged commit 61f31e0 into main Jun 13, 2022
@ytyubox ytyubox deleted the 44 branch September 20, 2022 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant