Another way of looking at this would be all the elements of the first column would become the first row but in the reverse order and the same goes for all the columns n=len(matrix) ans =[] for _ in range(n): ans.append([0*n]*n) i=0 m=n while i
Because we would end up swapping elements twice leaving the board essentially unchanged. (i,j) would swap with (j,i) but when u get to (j,I) you’re swapping it again with (i,j).
I solved by implementing it to n and adding a if statment "if i>= j:". If you think about it visualy, the (i+1 to n) or "if i>= j" is always selecting a element that is after the main diagonal and swapping with one before.
Without knowing this, what I found out is that you can rotate elements by "levels". Imagine the matrix as an onion, the first level would be the outermost layer consisting of the first and last row, and first and last column. So after rotating every element in the first level, you are left with an inner matrix of size N-1. So all you need to do is repeat the same process N // 2 times. N being the matrix' size. Here's the code: ``` level = 1 N = len(matrix) while level
@@ARkhan-xw8ud bro I am sorry for Mis typed but this question was asked in TCS 2023 September and there is sort colors problem on leetcode that same question was asked too as passenger luggage risk problem
Master Data Structures & Algorithms For FREE at AlgoMap.io!
I love this series. Helps in understanding DSA a bit optimized way.
When transposing the inner loop (i+1, n) definitely takes some extra thought. Wouldn't have been able to come up with that in an interview setting.
Step 1 take transpose
Step 2 reverse row
What is the intuition behind this approach ? If not intuition, is it just by observation ?
Another way of looking at this would be all the elements of the first column would become the first row but in the reverse order and the same goes for all the columns
n=len(matrix)
ans =[]
for _ in range(n):
ans.append([0*n]*n)
i=0
m=n
while i
In transpose part why the loop for J runs from (i+1 to n) instead of ( only n)
Because we would end up swapping elements twice leaving the board essentially unchanged. (i,j) would swap with (j,i) but when u get to (j,I) you’re swapping it again with (i,j).
I solved by implementing it to n and adding a if statment "if i>= j:". If you think about it visualy, the (i+1 to n) or "if i>= j" is always selecting a element that is after the main diagonal and swapping with one before.
Also, from the transposed matrix mT:
>>>list(map(operator.methodcaller('reverse'), mT))
does it inplace.
couldn't we also do matrix[i].reverse() in the second loop?
You are the best man!!! thanks
any tips on how to learn algorithms and data structures?
To conceptualize this, I tried flipping my notebook without rotating it. In every case this worked.
Amazing
Thanks!
Is this considered in-place modification -
for idx, lst in enumerate(map(reversed, zip(*matrix))):
matrix[idx] = list(lst)
Without knowing this, what I found out is that you can rotate elements by "levels". Imagine the matrix as an onion, the first level would be the outermost layer consisting of the first and last row, and first and last column. So after rotating every element in the first level, you are left with an inner matrix of size N-1. So all you need to do is repeat the same process N // 2 times. N being the matrix' size.
Here's the code:
```
level = 1
N = len(matrix)
while level
I feel like vertical reflection would make more sense haha.
matrix = list(map(list, map(reversed, zip(*matrix))))
coming up with these answers are definitely not easy, whoever solves these is cracked lol
TCS question
leetcode
@@ARkhan-xw8ud bro I am sorry for Mis typed but this question was asked in TCS 2023 September and there is sort colors problem on leetcode that same question was asked too as passenger luggage risk problem