3643. Flip Square Submatrix Vertically
UnknownView on LeetCode
Time: O(n * k)
Space: O(n²)
Problem Overview
Flip Square Submatrix Vertically (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Dynamic Programming pattern in coding interviews. For each cell determine flip parity from range queries applied; XOR result.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
For each cell determine flip parity from range queries applied; XOR result.
Related patterns: Array, Dynamic Programming, Matrix
3643.cs
C#
// Approach: For each cell determine flip parity from range queries applied; XOR result.
// Time: O(n * k) Space: O(n²)
public class Solution
{
public int[][] ReverseSubmatrix(int[][] grid, int x, int y, int k)
{
// Iterate through the first half of the rows in the submatrix
for (int row = x; row < x + k / 2; row++)
{
// Calculate the corresponding row from the bottom to swap with
int mirrorRow = x + k - 1 - (row - x);
// Swap all elements in the current row with the mirror row
for (int col = y; col < y + k; col++)
{
// Perform the swap using a temporary variable
int temp = grid[row][col];
grid[row][col] = grid[mirrorRow][col];
grid[mirrorRow][col] = temp;
}
}
return grid;
}
}Was this solution helpful?