All files / leetCode 0547.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24  3x 3x 3x 9x   9x 27x 3x       3x   3x 9x 6x 6x       3x    
export default function findCircleNum(isConnected: number[][]): number {
  const n = isConnected.length
  const visited = Array.from<boolean>({ length: n }).fill(false)
  const dfs = (i: number) => {
    visited[i] = true
 
    for (let j = 0; j < n; j++) {
      if (visited[j] === false && isConnected[i][j])
        dfs(j)
    }
  }
 
  let count = 0
 
  for (let i = 0; i < n; i++) {
    if (visited[i] === false) {
      dfs(i)
      count++
    }
  }
 
  return count
}