Rotate Image - Matrix - Leetcode 48

preview_player
Показать описание


0:00 - Read the problem
3:25 - Drawing explanation
9:55 - Coding explanation

leetcode 48

#microsoft #matrix #python

Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

This is a clear explanation, but definitely still not the simplest. For me, the most straightforward method is to transpose the matrix and reverse each row.
The code is simple and short.

#transpose
for row in range(len(matrix)):
for col in range(row, len(matrix)):
temp = matrix[row][col]
matrix[row][col] = matrix[col][row]
matrix[col][row] = temp

#reverse
for row in matrix:
row.reverse()

Accepted by leetcode.

meowmaple
Автор

This has been one of the toughest problems for me. Very hard to visualize and always used to make mistakes even after multiple attempts. The way that you explained the approach is THE BEST. You made it so crystal clear in visualizing the solution. Thank you so much!

srinadhp
Автор

Just got an offer at amazon. Your videos rock and helped me out so much!

doublegdog
Автор

Thanks a lot for the content mate! No offence to others but I really like your clear accent and structured material which is easy to follow. Hope you keep up posting!

MsSkip
Автор

Bro what an structured approach . Really loved your way of teaching man! You made it look so easy.

drtyharry
Автор

This is the best explanation for this problem. Crystal clear visualization, elegant code. Great job. Thank you so much for posting!

xqfang
Автор

Although this is a good way to do it, I found my way to be a bit simpler once you understand matrix manipulation. Rotating a matrix by 90⁰ is equivalent to flipping the matrix diagonally and then flipping it vertically. First try it out with paper, and once u get it, it's really easy. It doesn't save runtime or anything, but I find it easier in terms of code than to move 4 things at a time layer by layer.

parthpatel
Автор

I gotta say your videos are amazing. I've been grinding LC for the past 3 weeks, I went from struggling to solve even 1 question on the weekly leetcode contest to solving 2 - 3 questions each week. Thank you so much. I've also and will always share your videos and excel sheet on reddit whenever people ask for leetcode tips. Oh and its abit late but congrats on the Google offer!

I hope to one day get into google as well or any other company tbh ( my current tech job kinda blows ) ...

lottexy
Автор

This is so elegant. Have solved multiple of 2d array problems but never thought of accessing the rows literally by [bottom[[R] and [top][L]

sanidhyax
Автор

This is the best explanation of this problem so I've found. Thank you so much for the content! Keep up the good work 👏

annieonee
Автор

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

huimingli
Автор

Best explanation one could ever give for a problem!!!. Thank you for the effort and time you are putting into making all these videos.

jananiadanurrammohan
Автор

Mans went into god mode swapping the elements in reverse

kwaminaessuahmensah
Автор

This code makes the problem look way easier than it is! Love the code and explanation.

emanawel
Автор

Thank you. Best explanation without having to deal with 2 for loops with i, j or recursion and all other BS to be worried about.

suhasdon
Автор

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

ishaanjain
Автор

Great approach! Here you can see if you are confused with variable naming I have used some easy to understand names.Approach is still the same.

void rotate(vector<vector<int>> &matrix)
{
int size = matrix.size();
int startRow = 0;
int startColumn = 0;
int endRow = matrix.size() - 1;
int endColumn = matrix.size() - 1;

while (startRow < endRow && startColumn < endColumn)
{
int current_column_for_start_row = startColumn;
int current_row_for_end_Column = startRow;
int current_column_for_end_row = endColumn;
int current_row_for_start_column = endRow;

int current_size = endColumn - startColumn;

for (int i = 0; i < current_size; i++)
{
int temp =

=

=

=

= temp;





}

startRow++;
startColumn++;
endRow--;
endColumn--;
}
}

vaibhavkhanna
Автор

Your videos are excellent. You do a great job of being super clear! I often come here to Neetcode to see if you have the solution as it is better than the official explanation. Keep up the great work!

sumosam
Автор

Great content Bro :)

Solution in Java:

class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
int right=n-1, left=0;
//neetcode solution video
while(left<right){
for(int i=0; i<right-left; i++){
int top=left, bottom=right;

//save top left
int temp = matrix[top][left+i];
//rotate bottom left to top left

//rotate bottom roght to bottom left

//rotate top right to bottom right

//save top left to top right
matrix[top+i][right]=temp;
}
left++;
right--;
}
}
}

IncrementalNova
Автор

I really like the way u handled minimizing the temp variable swap. very well explained. Thank you so much.

sureshgarine