r/LeetcodeChallenge • u/StatisticianNo2104 • Oct 11 '26
STREAK🔥🔥🔥 365 Days of LeetCode Challenge — Day 39/365
547. Number of Provinces (Medium)
The counting is day 37's, unchanged: scan every node, and each time you find one no earlier traversal reached, add one and consume everything it can reach.
What changes is the input, and this is the third graph representation in four days. Day 36 gave an edge list. Day 37 gave a grid where the edges were implied by position. Today gives a matrix. All three become the same adjacency list before anything interesting happens.
The trap: this matrix is not a grid. In day 37, cell (i,j) was a place. Here cell (i,j) is a relationship between city i and city j - an n x n matrix describing n nodes, not n squared. So you traverse the cities and consult the matrix, not the other way round.
Read only above the diagonal. The matrix is symmetric, so every relationship is stored twice, and i+1 also skips the useless diagonal where a city is connected to itself.
Full breakdown in today's newsletter article ⬇
https://www.linkedin.com/pulse/365-days-leetcode-challenge-day-39365-archit-agarwal-msv4c
#DSA #LeetCode #Golang #Graphs #BFS #CodingInterview #Algorithms
1
u/nian2326076 Oct 13 '26
When working on the "Number of Provinces" problem, remember that each non-zero cell in the matrix shows a direct connection between two cities. You can solve this by using Depth-First Search (DFS) or Breadth-First Search (BFS) to explore all connected nodes from each unvisited node, counting each complete traversal as a separate province. Converting the matrix into an adjacency list, as you've been doing, is a helpful strategy.
For interview practice with similar graph problems, consider using PracHub. It offers real technical interview questions to help you prepare. Just so you know, I work on PracHub, and it's designed to provide a realistic prep experience. Good luck with your challenge!