Groups linked by follows
Write code · Medium · §9.1 Following relationships
There are n users on a social network, numbered from 0 to n - 1. A record [a, b] in follows means that a follows b.
No matter who follows whom, two people are linked if there is a follow record between them. People who can reach one another by going along these links all belong to one group, and a person with no follow records at all forms a group on their own.
Complete the function solution, which returns the number of groups.
Function
solution(n, follows)
Constraints
nis an integer from 1 to 900.- The length of
followsis from 0 to 10,000. - Each element of
followshas the form[a, b], whereaandbare different integers from 0 ton - 1. - The same record never appears twice.
[a, b]and[b, a]are different records.
Examples
| n | follows | result |
|---|---|---|
5 | [[0, 1], [1, 2], [3, 4]] | 2 |
7 | [[0, 1], [2, 3], [2, 4], [5, 6]] | 3 |
Starter code
def solution(n, follows):
answer = 0
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.