Count the Links
A network is given as neighbor lists: neighbors[i] lists the nodes that node i is linked to. Links work in both directions, so whenever j is in neighbors[i], i is in neighbors[j] too.
Return the number of links in the network.
Example 1
neighbors [[1,2],[0],[0]]
returns 2
The links are 0 with 1 and 0 with 2. Each of them shows up in two lists.
Example 2
neighbors [[1,2],[0,2],[0,1]]
returns 3
A triangle has three links.
Example 3
neighbors [[]]
returns 0
A single node has no links.
Constraints
1 <= len(neighbors) <= 12- no node is linked to itself, and no list names a node twice
- the lists agree with each other in both directions
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Graphs problems
- Dead EndsBeginner
- Degree of Every NodeBeginner
- Everyone Knows EveryoneBeginner
- Friends in CommonBeginner
- Is There a RoadBeginner
- Next DoorBeginner
- No HandshakeBeginner
- Who Points at MeBeginner