AlgoFrames

In-degree and out-degree

Standard · Write code · about 8 min

graph is directed. An edge [u, v] is an arrow from u to v. Return [out, in]: out[v] counts arrows leaving v and in[v] counts arrows entering v.

Example 1

Input:

[{"n":5,"edges":[[0,1],[2,1],[3,1],[1,4]],"directed":true}]

Output:

[[1,1,1,1,0],[0,3,0,0,1]]

Three arrows enter 1 and one leaves it.

Example 2

Input:

[{"n":3,"edges":[[0,1],[1,2],[2,0]],"directed":true}]

Output:

[[1,1,1],[1,1,1]]

In a directed cycle every vertex has one in and one out.

Constraints

  • 1 ≤ n ≤ 1,000
  • 0 ≤ edges.length ≤ 2,000
  • 0 ≤ u, v < n

Learn the idea first: Graphs: vertices, edges, directed, weighted, degree, paths, a visual lesson.

More Graphs: vertices, edges, directed, weighted, degree, paths problems