Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Matrix Revision
DSA

Matrix Revision

Quickly revise matrix operations, traversal techniques, transformations, and common interview patterns.

1 Matrix Traversal

// Row-wise
for i in 0..n-1:
    for j in 0..m-1:
        process matrix[i][j]

// Column-wise
for j in 0..m-1:
    for i in 0..n-1:
        process matrix[i][j]
int n = matrix.length, m = matrix[0].length;

for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
        // row-major: matrix[i][j]

for (int j = 0; j < m; j++)
    for (int i = 0; i < n; i++)
        // column-major: matrix[i][j]
n, m = len(matrix), len(matrix[0])

for i in range(n):
    for j in range(m):
        ...      # row-major: matrix[i][j]

for j in range(m):
    for i in range(n):
        ...      # column-major: matrix[i][j]
int n = matrix.size(), m = matrix[0].size();

for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
        /* row-major: matrix[i][j] */;

for (int j = 0; j < m; j++)
    for (int i = 0; i < n; i++)
        /* column-major: matrix[i][j] */;
const n = matrix.length,
  m = matrix[0].length;

for (let i = 0; i < n; i++)
  for (let j = 0; j < m; j++) // row-major: matrix[i][j];

for (let j = 0; j < m; j++)
  for (let i = 0; i < n; i++); // column-major: matrix[i][j];

2 Spiral Matrix

top = 0, bottom = n-1
left = 0, right = m-1

while top <= bottom && left <= right:
    traverse top row left->right, top++
    traverse right col top->bottom, right--
    if top <= bottom: traverse bottom row right->left, bottom--
    if left <= right: traverse left col bottom->top, left++
public List<Integer> spiralOrder(int[][] matrix) {
    List<Integer> res = new ArrayList<>();
    int top = 0, bottom = matrix.length - 1;
    int left = 0, right = matrix[0].length - 1;

    while (top <= bottom && left <= right) {
        for (int j = left; j <= right; j++) res.add(matrix[top][j]);
        top++;
        for (int i = top; i <= bottom; i++) res.add(matrix[i][right]);
        right--;
        if (top <= bottom)
            for (int j = right; j >= left; j--) res.add(matrix[bottom][j]);
        bottom--;
        if (left <= right)
            for (int i = bottom; i >= top; i--) res.add(matrix[i][left]);
        left++;
    }

    return res;
}
def spiral_order(matrix):
    res = []
    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): res.append(matrix[top][j])
        top += 1
        for i in range(top, bottom + 1): res.append(matrix[i][right])
        right -= 1
        if top <= bottom:
            for j in range(right, left - 1, -1): res.append(matrix[bottom][j])
            bottom -= 1
        if left <= right:
            for i in range(bottom, top - 1, -1): res.append(matrix[i][left])
            left += 1

    return res
vector<int> spiralOrder(vector<vector<int>>& matrix) {
    vector<int> res;
    int top = 0, bottom = matrix.size() - 1;
    int left = 0, right = matrix[0].size() - 1;

    while (top <= bottom && left <= right) {
        for (int j = left; j <= right; j++) res.push_back(matrix[top][j]);
        top++;
        for (int i = top; i <= bottom; i++) res.push_back(matrix[i][right]);
        right--;
        if (top <= bottom)
            for (int j = right; j >= left; j--) res.push_back(matrix[bottom][j]);
        bottom--;
        if (left <= right)
            for (int i = bottom; i >= top; i--) res.push_back(matrix[i][left]);
        left++;
    }

    return res;
}
function spiralOrder(matrix) {
  const res = [];
  let top = 0,
    bottom = matrix.length - 1;
  let left = 0,
    right = matrix[0].length - 1;

  while (top <= bottom && left <= right) {
    for (let j = left; j <= right; j++) res.push(matrix[top][j]);
    top++;
    for (let i = top; i <= bottom; i++) res.push(matrix[i][right]);
    right--;
    if (top <= bottom) {
      for (let j = right; j >= left; j--) res.push(matrix[bottom][j]);
      bottom--;
    }
    if (left <= right) {
      for (let i = bottom; i >= top; i--) res.push(matrix[i][left]);
      left++;
    }
  }

  return res;
}

3 Rotate Image

// Transpose
for i in 0..n-1:
    for j in i+1..n-1:
        swap matrix[i][j], matrix[j][i]

// Reverse each row
for i in 0..n-1:
    reverse matrix[i]
public void rotate(int[][] matrix) {
    int n = matrix.length;

    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            swap(matrix, i, j, j, i);

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n / 2; j++)
            swap(matrix, i, j, i, n - 1 - j);
}
def rotate(matrix):
    n = len(matrix)

    for i in range(n):
        for j in range(i + 1, n):
            matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

    for row in matrix:
        row.reverse()
void rotate(vector<vector<int>>& matrix) {
    int n = matrix.size();

    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            std::swap(matrix[i][j], matrix[j][i]);

    for (auto& row : matrix)
        std::reverse(row.begin(), row.end());
}
function rotate(matrix) {
  const n = matrix.length;

  for (let i = 0; i < n; i++)
    for (let j = i + 1; j < n; j++)
      [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];

  for (const row of matrix) row.reverse();
}

4 Flood Fill

function floodFill(image, sr, sc, newColor):
    if image[sr][sc] == newColor: return
    original = image[sr][sc]
    dfs(image, sr, sc, original, newColor)
public void dfs(int[][] image, int i, int j, int orig, int color) {
    if (i < 0 || j < 0 || i >= image.length || j >= image[0].length)
        return;
    if (image[i][j] != orig) return;

    image[i][j] = color;
    dfs(image, i+1, j, orig, color);
    dfs(image, i-1, j, orig, color);
    dfs(image, i, j+1, orig, color);
    dfs(image, i, j-1, orig, color);
}
def dfs(image, i, j, orig, color):
    if i < 0 or j < 0 or i >= len(image) or j >= len(image[0]):
        return
    if image[i][j] != orig:
        return

    image[i][j] = color
    dfs(image, i + 1, j, orig, color)
    dfs(image, i - 1, j, orig, color)
    dfs(image, i, j + 1, orig, color)
    dfs(image, i, j - 1, orig, color)
void dfs(vector<vector<int>>& image, int i, int j, int orig, int color) {
    if (i < 0 || j < 0 || i >= image.size() || j >= image[0].size())
        return;
    if (image[i][j] != orig) return;

    image[i][j] = color;
    dfs(image, i+1, j, orig, color);
    dfs(image, i-1, j, orig, color);
    dfs(image, i, j+1, orig, color);
    dfs(image, i, j-1, orig, color);
}
function dfs(image, i, j, orig, color) {
  if (i < 0 || j < 0 || i >= image.length || j >= image[0].length) return;
  if (image[i][j] !== orig) return;

  image[i][j] = color;
  dfs(image, i + 1, j, orig, color);
  dfs(image, i - 1, j, orig, color);
  dfs(image, i, j + 1, orig, color);
  dfs(image, i, j - 1, orig, color);
}

My Private Notes

Notes are auto-saved locally to this device.