FastPrepNumber of Provinces

Number of Provinces

SambaNova Systems logoSambaNova Systems● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

You are given an n x n matrix isConnected. If isConnected[i][j] = 1, person i and person j are direct friends.

A friend circle is a group of directly or indirectly connected people with no friendship to anyone outside the group.

Return the total number of friend circles.

Function

findCircleNum(isConnected: int[][]) → int

Examples

Example 1

isConnected = [[1,1,0],[1,1,0],[0,0,1]]return = 2

People 0 and 1 form one friend circle, while person 2 forms another.

Example 2

isConnected = [[1,0,0],[0,1,0],[0,0,1]]return = 3

No two distinct people are friends, so every person is their own friend circle.

Example 3

isConnected = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]]return = 1

The direct friendships form one transitive chain containing all four people.

Constraints

  • 1 ≤ n ≤ 200
  • isConnected.length = n
  • isConnected[i].length = n
  • isConnected[i][j] is 0 or 1.
  • isConnected[i][i] = 1
  • isConnected[i][j] = isConnected[j][i]

More SambaNova Systems problems

See SambaNova Systems hiring insights
public int findCircleNum(int[][] isConnected) {
    // write your code here
}
isConnected[[1,1,0],[1,1,0],[0,0,1]]
expected2
Checking account…