Solved Reshape the Matrix problem on LeetCode

🚀 Day 50 of My LeetCode Journey Today, I solved Reshape the Matrix. Problem in short: Given a matrix, reshape it to a new size r x c while keeping the row-traversal order intact. If it’s impossible, return the original matrix. Approach I used: First, check if total elements match (originalRows * originalCols == r * c). Use a single counter to traverse the original matrix and place elements in the new matrix. Map the counter to new matrix indices: row = count / c col = count % c Why this is clean: Simple and intuitive. No need for multiple variables for row and column. Works for any size matrix. Example: Original: 1 2 3 4 Reshaped to 1×4 → 1 2 3 4 This problem taught me how matrix manipulation can be simplified using smart indexing. #100DaysOfLeetCode #CodingJourney #ProblemSolving #DSA #Java #Matrix

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories