Link: https://leetcode.com/problems/rotate-array/
Solution:
Topics: in-place
Intuition
There is a very cool trick here to solve the problem in constant space. The trick is somewhat obvious but admittedly, somehow I could not come up with it until reading hint 2. Basically the idea is to reverse the list and then again reverse the rotated partitions. Thats it.
For example:
[1,2,3,4,5,6] k=3 ----> [4,5,6,1,2,3]
^ ^ ^
Reverse the list:
[6,5,4,3,2,1]
Take a look at the partitions that we want to rotate:
[6,5,4,3,2,1]
^ ^ ^ -----
We can observe that reversing the partitions will yield the correct array:
[4,5,6,1,2,3]
Implementation
Mnemonic
Rotate -⇒ Reflect! (this works for rotate matrix too, minus the transpose)
Review 1
Happy to have solved this one quickly in constant space. Progress! Don’t forget to mod k.