0% found this document useful (0 votes)
4 views2 pages

Matrix Column Transformation Algorithm

Uploaded by

aqsakhan14251
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Matrix Column Transformation Algorithm

Uploaded by

aqsakhan14251
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# Approach

<!-- Describe your approach to solving the problem. -->


- Iterate through each column of matrix with the help of two for loops. **Outer for
loop runs from 0 to length of row** i.e `columns` and **inner for loop runs from 0
to length of matrix** i.e `rows`.
Hint :- **Just make row -> column and column -> row.**
E.g :-
![[Link]]([Link]
0b29068461c4_1723792191.[Link])

```
for row in range(len(matrix[0])): # 0 to 2 cols = 3
for col in range(len(matrix)): # 0 to 3 rows = 4
print(matrix[col][row])

First Iteration:-
row = 0
col = 0
matrix[col][row] = matrix[0][0] = -1

row = 0
col = 1
matrix[col][row] = matrix[1][0] = 2

row = 0
col = 2
matrix[col][row] = matrix[2][0] = 4

row = 0
col = 3
matrix[col][row] = matrix[3][0] = -1

Here is how we get our first column i.e [-1,2,4,-1].


```

# Complexity
- Time complexity: O()
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O()


<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```
class Solution:
def modifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
for row in range(len(matrix[0])):
neg_position = []
max_element = float('-inf')
for col in range(len(matrix)):
if matrix[col][row] > max_element:
max_element = matrix[col][row]
if matrix[col][row] == -1:
neg_position.append((col,row))
if neg_position:
for x in neg_position:
matrix[x[0]][x[1]] = max_element

return matrix
```

You might also like