Skip to content

Commit 5461093

Browse files
committed
added Reshape the Matrix (easy)
1 parent 7c1880f commit 5461093

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

Easy/ReshapeMatrix/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# Reshape the Matrix
3+
[Leetcode Link](https://leetcode.com/problems/reshape-the-matrix/)
4+
5+
## Problem:
6+
7+
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.
8+
9+
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.
10+
11+
The reshaped matrix need to be filled with all the elements of the original matrix in the same **row-traversing** order as they were.
12+
13+
If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
14+
15+
## Example:
16+
17+
```
18+
Input:
19+
nums =
20+
[[1,2],
21+
[3,4]]
22+
r = 1, c = 4
23+
Output:
24+
[[1,2,3,4]]
25+
Explanation:
26+
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.
27+
```
28+
```
29+
Input:
30+
nums =
31+
[[1,2],
32+
[3,4]]
33+
r = 2, c = 4
34+
Output:
35+
[[1,2],
36+
[3,4]]
37+
Explanation:
38+
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
39+
```
40+
41+
## Note:
42+
43+
1. The height and width of the given matrix is in range [1, 100].
44+
2. The given r and c are all positive.

Easy/ReshapeMatrix/solution.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
4+
def matrixReshape(nums: List[List[int]], r: int, c: int) -> List[List[int]]:
5+
# if r and c not valid, return original list
6+
if len(nums)*len(nums[0]) != r*c:
7+
return nums
8+
numCol = c
9+
reshaped = list()
10+
col = list()
11+
# iterate over nums and decrement numCol by 1 and when numCol reaches 0, append that col list to reshaped list, and reset numCol to c
12+
for i in range(len(nums)):
13+
for j in range(len(nums[i])):
14+
col.append(nums[i][j])
15+
numCol -= 1
16+
if numCol == 0:
17+
# reset col list for next row
18+
reshaped.append(col)
19+
col = list()
20+
numCol = c
21+
return reshaped
22+
23+
24+
# test driver
25+
nums = [[1, 2, 3], [4, 5, 6]]
26+
r = 3
27+
c = 2
28+
print("Input: nums =", nums)
29+
print("r =", r, ", c =", c)
30+
print("Output:", matrixReshape(nums, r, c))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Languages used: Java and Python
2626
- [Maximize Sum Of Array After K Negations](Easy/MaximizeSumAfterKNegations)
2727
- [House Robber](Easy/HouseRobber)
2828
- [Majority Element](Easy/MajorityElement)
29+
- [Reshape the Matrix](Easy/ReshapeMatrix)
2930
- Medium
3031
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
3132
- [Distribute Coins in Binary Tree](Medium/DistributionCoinsInBinaryTree)

0 commit comments

Comments
 (0)