To cyclically rotate a grid by K layers, first calculate the number of layers as min(R, C)/2, then for each layer, extract its perimeter elements in clockwise order, rotate them by K positions using modular arithmetic (K mod perimeter), and place them back; this approach handles large K values efficiently through modular math and processes each concentric layer independently.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Daily Leetcode #2230: May 09, 2026 - 1914. Cyclically Rotating a GridAdded:
Hey, hey everybody. This is Larry. This is another day challenge. Hit the like button, subscribe button, join me on Discord. Let me know what you think about everything. Uh yeah, here I am in the Philippines.
You can see it's beautiful behind me.
I'll do a spin, but I'm actually on a in another mountain top on on a thing, and it's pretty uh I don't want to do a spin, but we have another thing that could do a spin. So, let's do that.
Let's go.
Heat. Heat.
Heat. Heat.
Heat. Hey, Heat.
Hey, hey everybody. This is Larry. This is day nine of the legal day challenge.
the like button, subscribe button, join me on Discord. Let me know what you think about today's problem. Uh yeah, hope you like the intro. Um yeah, I did a a little bit of a dive. Oh, wait. No, was that No, no, yeah. Yeah, that's today. I did a little bit of a dive. I went to the mountains. Uh I might Yeah.
Well, I was going to say I have to wake up in like four four hours. So, uh, this may be a very hopefully this is a short video. Uh, we'll see how the intro goes.
Um, hope you enjoy it as usual. I mean, you know, uh, yeah. All right. Thanks for loading lead code. Uh, let's get started then. Without further ado, I'm going to rush. Then let's go. We have 1914. Also, if you really like the intro, leave a comment because I don't know if people watch it or people just skip it. And just nice. And also, if you actually like it, you know, let me know, too. uh here in SEU in Philippines.
Yeah. Uh today we have 1914 silically citically rotating a grid. You have a grid that okay they're both even. Okay.
And integer K layers. Okay. Fine.
Okay. So each layer um Okay. So some so add layers you clockwise count uh uh or the other way around. Oh no. Is it all the same? Okay, it's the same rotation every times.
Okay, so um yet another annoying problem, but also like not impossible, right? I mean the I thought we did something similar, but the first thing of course is checking the the constraints and checking K is 10^ the 9 means that we have to do some modular math, but that's okay, right?
uh and we have to just you know there's a lot of thinking I mean it's not hard I don't think because once you get it like or there's no data structure and something that you know you have to try to figure out or something like that right but um but yeah um but yeah uh that's basically the idea I mean maybe we can do I mean, we had uh a problem, right, with something like this uh a couple of days ago. So, we should probably be able to figure it out. Um so, how many layers there, right? Uh layers is just um well, layers, let's just say layers is equal to min of two and C divided by two, right?
Do they even tell you this?
I don't Maybe they even tell you explicitly. By the way, it's straightforward to see, right? Um just by symmetry.
Now what? Well, I mean we do have to do it layer by layer because um each layer will have different number of numbers in general, right? So maybe we do like something like for layer and layers. Um I don't know. Uh let's just say south layer, right? Um and then we just return grid. Maybe we do it in price. I don't know, right?
Uh and then for layer, how many items are there? Um how many items are there? So for layer zero because we'll be we'll start from zero right for layer Z for layer zero we have um it's going to be all by secret right so okay so layers zero is going all by secret we'll ignore the the the whatever for now layer one is going to be all minus - 2 * c minus 2 right and then so forth. So I think once you write it out like this it come pretty straight uh forward to you know to just visualize right um and you also just make the argument that you know for each thing you subtract one side right so okay so then now um yeah so then how many so this is the the um the the perimeter that we're doing so I have to calculate the perimeter right so Maybe we'll even just write it out. Um right. So items is you go to um and the perimeter is there number of ways to write it. But for example for the R by R* C isn't quite the representation. Maybe we want R by C instead is a way phrasing. And then it's it's not it's of course just to be clear not two times R time C, right? Because then now you're double counting the the corners or in this case four times corner. I guess you always have four corners. So you only for this problem, you only always Yeah. Um and because they're always even, you always have four corners. So that's actually fine, right? So then maybe you have like rows is equal to R minus layer time 2 right um and cost is equal to C minus layer * 2 right um and then items is equal to just rows times columns minus 4 right for the because for each of the four corners we double count okay and then now we have um let's just call K is equal to K mod items is what I wanted to start it off and then we just do the Now we do the rotation to part, right?
Um I'm just reaching over to Press.
Um, how do I want to write this?
I mean, you know, uh this is definitely maybe I've been saying this a lot for this week for maybe also obvious reasons. Um which is that, you know, there are good ways to solve this maybe and there also bad ways to solve it, but if they're bad and you still solve it, it's still fine, right? And that's what I'm trying to figure out. Um like and by it's not even a bad way, but it's just like a more verbose way, right? with a lot of if statements. I'm trying to figure out if I want to if I if I want to use the if state or and maybe that's fine. And it's not even necessary if statements. You just break it down to case analysis, right?
Um, do I want to do it in place? Maybe I don't want to do it in place. Let's not do it in price. Um because because I don't know. I think maybe I try to I was just lazy. Do I just I don't really have a good reason to do it in place. It just makes things harder for no reason because then now we can just um you know copy over, right? Maybe maybe we'll just write it explicitly. I don't know. Uh top, right? Top. What is top?
Right. Top is when um let's define this real quick.
uh row is equal to it's not zero right I mean zero for the layer zero uh row one for layer one row two for layer two so row is just equal to layer right um yeah and that's it well and then that's the row the column uh is going to go from uh layer to C minus layer Okay.
Uh minus one if you want to do inclusive.
Um okay. So then what does that look like? Um yeah. So n grid of I mean we'll just write it out right. So for C for car and range from layer to C minus layer minus one + one to be inclusive. Um then it is going to be layer of car of C rider is equal to grid uh and we'll just write out manually right. Um, so there it's going to be layer C + one kind of uh layer C + one, right?
Something like this, but not because we don't do it on the last column. So now we remove this again.
Okay. Uh, now let's give it a spin. I know obviously I know that this is not done, but uh we'll take a look for now.
So I I just forgot how to code in Pyfinder turns out.
Um really I mean I could see how okay 0 0 what is that? One one layer is zero.
Uh all minus well one this is going to be one one items is oh huh I mean this is the one by one or two by two case and then now you have one by one wait what why would this be why would this give me uh what is row again row is layers is one which is correct I think R is going to be two two right so 2 - 0 2 - 0 should be 4 * 4 oh no this is 2 * 2 is four um and then we minus corner this is dumb Larry you're dumb this isn't what I meant to do uh I I I don't know but this is a brain That's not how you get the parameter is what I meant, right? Is this what is going on? That doesn't even make I was trying to like make sense. I'm like why? Why? Why is it get w times? Like why would it be rows times columns? I'm just like dumb. Sorry if you're following along. Larry's dumb.
And I'm just doing this so that I can see the output. Ideally, the top is correct is what I'm trying to do. Um this is partially correct, which is kind of what I expect. Um, this is not as correct as I would like. Uh, which one is this?
Uh, one, two. This is this one. Nope.
This. Wait, what?
Did they switch case one case two or something? Oh, no. No. There we go. Oh, no. No. Oh, this is very weird. But, okay, fine. Oh, it's after two rotations.
Huh? Why did I skip this? How did I forget this one? Well, I mean, how did I forget the K point is what I mean. Um, that's annoying.
Then I mean we have to in that case then we just have to do some circular thing for some reason. Sorry friends, I I I I did I mean this is just one move. Um, and we shouldn't do K moves. So then maybe we just do it by indexes, right?
So then I I think that's how I had it.
Wait. So maybe we have um how everybody I mean the other way you can do it is by matrix matrix exponentization uh to do this but that's fine we it's it's we don't need to we can we don't need to Um, okay. I see. Uh, then fine, fine, fine.
So, then we'll just go uh a loop, right?
I I don't know if I'm looping it the right way, but uh we we'll just do it.
So, then we have uh maybe just call it ordering, right? So, then now we have ordering.appen uh layer C, right?
And then now we go um on so we now we did the top.
Now let's do um the right side. We're just going the the direction. Well, technically I think the opposite direction, but whatever. Right. So then now uh in range um it is going to be the same thing really.
It's just that this is going to be C minus L. No, this is R. Then C minus layer minus one, right? And then bottom it's going to be this but in the other direction I think. Um right let's do uh Let's just do this. The reason why I'm doing this, even though it doesn't make sense necessary, is um it's just that it's slightly harder to get wrong. Um because I maybe it's not.
I don't know. Maybe I'm just being silly. But yeah, but this is now left, right?
Uh, it just depends on which symmetry you you think is more interesting to you. Um, but I don't know. I don't know how I feel about it.
This is kind of awkward though. The the symmetry is a little bit awkward.
I finished my tree already.
Okay. Well, and then now we have F4 XY and ordering, right?
Then now um the new grid of XY is equal to grid of oh I guess I have to enumerate this and then new index or K index maybe is equal to index plus K u mod um length of ordering right.
Right. So then now we have um new x new y is equal to ordering of k index and we have nx and y right and that should be good. Decided it the other way like I always do. Uh we still have a negative one. So that's not so good.
Uh, unless I miscounted slash double counted one of them, which maybe is true cuz this one the top we don't we missed the last item. This one we missed the last item. This one No, this should be right. Unless I'm dumb, which is true.
No, this is wrong.
This should be layer. That's the symmetry. I knew that the symmetry looked a little bit weird that like I was like, why did I have this twice? Um, okay. Let's see. Okay, it's still wrong.
Not so good.
Uh, why are you wrong?
Maybe this one's a little bit more illustrative, but I'm missing I'm getting negative ones in the same place, right? In the LA the last element or at least the corner element. Oh, I see.
because because the way that I did the bottom and left, I used the same loop, but this is intentionally missing the last element, but then I did the reverse thing, right? So maybe I did mess myself up anyway. Um, so we did want to skip this one, and we don't want to skip this one. So yeah, I think that's how it ends up. Maybe.
Is this a negative? Because my screen is too early. I guess that's why. Okay, so that looks good now. Um, no reason to. I mean, this is crazy code, but uh, we'll give a YOLO submit just because I'm lazy and tired. That always scares me because usually I just wait for it, but I guess it's the new UI always as in like two days, but um, okay. Um, and that's it.
That's all I have for this one. Um, I don't know how I feel about it. I mean, it's okay. I mean, we have linear time, linear space. Maybe well we can't do better than linear space per se depend on how you want to think about the definition of in place but but otherwise like that's the size of output so you know can do better than linear um and that's all I have for today that's all I have for this one let's call it okay for now um yep thanks for watching stay good stay healthy to good mental health I'll see y'all later and take care bye-bye Bye.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsโข2026-05-28
How agent o11y differs from traditional o11y โ Phil Hetzel, Braintrust
aiDotEngineer
450 viewsโข2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation๐ฏโ
LearnwithSahera
1K viewsโข2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsโข2026-05-29
Search Algorithms Explained in 60 Seconds! ๐ค๐จ
samarthtuliofficial
218 viewsโข2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsโข2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 viewsโข2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 viewsโข2026-06-01











