Flipping Submatrix Vertically in Java

Day 80: The Vertical Flip 🔄 Problem 3643: Flip Square Submatrix Vertically Today’s task was all about in-place matrix manipulation. The goal: flip a k×k submatrix vertically within a larger grid, starting from a given (x,y) coordinate. The Strategy: • Two-Pointer Logic: Instead of creating a copy, I used a row-swapping approach. By iterating only halfway through the submatrix rows (k/2), I could swap the top rows with the bottom ones. • Coordinate Mapping: Carefully mapped the indices so that for every row i, its "mirror" row was x+k−1−(i−x). • In-Place Efficiency: This approach keeps the space complexity at O(1) while the time complexity stays a lean O(k²), making it as fast as physically possible to touch every element. Directly manipulating the grid is always more satisfying than allocating extra memory. It’s all about those clean, symmetrical swaps. 🚀 #LeetCode #Java #MatrixManipulation #Algorithms #ProblemSolving #DailyCode

  • text

To view or add a comment, sign in

Explore content categories