当前位置:网站首页>一道有趣的阿里面试题
一道有趣的阿里面试题
2022-04-23 10:57:00 【Hollis Chuang】
在阿里巴巴的文化中,充满了各种武侠元素。在面试阿里时,也经常会出现一些有趣的题目。
今天,我们来看一道阿里巴巴面试的题目:
在武林中,朋友的朋友还是朋友,比如朋友关系如下表所示,那么周芷若、张无忌、韩小昭属于一个朋友圈,成昆和陈友谅属于一个朋友圈,杨逍和纪晓芙属于一个朋友圈。最终,不同朋友圈的个数是 3。
人物 | 人物 |
关系 |
周芷若 |
张无忌 | 朋友 |
张无忌 | 韩小昭 |
朋友 |
成昆 |
陈友谅 |
朋友 |
杨逍 |
纪晓芙 |
朋友 |
(1)任意给定两个人,判断他们是否属于同一个朋友圈。
(2)编程求出不同朋友圈的个数。
题目理解
首先,我们来看看周芷若、张无忌和韩小昭长什么样子吧,还是挺可爱的。
我来解释什么叫“朋友的朋友还是朋友”:周芷若和张无忌是朋友,张无忌和韩小昭是朋友。所以,他们三人是朋友,属于同一个朋友圈。趣味动图解释如下:
思路分析
在解决这个问题之前,我们得想一种合理的数据结构,然而,貌似书本上的数据结构都不合适,那怎么办呢?
我们来讲一个周芷若与并查集的故事。先来分析一下,好友关系如下:
{周芷若,张无忌}
{张无忌,韩小昭}
{成昆,陈友谅}
{杨逍,纪晓芙}
他们关系的逻辑图如下:
从上图可知,这些人属于不同的 3 个朋友圈,那么这 3 个朋友圈是怎么得出来的呢?
很显然,我们可以在每个朋友圈定义一个名义上的 leader。
如果要判断两个人是否属于一个朋友圈,只需要判断他们的 leader 是否为同一个人,这是一个查询的过程。
如果两个人是好友关系,则需要把两个人并入同一个朋友圈,这是一个合并的过程。
这一查一并的操作,就分出了最终有多少个朋友圈,对应的数据结构就是并查集。在很多笔试面试或 ACM 竞赛中,并查集屡见不鲜。
编程实现
根据上述思路分析,我们用 C++ 代码来实现并查集,并给出测试和结果。代码的注释非常详细,所以不再赘述:
#include <iostream>
#include <map>
using namespace std;
map<string, string> leader;
// 每一行表示一对朋友关系
string input[] =
{
"周芷若", "张无忌",
"张无忌", "韩小昭",
"成昆", "陈友谅",
"杨逍", "纪晓芙",
};
// 测试每行的两个人是否为朋友
string test[] =
{
"周芷若", "韩小昭",
"张无忌", "成昆",
};
// 初始化
void setLeader()
{
int i = 1;
int totalPerson = 8;
for(i = 0; i < totalPerson; i++)
{
leader[input[i]] = input[i]; // 将自己初始化为自己的领导
}
}
// 查找领导,看看究竟是谁
string findLeader(string s)
{
string r = s;
while(leader[r] != r)
{
r = leader[r]; // 没找到的话,一直往上找
}
return r;
}
// 将两个领导的朋友圈合并,从此,leaderX和leaderY是一个朋友圈了
void uniteSet(string leaderX, string leaderY)
{
leader[leaderX] = leaderY;
}
int main()
{
int numberOfSets = 7; // 最开始有7个不重复的人
// 初始化领导
setLeader();
int i = 0;
int j = 0;
int n = 4; // 人物关系的数组有4行
for(j = 0; j < n; j++)
{
string u = input[i++];
string v = input[i++];
// 找领导
u = findLeader(u);
v = findLeader(v);
// 领导不相等,则合并两个朋友圈
if(u != v)
{
uniteSet(u, v);
numberOfSets--;
}
}
i = 0;
n = 2; // 待测试人物关系的数组有2行
for(j = 0; j < n; j++)
{
string u = test[i++];
string v = test[i++];
// 找领导
u = findLeader(u);
v = findLeader(v);
// 如果领导不相同,则不属于一个朋友圈
if(u != v)
{
cout << "NO" << endl;
}
else // 如果两个领导相同,则肯定属于一个朋友圈
{
cout << "YES" << endl;
}
}
cout << numberOfSets << endl;
return 0;
}
程序结果为:
YES
NO
3
很显然,周芷若和韩小昭是朋友关系,张无忌和成昆不是朋友关系,而且,朋友圈个数为 3, 如上程序可以通过阿里的面试。
并查集很有意思,也是经常涉及到的考点,关键还是在于思路。希望大家对并查集了如指掌,顺利通过笔试、面试,拿到更好的 offer。
完
往期推荐
有道无术,术可成;有术无道,止于术
欢迎大家关注Java之道公众号
好文章,我在看️
版权声明
本文为[Hollis Chuang]所创,转载请带上原文链接,感谢
https://hollis.blog.csdn.net/article/details/124358199
边栏推荐
- 使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
- 微信小程序中app.js文件、组件、api
- Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
- SQL Server 递归查询上下级
- Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
- 高价买来的课程,公开了!phper资料分享
- SQL Server 游标循环表数据
- Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
- Notes on concurrent programming of vegetables (V) thread safety and lock solution
- MySQL how to merge the same data in the same table
猜你喜欢
JVM - common parameters
Visual common drawing (V) scatter diagram
Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
Solutions to common problems in visualization (IX) background color
Typora operation skill description (I) md
C语言之结构体(进阶篇)
【leetcode】107. Sequence traversal of binary tree II
Xshell+Xftp 下载安装步骤
精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
Visual common drawing (III) area map
随机推荐
VIM usage
Visual common drawing (III) area map
精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
Anaconda3 installation
454. Sum of four numbers (hash table)
Solution architect's small bag - 5 types of architecture diagrams
Mba-day5 Mathematics - application problems - engineering problems
【leetcode】199. Right view of binary tree
RESTful和SOAP的区别
Gets the current time in character format
VIM + ctags + cscope development environment construction guide
remote: Support for password authentication was removed on August 13, 2021.
微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
关于JUC三大常用辅助类
MBA-day5数学-应用题-工程问题
[provincial election joint examination 2022 d2t1] card (state compression DP, FWT convolution)
Visualization Road (10) detailed explanation of segmentation canvas function
Esp32 learning - use and configuration of GPIO
HuggingFace
Simple thoughts on the design of a microblog database