We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Matrix Layer Rotation
Matrix Layer Rotation
Sort by
recency
|
778 Discussions
|
Please Login in order to post a comment
They called me a mad man. Single pass, 1 nested loop, let's goooooooooo!
So, what we are doing, since we already have the matrix, is calculating the location of the number from the original matrix that should fall on the position we are currently printing. So, instead of transforming the matrix, we are kind of going in the oposite directioin and figuring out what number would be where in the final matrix rather than transforming it.
(The amount of times those ifs were tweaked is not up for discussion.)
Also, do beware, this is not a pretty sight.
One of the idea is by extracting to straps, rotate it, and combine it back as a matrix.
For the last one, combining it back is quite tedious, so I personally prefer to utilize classes (in-place assignments / reference-assignment) so that I dont need to deal the last one (combine it back)
For the strap example, it should be: matrix (height=5, width=4):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
There will be 2 straps:
[1, 5, 9, 13, 17, 18, 19, 20, 16, 12, 8, 4, 3, 2]
and the second one:[6, 10, 14, 15, 11, 7, ]
Javascript
hello everyone
this is my java sol and this it is passing all the tese cases. can anyone give me the suggetion to improve my code public static void printMatrix(List> matrix){ for(int i=0;i> matrix, int r) { // Write your code here int row = matrix.size(); int col = matrix.get(0).size();
}
Last 2 for loops were for debugging, you only need the second one for the actual solution, and GrowthMount can help optimize this process.