Перейти к содержимому

Number of Provinces | Count the Groups | LeetCode 75 #44

Placements

0:00 / 0:00

Number of Provinces | Count the Groups | LeetCode 75 #44

3 просмотра · 9 дней назад
Placements
2 подписчика
3 просмотра · 9 дней назад
Number of Provinces — LeetCode 547 — explained in Hindi, step by step, with animations. Problem: https://leetcode.com/problems/number-... n cities numbered 0 to n-1. isConnected[i][j] = 1 means city i and city j are directly connected (the matrix is symmetric, and every city counts as connected to itself). A province is a group of cities connected directly or indirectly (friend-of-friend). Return the number of provinces — i.e. the number of connected components. In this video: Problem statement, word by word, with both official examples Core idea in one line + the villages-and-messenger analogy (DFS = messenger, visited = flags, provinces = areas) HARD PART 1: why DFS — the "count the rows" trap, the "unique rows" chain trap (friend-of-friend), why DFS from city 0 alone misses disconnected cities, and why the visited array is the whole game (infinite loop without it, partition proof) HARD PART 2: symmetric matrix means row i is enough; the diagonal 1s (self-loops) bounce off visited — mark-before-push, always HARD PART 3: count discoveries, not visits — 3 cities visited but 2 provinces; the guard if (!visited[i]) decides everything Full animated dry run on [[1,1,0],[1,1,0],[0,0,1]] with live stack + visited tiles + provinces counter C++ code walkthrough (debugger style, fused with live cities) with the WHY behind every line Correctness proof (both directions, induction on path length, termination) + O(n²) time reasoning with lower-bound argument + O(n) space Edge cases: n=1, all isolated, all connected, n=200, the three traps (no outer loop, counting visits, skipping the visited check) Interview follow-ups: DFS vs BFS, Union-Find alternative, recursion vs iterative, non-symmetric matrix, largest province, O(n²) lower bound, stack safety This is video #44 of the LeetCode 75 series — DSA in Hindi. #LeetCode #LeetCode75 #DSA #Graph #DFS #ConnectedComponents #HindiCoding #Placements #CodingInterview #DataStructures