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.
Python implementation within time constraints.
The idea is to get a linear list for a given circle and set the starting pointer to where it should be after rotating N times instead of rotating each circle N times.
defrotate(matrix:list[list[int]],count:int):m=len(matrix)n=len(matrix[0])# given circle forrinrange(min(m,n)// 2):max_x=n-r-1max_y=m-r-1# flattening the circlecircle=(# top[matrix[r][i]foriinrange(r,max_x+1)]+# right[matrix[y][max_x]foryinrange(r+1,max_y)]+# bottom[matrix[max_y][i]foriinrange(max_x,r-1,-1)]+#left[matrix[y][r]foryinrange(max_y-1,r,-1)])length=len(circle)pointer=count%lengthidx=pointer# topforiinrange(r,max_x+1):matrix[r][i]=circle[idx]idx=idx+1ifidx<length-1else0# rightforiinrange(r+1,max_y):matrix[i][max_x]=circle[idx]idx=idx+1ifidx<length-1else0# bottom foriinrange(max_x+1,r,-1):matrix[max_y][i-1]=circle[idx]idx=idx+1ifidx<length-1else0# leftforiinrange(max_y,r+1,-1):matrix[i-1][r]=circle[idx]idx=idx+1ifidx<length-1else0defmatrixRotation(matrix,r):rotate(matrix,r)forrowinmatrix:print(*row)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Matrix Layer Rotation
You are viewing a single comment's thread. Return to all comments →
Python implementation within time constraints. The idea is to get a linear list for a given circle and set the starting pointer to where it should be after rotating N times instead of rotating each circle N times.