在FAANG面试中破解堆算法

In FAANG company interview, Candidates always come across heap problems. There is one question they do like to ask — Top K. Because these companies have a huge dataset and they can’t always go through all the data. Finding tope data is always a good option. In this article, I would like to share my methodology on handling heap interview problems. Also, I’ll share 2 typical heap algorithm problems — Top K Frequent, Merge K arrays.

在FAANG公司面试中,候选人总是遇到堆砌问题。 他们想问一个问题-TopK。因为这些公司拥有庞大的数据集,而且不能总是遍历所有数据。 查找tope数据始终是一个不错的选择。 在本文中,我想分享我的方法来处理堆访谈问题。 另外,我将分享2个典型的堆算法问题-排在前K个,合并K个数组。

什么是堆? (What is Heap?)

Heap is a complete Tree-like data structure. It has a parent node, left child node and right child node. For the interview, the min-heap and max-heap are very common to see.

堆是一个完整的树状数据结构。 它具有父节点,左子节点和右子节点。 对于采访来说,最小堆和最大堆很常见。

Max-Heap: In a Max-Heap the value present at the parent node must be greatest among the values present at all of its children.

最大堆 :在最大堆中,父节点上存在的值必须在其所有子节点上存在的值中最大。

Min-Heap: In a Min-Heap the value present at the parent node must be minimum among the values present at all of its children.

最小堆 :在最小堆中,父节点上存在的值必须在其所有子节点上存在的值中最小。

堆方法 (Heap methodology)

This is the methodology to help think about the solution:

这是帮助考虑解决方案的方法:

Image for post
Image by author图片作者

For most of the popular data structure problems. The feature is common: we have to do sort-like steps. We can sort all the elements: heapsort. We can sort just several elements: top-k. The elements should be comparable. Otherwise, we may have to push some comparable value along with the elements.

对于大多数流行的数据结构问题。 该功能很常见:我们必须执行类似排序的步骤。 我们可以对所有元素进行排序:heapsort。 我们可以对几个元素进行排序:top-k。 元素应该是可比较的。 否则,我们可能不得不将一些可比较的价值与要素一起推向市场。

Push: What we push some comparable elements such as int. Sometimes I have to push a comparable object. No matter what we push, we are doing it for comparison and heapify.

推送 :我们推送一些可比较的元素,例如int。 有时我必须推动一个类似的对象。 无论我们推动什么,我们都在进行比较和堆化。

Heapify: Heapify is the process of converting a binary tree into a Heap data structure by rearranging the nodes in the binary tree to a max-heap or min-heap.

Heapify :Heapify是通过将二叉树中的节点重新排列为max-heap或min-heap来将二叉树转换为Heap数据结构的过程。

In case one of your interview questions is implementing heapify, Here I attached the code:

如果您的面试问题之一是实现heapify,请在此处附加代码:

In this case, we have to push the node to the heap. As heap’s representation is an array. It appends the new node to the end and heapify it. Here is the code for a max-heap:

在这种情况下,我们必须将节点推送到堆。 由于堆的表示是一个数组。 它将新节点附加到末尾并对其进行堆放。 这是最大堆的代码:

For each insert, it takes O (log K) time to heapify the node, K is the heap size.

对于每个插入,需要O(log K)时间来堆满节点,K是堆大小。

Pop: Now it’s time to get what we want. Here we have two cases:

Pop :现在是时候获得我们想要的东西了。 这里有两种情况:

  • We use the value on the root of the heap. If needed, we can use heap[0] to check the top one if we have some extra conditions in the question. After we get the value, we can put it in an array or do extra work as the question requested.

    我们在堆的根上使用该值。 如果需要,可以在问题中使用一些额外的条件,使用heap [0]检查最上面的一个。 获得值后,我们可以将其放入数组中,也可以根据要求进行其他工作。

  • In other cases, we need the rest of the values in the Heap. We can loop through the heap and return it.

    在其他情况下,我们需要堆中的其余值。 我们可以遍历堆并返回它。

实践 (Practices)

Here I provide solutions with python. For most of the Algorithm problems, it’s not just one solution. It’s better to give different solutions, analyse the time/space complexity and move to the best one.

在这里,我提供了python解决方案。 对于大多数算法问题,这不仅仅是一种解决方案。 最好提供不同的解决方案,分析时间/空间的复杂性,然后转向最佳解决方案。

In python, we have heapq library to help us operate the heap properly. It has nlargest and nsmallest which we can use to solve top-k type problem in one line of code. However, to ensure you can understand better about Heap, let’s go step by step.

在python中,我们有heapq库来帮助我们正确地操作堆。 它具有nlargensmallest ,可用于在一行代码中解决top-k类型的问题。 但是,为了确保您可以更好地了解Heap,让我们逐步进行。

前K频 (Top K Frequent)

Here is the reference question on leetcode. We just find the top K frequent values in an array.

这是有关leetcode的参考问题 。 我们只是在数组中找到前K个频繁值。

Here are the steps from the code:

这是代码中的步骤:

  • Convert the array to a new array where the element is a tuple with value and frequency.

    将数组转换为新数组,其中元素是具有值和频率的元组。
  • Push the tuple elements to the k-size heap. The heap heapify automatically whenever a new node has been pushed.

    将元组元素推入k大小的堆。 每当推送新节点时,堆都会自动堆化。
  • After we go through all the elements with the heap, we loop the rest of the heap to find our target values.

    在遍历了堆中的所有元素之后,我们循环其余堆以找到目标值。

If you are pushing an object, you have to ensure the object is comparable. If you push a tuple, the heap compare the first element while doing heapify, if ties happened, it will compare the next value.

如果要推动对象,则必须确保该对象具有可比性。 如果推入一个元组,堆将在执行heapify时比较第一个元素如果发生联系,它将比较下一个值

合并K个数组 (Merge K arrays)

Here is the reference question on leetcode. We have to Merge k sorted linked lists and return it as one sorted list. Here is the solution with Heap:

这是有关leetcode的参考问题 。 我们必须合并k个排序的链表,并将其作为一个排序表返回。 这是堆的解决方案:

Here are the steps from the code:

这是代码中的步骤:

  • We build an array with the first node and its value from each ListNode in lists. Then we heapify it to be a min-heap.

    我们使用列表中每个ListNode的第一个节点及其值构建一个数组。 然后我们将其堆成最小堆。
  • We use the peek node in the heap to build the output ListNode.

    我们使用堆中的peek节点构建输出ListNode。
  • If the node not ends, we use heapreplace which means we pop and return the smallest item from the heap, and also push the new item.

    如果节点未结束,则使用heapreplace,这意味着我们从堆中弹出并返回最小的项,并推送新

Here I would like to explain another solution to this question with the Priority Queue.

在这里,我想用“优先级队列”解释这个问题的另一种解决方案。

Priority Queues are abstract data structures where each data/value in the queue has a certain priority. Priority Queue is an extension of the queue with the following properties.1, An element with high priority is dequeued before an element with low priority.2, If two elements have the same priority, they are served according to their order in the queue.

优先级队列是抽象的数据结构,其中队列中的每个数据/值都有特定的优先级。 Priority Queue是具有以下属性的队列的扩展:1,将高优先级的元素排在优先级低的元素之前。2,如果两个元素具有相同的优先级,则根据它们在队列中的顺序进行服务。

Here are the steps from the code:

这是代码中的步骤:

  • From each element in the List, we find the first node with its value in the ListNode, put it into the Priority Queue. We need the node value because we need it for comparison in the Priority Queue.

    从List中的每个元素中,我们在ListNode中找到具有其值的第一个节点,并将其放入Priority Queue。 我们需要节点值,因为在优先级队列中需要进行比较。
  • Then, we get the element from the Priority Queue (the element with the smallest value), link it to the output ListNode.

    然后,我们从Priority Queue中获得元素(值最小的元素),并将其链接到输出ListNode。
  • We move the node to the next, if it exists, we put it into the Priority Queue. After all these steps, we return the output ListNode which is the head.next.

    我们将节点移到下一个节点(如果存在),将其放入优先级队列。 完成所有这些步骤之后,我们返回输出ListNode,它是head.next。

最后的话 (Final Words)

After you read this article, you may think heap is not as hard as you thought with this methodology. However, we may have other data structure comes up with Heap such as the LinkedList. Therefore, we still need to practise and master the basic data structure API with the language you preferred.

阅读本文之后,您可能会认为堆并不像您对这种方法所想象的那样难。 但是,Heap可能还会提供其他数据结构,例如LinkedList。 因此,我们仍然需要使用您喜欢的语言来练习和掌握基本的数据结构API。

Hope you learn something from this article. If you are interested in reading any of my other articles, you are welcome to check them out in my profile. Best wishes for your job hunting!

希望您能从本文中学到一些东西。 如果您有兴趣阅读我的其他文章,欢迎您在我的个人资料中查看。 祝您工作愉快!

翻译自: https://medium.com/dev-genius/a-methodology-to-ace-the-heap-algorithms-question-c7063b2ba7fd

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388900.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android webView的缓存机制和资源预加载

android 原生使用WebView嵌入H5页面 Hybrid开发 一、性能问题 android webview 里H5加载速度慢网络流量大 1、H5页面加载速度慢 渲染速度慢 js解析效率 js本身的解析过程复杂、解析速度不快,前端页面设计较多的js代码文件 手机硬件设备的性能 机型多,…

mysql springboot 缓存_Spring Boot 整合 Redis 实现缓存操作

摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!『 产品没有价值,开发团队再优秀也无济于事 – 《启示录》 』本文提纲一、缓存的应用场景二、更新缓存的策略三、运行 springboot-mybatis-redis 工程…

http压力测试工具及使用说明

http压力测试工具及使用说明 转 说明:介绍几款简单、易使用http压测工具,便于研发同学,压测服务,明确服务临界值,寻找服务瓶颈点。 压测时候可重点以下指标,关注并发用户数、TPS(每秒事务数量&a…

itchat 道歉_人类的“道歉”

itchat 道歉When cookies were the progeny of “magic cookies”, they were seemingly innocuous packets of e-commerce data that stored a user’s partial transaction state on their computer. It wasn’t disclosed that you were playing a beneficial part in a muc…

使用Kubespray部署生产可用的Kubernetes集群(1.11.2)

Kubernetes的安装部署是难中之难,每个版本安装方式都略有区别。笔者一直想找一种支持多平台 、相对简单 、适用于生产环境 的部署方案。经过一段时间的调研,有如下几种解决方案进入笔者视野: 部署方案优点缺点Kubeadm官方出品部署较麻烦、不够…

android webView 与 JS交互方式

webView 与JS交互 Android调用JS代码的方法有: 通过WebView的loadUrl()通过WebView的evaluateJavascript() 对于JS调用Android代码的方法有3种: 通过WebView的addJavascriptInterface(&…

matlab软件imag函数_「复变函数与积分变换」基本计算代码

使用了Matlab代码,化简平时遇到的计算问题,也可以用于验算结果来自211工科专业2学分复变函数与积分变换课程求复角主值sym(angle(待求复数))%公式 sym(angle(1sqrt(3)*i))%举例代入化简将 代入关于z的函数f(z)中并化解,用于公式法计算无穷远点…

数据科学 python_为什么需要以数据科学家的身份学习Python的7大理由

数据科学 pythonAs a new Data Scientist, you know that your path begins with programming languages you need to learn. Among all languages that you can select from Python is the most popular language for all Data Scientists. In this article, I will cover 7 r…

[luoguP4142]洞穴遇险

https://www.zybuluo.com/ysner/note/1240792 题面 戳我 解析 这种用来拼接的奇形怪状的东西,要不就是轮廓线\(DP\),要不就是网络流。 为了表示奇数点(即\((xy)\%21\))的危险值,把该点拆为两个点,连一条边长…

飞信虚拟机

做完了一个图片处理软件,突然想到上次上网看到C#程序脱离.NET FRAMEWORK运行的文章,于是决定自己动手试一下。 之前看到有用别的方法来实现的,但我还是选择了现在比较流行的软件飞信中带的VMDotNet,也就是所谓的.NET FRAMEWORK虚拟机吧。相信有很多人也已…

django的contenttype表

https://blog.csdn.net/aaronthon/article/details/81714496 这篇文章已经非常详细了,供自己以后忘了...回看...... 总结: 当一张表和多个表FK关联,并且多个FK中只能选择其中一个或其中n个时,可以利用contenttype,固定用三个字段…

视频播放问题和提高性能方案

1.Five symptoms of poor video performance 1.1 视频加载缓慢 ​Perceived Wait Time Time to first frame (TTFF): ​ 播放开始所需的adaptive bitrate(ABR)流媒体段的数量。(我们稍后将对此进行更详细的讨论。) ​ 视频请求发送到视频加载之间的时间(即接收到足够的数据…

rabbitmq 不同的消费者消费同一个队列_RabbitMQ 消费端限流、TTL、死信队列

消费端限流1. 为什么要对消费端限流假设一个场景,首先,我们 Rabbitmq 服务器积压了有上万条未处理的消息,我们随便打开一个消费者客户端,会出现这样情况: 巨量的消息瞬间全部推送过来,但是我们单个客户端无法同时处理这…

动量策略 python_在Python中使用动量通道进行交易

动量策略 pythonMost traders use Bollinger Bands. However, price is not normally distributed. That’s why only 42% of prices will close within one standard deviation. Please go ahead and read this article. However, I have some good news.大多数交易者使用布林…

css3 变换、过渡效果、动画

1 CSS3 选择器 1.1 基本选择器 1.2 层级 空格 > .itemli ~ .item~p 1.3 属性选择器 [attr] [attrvalue] [attr^value] [attr$value] [attr*value] [][][] 1.4 伪类选择器 :link :visited :hover :active :focus :first-child .list li:first-child :last-chi…

webservice 启用代理服务器

您会发现你写完了一个webservice在调用的时候发现怎也没办法调用,一个简单的webservice怎么不能使用,一肚子的怨恨,哈哈您可能没有为webservice设置代理。 下面就给您写个调用的用例和大家分享下。其实很简单,但是你没有想到的时…

mysql常用的存储引擎_Mysql存储引擎

什么是存储引擎?关系数据库表是用于存储和组织信息的数据结构,可以将表理解为由行和列组成的表格,类似于Excel的电子表格的形式。有的表简单,有的表复杂,有的表根本不用来存储任何长期的数据,有的表读取时非…

android studio设计模式和文本模式切换

转载于:https://www.cnblogs.com/judes/p/9437104.html

高斯模糊为什么叫高斯滤波_为什么高斯是所有发行之王?

高斯模糊为什么叫高斯滤波高斯分布及其主要特征: (Gaussian Distribution and its key characteristics:) Gaussian distribution is a continuous probability distribution with symmetrical sides around its center. 高斯分布是连续概率分布,其中心周…

C# webbrowser 代理

百度,google加自己理解后,将所得方法总结一下: 方法1:修改注册表Software//Microsoft//Windows//CurrentVersion//Internet Settings下 ProxyEnable和ProxyServer。这种方法适用于局域网用户,拨号用户无效。 1p…