Matrix అనేది 2D array, కానీ interviews లో ఇది తరచుగా grid ను సూచిస్తుంది - map, board లేదా maze. ప్రతి cell grid[r][c] ఒక node, దాని neighbors పక్కనే ఉన్న cells.
grid = [
[1, 1, 0],
[1, 0, 0],
[0, 0, 1]
]
Row r, column c, dimensions m × n. సరళం - కానీ దీని పైన నిర్మించే patterns శక్తివంతమైనవి.
నాలుగు వేర్వేరు if-statements రాయడానికి బదులు, direction array ఉపయోగించండి:
# 4-దిశల (పైన, కింద, ఎడమ, కుడి)
dirs = [(-1,0), (1,0), (0,-1), (0,1)]
# 8-దిశల (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:
# neighbor ను process చేయి
Grid సమస్యలలో అత్యంత పునర్వినియోగ code snippet ఇది.
BFS unweighted grid లో shortest path కనుగొంటుంది. Source నుండి ప్రారంభించి, దూరం 1 వద్ద అన్ని neighbors ను, తర్వాత దూరం 2 వద్ద, ఇలా explore చేయండి.
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 cells ను మార్చండి. DFS లేదా BFS రెండూ పనిచేస్తాయి.
Sign in to join the discussion
Islands సంఖ్య: grid scan చేయండి; 1 కనుగొంటే, మొత్తం island ను visited గా mark చేయడానికి DFS/BFS run చేసి, count పెంచండి.
def 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) # island mark చేయి
count += 1
return count
'Islands సంఖ్య' లో, DFS సమయంలో cells ను visited గా ఎందుకు mark చేస్తాం?
మొదట్లో rotten అయిన అన్ని oranges sources. వాటన్నింటినీ time 0 వద్ద queue లో push చేసి, BFS చేయండి. ప్రతి level ఒక నిమిషం. Queue ఖాళీ అయినప్పుడు, fresh oranges మిగిలాయా తనిఖీ చేయండి.
Minute 0: [2, 1, 1] 2 = rotten, 1 = fresh
[1, 1, 0]
[0, 1, 1]
Minute 4: [2, 2, 2] చేరుకోగల అన్ని oranges rotten
[2, 2, 0]
[0, 2, 2]
కీలక అంతర్దృష్టి: multi-source BFS ఒకే node నుండి కాకుండా అనేక nodes నుండి ఏకకాలంలో ప్రారంభిస్తుంది.
'Rotting Oranges' ను standard BFS నుండి ఏమి భిన్నంగా చేస్తుంది?
Matrix ను spiral క్రమంలో నడవండి: కుడి → కింద → ఎడమ → పైన, ప్రతి pass తర్వాత boundaries తగ్గిస్తూ.
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 ([r][c] ని [c][r] తో swap), తర్వాత ప్రతి row reverse.
Original → Transpose → Rows reverse
1 2 3 1 4 7 7 4 1
4 5 6 → 2 5 8 → 8 5 2
7 8 9 3 6 9 9 6 3
రెండు సాధారణ variants:
Unique Paths: top-left నుండి bottom-right కి కేవలం కుడి లేదా కింద కదులుతూ paths లెక్కించండి. dp[r][c] = dp[r-1][c] + dp[r][c-1].
Minimum Path Sum: అదే ఆలోచన కానీ minimum incoming path ఎంచుకోండి.
ఇవి 2D dynamic programming కి తరచుగా సులభమైన పరిచయం.
Unique Paths సమస్యలో, ఏ column c కైనా dp[0][c] ఏమిటి?
| Pattern | Technique | కీలక సమస్య | |---------|-----------|------------| | 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 | | Paths లెక్కించడం | DP | Unique Paths | | Sorted grid లో search | Staircase / binary search | Search 2D Matrix |