LeetCode Friend Circles
原题链接在这里:https://leetcode.com/problems/friend-circles/description/
题目:
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.
Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.
Example 1:
Input:
[[1,1,0],
[1,1,0],
[0,0,1]]
Output: 2
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
The 2nd student himself is in a friend circle. So return 2.
Example 2:
Input:
[[1,1,0],
[1,1,1],
[0,1,1]]
Output: 1
Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends,
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.
Note:
- N is in range [1,200].
- M[i][i] = 1 for all students.
- If M[i][j] = 1, then M[j][i] = 1.
题解:
与Number of Connected Components in an Undirected Graph类似. 可以采用DFS, BFS, Union Find三种方法.
当M[i][j] == 1. 就说明i 和 j是好友. DFS, BFS时标记走过的点即可.
Time Complexity: O(n^2). n = M.length. M上的点最多走两遍.
Space: O(n). 开了visited array标记. 最多用了n层stack.
AC Java:
class Solution {
public int findCircleNum(int[][] M) {
if(M == null || M.length == 0 || M[0].length == 0){
return 0;
} int len = M.length;
boolean [] visited = new boolean[len];
int res = 0;
for(int i = 0; i<len; i++){
if(!visited[i]){
dfs(M, visited, i);
res++;
}
} return res;
} private void dfs(int [][] M, boolean [] visited, int i){
for(int j = 0; j<M[0].length; j++){
if(!visited[j] && M[i][j]==1){
visited[j] = true;
dfs(M, visited, j);
}
}
}
}
BFS可以选择在出queue的时候更改标记.
Time Complexity: O(n^2). n = M.length.
Space: O(n).
AC Java:
class Solution {
public int findCircleNum(int[][] M) {
if(M == null || M.length == 0 || M[0].length == 0){
return 0;
} int res = 0;
int len = M.length;
boolean [] visited = new boolean[len];
LinkedList<Integer> que = new LinkedList<Integer>();
for(int i = 0; i<len; i++){
if(!visited[i]){
res++;
que.add(i);
while(!que.isEmpty()){
int cur = que.poll();
visited[cur] = true;
for(int j = 0; j<len; j++){
if(!visited[j] && M[cur][j]==1){
que.add(j);
}
}
}
}
} return res;
}
}
Union Find Methos is to return the count of unions.
Time Complexity: O(n^2logn). find takes O(logn). With path compression and union by weight, amatorize O(1).
Space: O(n).
AC Java:
class Solution {
public int findCircleNum(int[][] M) {
if(M == null || M.length == 0 || M[0].length == 0){
return 0;
} int n = M.length;
UnionFind uf = new UnionFind(n);
for(int i = 0; i<n; i++){
for(int j = 0; j<i; j++){
if(i!=j && M[i][j] == 1){
if(uf.find(i) != uf.find(j)){
uf.union(i, j);
}
}
}
} return uf.count;
}
} class UnionFind{
int [] parent;
int [] size;
int count; public UnionFind(int n){
this.parent = new int[n];
this.size = new int[n];
this.count = n; for(int i = 0; i<n; i++){
parent[i] = i;
size[i] = 1;
}
} public int find(int i){
while(i != parent[i]){
parent[i] = parent[parent[i]];
i = parent[i];
} return parent[i];
} public void union(int i, int j){
int x = find(i);
int y = find(j); if(size[x] > size[y]){
parent[y] = x;
size[x] += size[y];
}else{
parent[x] = y;
size[y] += size[x];
} this.count--;
}
}
类似The Earliest Moment When Everyone Become Friends.
LeetCode Friend Circles的更多相关文章
- [LeetCode] Friend Circles 朋友圈
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)
Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递 ...
- [LeetCode] 547. Friend Circles 朋友圈
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- LeetCode 547. Friend Circles 朋友圈(C++/Java)
题目: https://leetcode.com/problems/friend-circles/ There are N students in a class. Some of them are ...
- 【LeetCode】547. Friend Circles 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- [LeetCode]547. Friend Circles朋友圈数量--不相邻子图问题
/* 思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友 ,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人. 其实dfs真正有用的是flag这个变量,因为如果 ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
随机推荐
- 【Python】常用排序算法的python实现和性能分析
作者:waterxi 原文链接 背景 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试题整 ...
- 开发H5游戏引擎的选择:Egret或Laya?
开发H5游戏引擎的选择:Egret或Laya? 一.总结 一句话总结:选laya吧 二.开发H5游戏引擎的选择:Egret或Laya? 一.H5游戏开发的引擎介绍 开发H5游戏的引擎有很多,比如egr ...
- python判断指定路径是否存在
https://www.cnblogs.com/jhao/p/7243043.html
- Nginx、Apache工作原理及Nginx为何比Apache高效
Nginx才短短几年,就拿下了web服务器大笔江山,众所周知,Nginx在处理大并发静态请求方面,效率明显高于httpd,甚至能轻松解决C10K问题. 在高并发连接的情况下,Nginx是Apache服 ...
- crontab执行定时任务
在linux下面使用命令crontab -e 编辑任务: [adv@localhost]$ crontab -e 之后开始编辑任务 * * * * * cd /home/adv/work/cutte ...
- python进行linux系统监控
python进行linux系统监控 Linux系统下: 静态指标信息: 名称 描述 单位 所在文件 mem_total 内存总容量 KB /proc/meminfo disks 磁盘相关信息 - ...
- Check for Palindromes
如果给定的字符串是回文,返回true,反之,返回false. 如果一个字符串忽略标点符号.大小写和空格,正着读和反着读一模一样,那么这个字符串就是palindrome(回文). 注意你需要去掉字符串多 ...
- 初识async函数
为什么会出现async函数 首先从大的方面来说,出现async函数时为了解决JS编程中的异步操作,再往具体说就是为了对以往异步编程方法的一种改进,也有人说仅仅只是Generator 函数的语法糖,这个 ...
- 转:基于Flume的美团日志收集系统(一)架构和设计
美团的日志收集系统负责美团的所有业务日志的收集,并分别给Hadoop平台提供离线数据和Storm平台提供实时数据流.美团的日志收集系统基于Flume设计和搭建而成. <基于Flume的美团日志收 ...
- SQL 二进制和字符互转
1.二进制转为字符串 ALTER function varbin2hexstr( ) )) as begin ),@i int select @re='',@i=datalength(@bin) ), ...