www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Reshape the Matrix

Last update: 5/6/2017 11:34:00 PM

By Fatih KABAKCI

The problem is to create a function that reshapes the matrix to a new form that satisfies the matrix size, in another words, does not violate the rules in terms of matrix laws.

Problem #566 - Reshape the Matrix

Description: In MATLAB, there is a very useful function called "reshape", which can reshape a matrix into a new one with different size but keep its original data.

You"re given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.

The reshaped matrix need to 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.

Example 1:
Input:
nums =
[[1,2],
[3,4]]
r = 1, c = 4
Output:
[[1,2,3,4]]

Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

Example 2:
Input:
nums =
[[1,2],
[3,4]]
r = 2, c = 4
Output:
[[1,2],
[3,4]]

Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix..

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.ReshapeMatrix;

/**
 * @author fkabakci 
 * Problem Description: In MATLAB, there is a very useful function called "reshape", 
 * which can reshape a matrix into a new one with different size but keep its original data.
 * 
 * You are given a matrix represented by a two-dimensional array, and two positive integers r and c 
 * representing the row number and column number of the wanted reshaped matrix, respectively.
 * 
 * The reshaped matrix need to 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.
 * 
 * Example 1:
 * Input: 
 * nums = 
 * [[1,2],
 * [3,4]]
 * 
 * r = 1, c = 4
 * 
 * Output: [[1,2,3,4]]
 * 
 * Explanation: The row-traversing of nums is [1,2,3,4]. 
 * The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
 * 
 * Example 2:
 * 
 * Input: 
 * nums = 
 * [[1,2],
 * [3,4]]
 * 
 * r = 2, c = 4
 * 
 * Output: 
 * [[1,2],
 *  [3,4]]
 *  
 *  Explanation:
 *  There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.

 * Solution:
 * 1. Check the condition that if the intended new matrix applies the rules
 * 	  If so, transfer all the data to the new matrix.
 *    Otherwise, return the same matrix.
 */

public class Solution {
	public int[][] matrixReshape(int[][] nums, int r, int c) {
		int n = nums.length, m = nums[0].length; 
		if(n * m != r * c) return nums;
		
		int [][] matrix = new int[r][c];
		int row = 0, col = 0;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				matrix[row][col] = nums[i][j];
				if(++col == c) {
					col = 0;
					row++;
				}
			}
		}
        return matrix;
    }
}

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.