Skip to main content

Command Palette

Search for a command to run...

Reshape the Matrix

Published
2 min read
Reshape the Matrix
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data.

You are given an m x n matrix mat and two integers r and c representing the number of rows and the number of columns of the wanted reshaped matrix.

The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

LeetCode Problem - 566

class Solution {
    public int[][] matrixReshape(int[][] mat, int r, int c) {
        // Get the number of rows and columns in the original matrix
        int rows = mat.length;
        int col = mat[0].length;

        // If the total number of elements doesn't match between the original and reshaped matrices, return the original matrix
        if((rows * col) != (r * c)) return mat;

        // Step 1: Flatten the original matrix into a list
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < mat.length; i++){
            for(int j = 0; j < mat[0].length; j++){
                list.add(mat[i][j]); // Add each element of the matrix to the list
            }
        }

        // Step 2: Create the reshaped matrix with r rows and c columns
        int[][] ans = new int[r][c];
        int idx = 0; // Index to track elements in the list

        // Step 3: Fill the reshaped matrix with elements from the list
        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                ans[i][j] = list.get(idx); // Assign elements from the list to the reshaped matrix
                idx++; // Move to the next element in the list
            }
        }

        // Return the reshaped matrix
        return ans;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.