Rotate Image - Matrix - Leetcode 48

Rotate Image - Matrix - Leetcode 48

NeetCode

3 года назад

215,923 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@burburchacha
@burburchacha - 03.12.2023 17:30

I hope you realise that you have a gift in explaining difficult concepts

Ответить
@ethanking123
@ethanking123 - 23.11.2023 05:33

Could you provide a C++ solution? The C++ approach on your website appears to be a direct copy from LeetCode, which differs from the demonstration in this video

Ответить
@wij8044
@wij8044 - 09.11.2023 21:39

Easier solution for nXn matrix! Neetcode solution may be better suited for nXm matrix.

function rotate(matrix: number[][]): void {
const n = matrix.length;

// Transpose the matrix, starting from i = 1
for (let i = 1; i < n; i++) {
for (let j = i; j < n; j++) {
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
}
}

// Reverse each row
for (let i = 0; i < n; i++) {
matrix[i].reverse();
}
}

Ответить
@krishnateja6428
@krishnateja6428 - 28.10.2023 17:29

Cleanest explanation I have ever seen. Thank you!

Ответить
@franly656
@franly656 - 26.10.2023 20:16

hi neetcode, i have a question, how to rotate matrix 45 degrees so the matrix size will increase, i trying to make the solution and now i want to give up (i have tried straight 5 hours btw). Can you make this solution a video pls.

Ответить
@preetirani8272
@preetirani8272 - 26.09.2023 13:39

List2 = []
for x in range(len(matrix[0])):
List1=[]
for y in matrix[::-1]:
list1.append(y[×])
List2.append(list1)
return list2


Why this code is giving wrong output in leetcode but right output in local jupyter notebook

Ответить
@idgafa
@idgafa - 24.09.2023 17:53

r seems like always n - l, because the matrix is nxn

Ответить
@msabhinavchandra4174
@msabhinavchandra4174 - 23.09.2023 08:22

great

Ответить
@freyappari
@freyappari - 14.09.2023 17:53

My solution:
def rotate(self, matrix: List[List[int]]) -> None:
N = len(matrix)

for y in range(N // 2):
for x in range(y, N - y - 1):
first = matrix[y][x]
matrix[y][x] = matrix[-x - 1][y]
matrix[-x - 1][y] = matrix[-y - 1][-x - 1]
matrix[-y - 1][-x - 1] = matrix[x][-y - 1]
matrix[x][-y - 1] = first


------- or

class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
N = len(matrix)

for y in range(N // 2):
for x in range(y, N - y - 1):
rot_x, rot_y = x, y

for i in range(3):
matrix[rot_y][rot_x], matrix[-rot_x - 1][rot_y] = (
matrix[-rot_x - 1][rot_y],
matrix[rot_y][rot_x],
)
rot_x, rot_y = rot_y, -rot_x - 1

Ответить
@dineshraj5957
@dineshraj5957 - 20.08.2023 20:27

The best explanation by far of the layer rotation method. Damn it. The best!!

Ответить
@barskarakus1036
@barskarakus1036 - 11.08.2023 05:28

j=0
for i in zip(*matrix):
matrix[j]=reversed(i)
j+=1

Ответить
@yabgdouglas6032
@yabgdouglas6032 - 10.08.2023 06:58

my ego has made me attempt this problem almost 3 hours – thank you for this clean explanation!

Ответить
@Kenny-st8cg
@Kenny-st8cg - 12.07.2023 21:25

Since its python you can just do

def rotate(self, matrix: List[List[int]]) -> None:
n = len(matrix)
l, r = 0, n - 1

while l < r:
top, bottom = l, r
for i in range(r - l):
(
matrix[top + i][r],
matrix[top][l + i],
matrix[bottom - i][l],
matrix[bottom][r - i]
) = (
matrix[top][l + i],
matrix[bottom - i][l],
matrix[bottom][r - i],
matrix[top + i][r]
)

r -= 1
l += 1


So theres no need for a temp variable, in that case we also wouldnt have to care about doing it in reverse.

But I think readablitiy suffers a little

Ответить
@wakka13371
@wakka13371 - 05.07.2023 19:41

To say this is time O(n^2) is subtly misleading/not best way to express the time complexity. While I understand that algebraically "n x n = n^2", when expressing the algo itself in time complexity, O(n^2) could be interpreted as a quadratic runtime. Since the algo only looks at each cell once, the algo is, in fact, linear, so it would be better to express "n x n" as just "M" and therefore O(M).

Ответить
@ishaanjain4973
@ishaanjain4973 - 04.07.2023 10:25

I cant explain you how much this channel helps me !! Other channels just tell the transpose method which is not so intuitive, you always tell solutions which I can think in future in real interviews and exams. Thanks a lot Neetcode !! Keep up the good work man <3

Ответить
@prankobano9076
@prankobano9076 - 08.06.2023 20:24

Cool beans dude!

Ответить
@khaleabhishek5023
@khaleabhishek5023 - 08.06.2023 16:04

this code is not working now

Ответить
@jagrutitiwari2551
@jagrutitiwari2551 - 10.05.2023 18:44

The explanation was amazing. I find subtracting i a little confusing. Though dry run help me understand how it works. How do I remember it when I code?

Ответить
@nikhilgoyal007
@nikhilgoyal007 - 08.04.2023 21:58

how does leetcode verify solutions to problems where there is no return value but just in place change? thank you!

Ответить
@pengmick2046
@pengmick2046 - 18.03.2023 06:47

Your explanation is so good

Ответить
@josecarlosfontanesikling306
@josecarlosfontanesikling306 - 15.03.2023 02:12

It's possible to do this without any extra memory at all.
Suppose you have variables x and y. You can swap them like this
y=y+x
x=y-x
y=y-x
This is all you need to transpose a matrix and to swap columns or rows. This rotation is just a transposition followed by inverting the order of the rows.

Ответить
@alexeymelezhik6473
@alexeymelezhik6473 - 08.03.2023 22:11

"top" and "bottom" variables are not necessary, one could replace them by "l" and "r"

Ответить
@AbhishekPandey-qg6mo
@AbhishekPandey-qg6mo - 01.03.2023 15:53

Can anyone tell what is wrong with c++ using the same approach??

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n=matrix.size();
int left = 0, right = n-1;
while(left <= right){
int top = left;
int bottom = right;
for(int i=left; i<right; i++){
//move top-left element in a temp variable
int temp = matrix[top][left+i];

//move bottom-left to top-left
matrix[top][left+i] = matrix[bottom-i][left];

//move bottom-right to bottom left
matrix[bottom-i][left] = matrix[bottom][right-i];

//move top-right to bottom-right
matrix[bottom][right-i] = matrix[top+i][right];

//move top-left (temp) to top-right
matrix[top+i][right] = temp;
}
left++;
right--;
}
}
};

Ответить
@dinamohamed13600
@dinamohamed13600 - 27.02.2023 13:21

fantastic teacher!

Ответить
@jamalyarfoor5798
@jamalyarfoor5798 - 20.02.2023 16:46

I beat 97% time and 70% memory by making a new list and then updating the original list to be that new list

Ответить
@ledinhanhtan
@ledinhanhtan - 14.02.2023 17:26

Beautiful code~

Ответить
@KaushikChavali
@KaushikChavali - 12.02.2023 08:09

I like the intuitively named variables.

Ответить
@eltaylor778
@eltaylor778 - 26.01.2023 09:04

Great explanation, does anyone know why the range has to be range(right- left) instead of range(left,right)? The range(left,right) works on many of the smaller test cases but does not work on many of the harder ones. Help!

Ответить
@sanketkoli8641
@sanketkoli8641 - 20.01.2023 03:09

Very nice explanation. Thanks NeetCode!

Ответить
@fedianiefuna9235
@fedianiefuna9235 - 31.12.2022 23:48

How is the complexity O(n^2) wouldn't it be O(n) since big O notation cares about the size of our data and we were given an n x n data set?

Ответить
@maamounhajnajeeb209
@maamounhajnajeeb209 - 31.12.2022 08:21

as usual, you made it easy man

Ответить
@samuelsun7347
@samuelsun7347 - 21.12.2022 16:44

what about make one round to 1D array and change the order for them ?

Ответить
@devenlad
@devenlad - 20.12.2022 07:11

Really good explanation

Ответить
@Nick-qy7lk
@Nick-qy7lk - 20.12.2022 05:37

Hi guys, does anyone know why the for loop (line 9) has to be range(right - left)? I thought range(left, right - 1) would work but I've tried it and it doesn't on the harder test cases. I can't wrap my head around why. Would appreciate any help.

Ответить
@sunnyding602
@sunnyding602 - 18.12.2022 00:11

Thanks for the clear explanation. I can understand it while I am in a food coma. lol

Ответить
@huimingli9207
@huimingli9207 - 15.12.2022 04:00

This is really a pure math problem. rotating a cell 90 degree, the index/coordinate change is from (x,y) -> (y, n-1-x).

Ответить
@plotfi1
@plotfi1 - 10.12.2022 10:29

I find the standard rotation technique to be pretty frustrating to think about. Instead ive found its easier to invert the values on the diagonal then reverse the rows.

Ответить
@RandomShowerThoughts
@RandomShowerThoughts - 30.11.2022 06:29

very intuitive

Ответить
@MahfujurRahmanAraf
@MahfujurRahmanAraf - 23.11.2022 17:53

Dude you are amazing

Ответить
@harishdukkipati8921
@harishdukkipati8921 - 12.11.2022 02:44

Your explanation is very clear and I understand how it works. However, when I try it on leetcode it says my time limit is exceeded. Would you have any reason for why that would be the case.

Ответить
@template_typename_t3128
@template_typename_t3128 - 11.11.2022 01:58

You can transpose the matrix in place and reverse each row to achieve the same result

Ответить
@chichiem2397
@chichiem2397 - 08.11.2022 00:11

These videos are great, this one in particular is perfect. I struggled with this a lot until I checked out this video. Awesome stuff!

Ответить
@suvrobanerjee2399
@suvrobanerjee2399 - 01.11.2022 04:49

Thanks for such a clear explanation.

Ответить
@peterosi192
@peterosi192 - 18.10.2022 15:40

Now this... I like!

Ответить
@johnwick2018
@johnwick2018 - 20.09.2022 21:31

simpler method is to transpose it and reverse rows.

Ответить
@tusharsingh7926
@tusharsingh7926 - 08.09.2022 05:39

Thank You Brother for this amazing video.............🙏🙏🙏🙏🙏🙏

Ответить
@harishsn4866
@harishsn4866 - 02.09.2022 05:08

Thank you so much. You're the best.

Ответить