Matrix बस एक 2D array है, लेकिन interviews में यह अक्सर grid represent करता है - map, board, या maze। हर cell grid[r][c] एक node है, और उसके neighbours adjacent cells हैं।
grid = [
[1, 1, 0],
[1, 0, 0],
[0, 0, 1]
]
Row r, column c, dimensions m × n। Simple - लेकिन इस पर बने patterns बहुत powerful हैं।
चार अलग if-statements लिखने की जगह direction array इस्तेमाल करो:
# 4-directional (up, down, left, right)
dirs = [(-1,0), (1,0), (0,-1), (0,1)]
# 8-directional (includes diagonals)
dirs = [(-1,0),(1,0),(0,-1),(0,1),
(-1,-1),(-1,1),(1,-1),(1,1)]
for dr, dc in dirs:
nr, nc = r + dr, c + dc
if 0 <= nr < m and 0 <= nc < n:
# process neighbour
Grid problems में यह सबसे reusable snippet है।
BFS unweighted grid में shortest path ढूँढता है। Source से शुरू करो, पहले distance 1 के सब neighbours explore करो, फिर distance 2, और आगे।
from collections import deque
def shortest_path(grid, start, end):
m, n = len(grid), len(grid[0])
q = deque([(start[0], start[1], 0)])
visited = {(start[0], start[1])}
dirs = [(-1,0),(1,0),(0,-1),(0,1)]
while q:
r, c, dist = q.popleft()
if (r, c) == end:
return dist
for dr, dc in dirs:
nr, nc = r+dr, c+dc
if 0<=nr<m and 0<=nc<n and (nr,nc) not in visited and grid[nr][nc]==0:
visited.add((nr, nc))
q.append((nr, nc, dist+1))
return -1
Flood fill (paint bucket tool जैसा): starting cell से सभी connected same-colour cells बदलो। DFS या BFS दोनों काम करते हैं।
grid scan करो; मिले तो DFS/BFS चलाकर पूरा island visited mark करो, फिर count बढ़ाओ।
Sign in to join the discussion
1def num_islands(grid):
count = 0
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] == '1':
dfs(grid, r, c) # mark island
count += 1
return count
'Number of Islands' में DFS के दौरान cells को visited क्यों mark करते हैं?
सभी शुरू से rotten oranges sources हैं। सबको time 0 पर queue में डालो, फिर BFS। हर level एक minute। Queue खाली होने पर check करो कोई fresh orange बचा तो नहीं। Multi-source BFS कई nodes से simultaneously शुरू होता है।
'Rotting Oranges' standard BFS से कैसे अलग है?
Matrix को spiral order में walk करो: right → down → left → up, हर pass के बाद boundaries shrink करो।
def spiral(matrix):
res = []
top, bot = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1
while top <= bot and left <= right:
for c in range(left, right + 1):
res.append(matrix[top][c])
top += 1
for r in range(top, bot + 1):
res.append(matrix[r][right])
right -= 1
if top <= bot:
for c in range(right, left - 1, -1):
res.append(matrix[bot][c])
bot -= 1
if left <= right:
for r in range(bot, top - 1, -1):
res.append(matrix[r][left])
left += 1
return res
N×N matrix clockwise in-place rotate: transpose (swap [r][c] with [c][r]), फिर हर row reverse।
दो common variants:
Unique Paths: top-left से bottom-right तक paths count करो, सिर्फ right या down move। dp[r][c] = dp[r-1][c] + dp[r][c-1]।
Minimum Path Sum: same idea लेकिन minimum incoming path चुनो।
Unique Paths problem में किसी भी column c के लिए dp[0][c] क्या है?
| Pattern | Technique | Key Problem | |---------|-----------|-------------| | Shortest path (unweighted) | BFS | Maze shortest path | | Connected components | DFS / BFS | Number of Islands | | Multi-source spread | Multi-source BFS | Rotting Oranges | | Traversal order | Boundary pointers | Spiral Matrix | | In-place transform | Transpose + reverse | Rotate Image | | Counting paths | DP | Unique Paths | | Sorted grid में search | Staircase / binary search | Search 2D Matrix |