Matrices Flashcards

(5 cards)

1
Q

What is the pattern and when is it used?

A

Processes 2D grid with traversal or manipulation.

Use Case: Grid search, transformations.

Example: [No LeetCode match, course example: Spiral Matrix]. Action: Verbalize use case aloud.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the key steps?

A
  1. Define boundaries (top, bottom, left, right).
  2. Traverse layer by layer, update boundaries.
  3. Handle remaining rows/columns.

Action: Explain steps for Spiral Matrix aloud.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does it apply to Spiral Matrix?

A

Return elements in spiral order.
Approach: Traverse boundaries, shrink after each direction.

Example: Spiral Matrix. Action: Verbalize solution logic aloud.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the complexity and gotchas?

A

Time: O(m*n), Space: O(1).

Gotchas: Empty matrix, single row/column.

Action: List edge cases for Spiral Matrix aloud.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Provide a visual or code example.

A

```python
from typing import List
def spiral_order(matrix: List[List[int]]) -> List[int]:
if not matrix:
return []
result = []
top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1
while top <= bottom and left <= right:
for j in range(left, right + 1):
result.append(matrix[top][j])
top += 1
for i in range(top, bottom + 1):
result.append(matrix[i][right])
right -= 1
if top <= bottom:
for j in range(right, left - 1, -1):
result.append(matrix[bottom][j])
bottom -= 1
if left <= right:
for i in range(bottom, top - 1, -1):
result.append(matrix[i][left])
left += 1
return result
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly