A post spreading through shares
Write code · Challenge · §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, so a post that b shares appears in a's feed.
Person start writes a post and shares it. Everyone whose feed shows this post reshares it. Complete the function solution, which returns the number of people, not counting start, whose feed shows this post at least once.
Function
solution(n, follows, start)
Constraints
nis an integer from 1 to 100,000.- The length of
followsis from 0 to 100,000. - Each element of
followshas the form[a, b], whereaandbare different integers from 0 ton - 1. The same record never appears twice. startis an integer from 0 ton - 1.
Examples
| n | follows | start | result |
|---|---|---|---|
6 | [[1, 0], [2, 0], [3, 1], [0, 4]] | 0 | 3 |
5 | [[1, 2], [3, 2], [0, 1]] | 2 | 3 |
Starter code
def solution(n, follows, start):
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.