Reshaping a Matrix with LeetCode

🚀 Day 56 of my #100DaysOfCode Journey Today, I solved LeetCode problem Reshape the Matrix Problem Insight: We are given a 2D matrix and two integers r and c. The task is to reshape the matrix into dimensions r × c while keeping the same elements in row-major order. If reshape is not possible, return the original matrix. Approach: • First, check if reshape is possible using condition → total elements must match (m × n == r × c) • Traverse the matrix using a single loop (treating it as a linear array) • Map old indices to new indices using division and modulo • Fill the new matrix in row-wise order Code Idea Used: Index mapping technique (1D simulation of 2D matrix) Time Complexity: O(m × n) | Space Complexity: O(1) (excluding output matrix) Key Takeaway: Understanding how to convert between 1D and 2D indexing (i/m, i%m) is very powerful for matrix transformation problems. #LeetCode #DSA #ProblemSolving #Java #CodingJourney #100DaysOfCode #Arrays #MatrixProblems

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories