0% found this document useful (0 votes)
3 views

Modify The Matrix Solution

Uploaded by

aqsakhan14251
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Modify The Matrix Solution

Uploaded by

aqsakhan14251
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# 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 :-
![image.png](https://assets.leetcode.com/users/images/d766a48e-af88-423b-b9c1-
0b29068461c4_1723792191.098882.png)

```
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