↓Skip to main content
  1. LeetCode/

LeetCode 3643: Flip Square Submatrix Vertically

·2 mins· ·
LeetCode Easy Two-Pointers Matrix
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-08-10

  • Submissions: Wrong Answer → Accepted → Accepted (retyped 2026-01-24)

Approach
#

The submatrix has top-left corner (x, y) and side k. Flipping it vertically swaps its i-th row with its i-th row from the bottom, but only the slice from y to y + k - 1; the rest of the matrix is untouched.

Use converging pointers: the top row starts at x going down, the bottom row at x + k - 1 going up, for k // 2 swaps.

My Attempt: using the last row of the whole matrix
#

for i in range(x, x + (len(grid) - x) // 2):
    grid[i][y:y + k], grid[-1][y:y + k] = grid[-1][y:y + k], grid[i][y:y + k]

Two mistakes:

  1. The bottom row is grid[-1], the last row of the whole matrix, and it is the same row every time instead of moving up with i.
  2. The number of swaps comes from the matrix’s remaining rows rather than the submatrix side k.

For a 4-row matrix with x = 2 and k = 1, the submatrix is a single cell and nothing should change, but this version swapped that cell in row 2 with the last row.

Solution
#

class Solution:
    def reverseSubmatrix(self, grid: List[List[int]], x: int, y: int, k: int) -> List[List[int]]:
        top, bottom = x, x + k - 1
        while top < bottom:
            grid[top][y:y + k], grid[bottom][y:y + k] = grid[bottom][y:y + k], grid[top][y:y + k]
            top += 1
            bottom -= 1
        return grid

The slice assignment grid[top][y:y + k] = ... replaces only that part of the row, leaving the other columns alone.

Complexity
#

Time O(k^2), space O(k) (slices copy a segment).

Mistakes to Avoid
#

  • Using grid[-1] as the submatrix’s last row.
  • Swapping whole rows instead of only the y to y + k - 1 slice.

Interview Explanation
#

Two pointers mark the submatrix’s first and last rows, x and x + k - 1. I swap their slices from y to y + k - 1 and move both inward, for k // 2 swaps in total. Time O(k^2).

Related

LeetCode 832: Flipping an Image
·2 mins
LeetCode Easy Array Two-Pointers Bit-Manipulation Matrix
LeetCode 832 note: reverse each row and invert its bits, which two pointers can do at once
LeetCode 2441: Largest Positive Integer That Exists With Its Negative
·2 mins
LeetCode Easy Hash-Map Two-Pointers Sorting
LeetCode 2441 note: keep positives and absolute values of negatives in separate sets and take the largest common value; absolute values alone mistake same-sign duplicates for pairs
LeetCode 1768: Merge Strings Alternately
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 1768 note: take characters in turn with one index, then append whatever remains of the longer string
LeetCode 1089: Duplicate Zeros
·2 mins
LeetCode Easy Array Two-Pointers
LeetCode 1089 note: count the zeros to know each value’s shift, then move from back to front, writing the extra 0 when copying a zero
LeetCode 977: Squares of a Sorted Array
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 977 note: the largest squares are at the two ends, so compare the ends with two pointers and fill the result from the back
LeetCode 905: Sort Array By Parity
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 905 note: a write pointer swaps even numbers to the front, just like move zeroes