2Brute2Best
🔥0

Number of Provinces

Medium·LeetCode #547 Pro OnlySlight Variation
LeetCode
01

Problem Statement

There are n cities. isConnected is an n × n table. Cell [i][j] is 1 when city i is directly connected to city j, and 0 when it is not. The table is symmetric, and each city is connected to itself.

A province is a group of cities that can reach each other through direct or indirect connections, and that has no connection to any city outside the group. Return how many provinces there are.

Input

An n × n table isConnected of 1s and 0s.

Output

The number of provinces (connected groups of cities).

Example 1
Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2

Cities 0 and 1 are linked to each other, and city 2 stands alone. That makes two groups.

Example 2
Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3

No two cities are linked, so every city is its own province.

Constraints
  • • 1 ≤ n ≤ 200
  • • n = isConnected.length, and each row has n entries
  • • isConnected[i][j] is 1 or 0
  • • isConnected[i][i] = 1
  • • isConnected[i][j] = isConnected[j][i]
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“How many provinces”
→ Count the connected groups. Counting links or 1s counts the wrong thing.
“directly or indirectly connected”
→ A group is closed under links: if you can get from a to b, they share one group.
“isConnected table”
→ The graph is given as an adjacency matrix, so each city's neighbours sit in one row to scan.
03

Approach Complexity Ladder

Brute Force — labels spread until nothing changesO(N³)O(N)

Each city keeps taking the smallest label among its neighbours; count the distinct labels at the end

Optimal — DFS from each unvisited cityO(N²)O(N)

Count the DFS starts: each unvisited city starts a new province and marks everything it can reach

Brute Force Intuition

Give each city its own number as a label. Repeat rounds: every city looks at the cities it is directly linked to and takes the smallest label it sees. Stop when a whole round changes nothing. Cities in the same province end with the same label, so the number of distinct labels is the number of provinces.

Recognition rule for this sub-pattern:

Adjacency list + visited set. Count how many times you must start a new traversal.
Remember this · Graph BFS / DFS
Trigger
Grid of cells, islands, regions, "minimum steps", transformations between states, connected components.
Think
What is a node, what is an edge, and do I need the shortest path or just reachability?
Key question
How do I mark visited so I never process a node twice?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.