Degree of Every Node
An undirected graph has n nodes numbered 0 to n - 1. edges is a list of pairs [a, b] with a != b; each pair is one edge joining node a and node b. The same pair may appear more than once, in either order, and every copy is a separate edge.
The degree of a node is the number of edges that touch it. Return a list of length n whose entry i is the degree of node i. With no edges every entry is 0.
Example 1
n 4
edges [[0,1],[1,2],[1,3]]
returns [1,3,1,1]
Node 1 is on three edges; each of the other nodes is on one.
Example 2
n 3
edges [[0,1],[1,0],[1,2]]
returns [2,3,1]
[0, 1] and [1, 0] are two separate edges, so nodes 0 and 1 are each touched twice by them.
Constraints
1 <= n <= 500 <= len(edges) <= 1000 <= a, b <= n - 1anda != b
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Graphs problems
- Everyone Knows EveryoneBeginner
- Friends in CommonBeginner
- Is There a RoadBeginner
- Next DoorBeginner
- No HandshakeBeginner
- Who Points at MeBeginner
- Busiest NodeEasy
- Cable GroupsEasy