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
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.
@@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.
Step 1 take transpose
Step 2 reverse row
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.
What is the intuition behind this approach ? If not intuition, is it just by observation ?
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
Also, from the transposed matrix mT:
>>>list(map(operator.methodcaller('reverse'), mT))
does it inplace.
You are the best man!!! thanks
couldn't we also do matrix[i].reverse() in the second loop?
To conceptualize this, I tried flipping my notebook without rotating it. In every case this worked.
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.
Is this considered in-place modification -
for idx, lst in enumerate(map(reversed, zip(*matrix))):
matrix[idx] = list(lst)
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
any tips on how to learn algorithms and data structures?
Amazing
Thanks!
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