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.
constmatrixPrinter=(matrix:number[][]):void=>{matrix.forEach(row=>console.log(row.join(' ')));}functionmatrixRotation(matrix:number[][],r:number):void{/** * the size of the maxtrix at circle 1 */constbound_y=matrix.lengthconstbound_x=matrix[0].length/** * read matrix and bind it to [hmap] to easy calculate/reposition after */lethmap:{[cordinate:string]:number}={}matrix.forEach((row,y)=>row.forEach((cell,x)=>{// counting [c], the circle indexed, start from 1letc=1while(true){if(y+1==c||y+1==bound_y-c+1)breakif(x+1==c||x+1==bound_x-c+1)breakc++}hmap[`${x+1}:${y+1}:${c}`]=cell;}))/** * calculate/reposition each number in [hmap] * * + [hmap_temp] create to holding reposition number, avoid to override value * + [x],[y] is cordinate of number * + [v] is the number * + [c] is the circle index where the number is in (from outside to inside, start from 1) * + [rc] is the remaining rotations of the circle [c] * * Q: why each number have [c] and [rc]? * A: cause each number can be on different circle, different circle will need different * number of [r] to complete fully rotation * * Q: why [rc] look so complicated? * A: the hour hand at 10AM today and 10AM tomorow is the same. we dont need to calculate * the between. save resources and time. * * EG: with input [r]=20 and matrix [5x4], we have 2 circle to calculate * * circle 1 * moves to complete fully circe: (5 - 1) * 2 + (4 - 1) * 2 = 14 * moves we need to real calculate: 20 % 14 = 6 * * circle 2 (the size reduce from 5x4 to 3x2, -2 from each side) * moves to complete fully circe: (3 - 1) * 2 + (2 - 1) * 2 = 6 * moves we need to real calculate: 20 % 6 = 2 */lethmap_temp:typeofhmap={}Object.keys(hmap).forEach(key=>{letx=Number(key.split(':')[0]);lety=Number(key.split(':')[1]);letc=Number(key.split(':')[2]);letv=Number(hmap[key]);letc_size={x:bound_x-(c-1)*2,y:bound_y-(c-1)*2}letc_rotations=r%((c_size.x-1)*2+(c_size.y-1)*2)while(c_rotations>0){letdirection:'up'|'down'|'left'|'right'// calculate move direction (for number on the edge) ...if(x==c)direction='down'if(x==bound_x-c+1)direction='up'if(y==c)direction='left'if(y==bound_y-c+1)direction='right'// calculate move direction (for number on the corner) ...if(x==c&&y==c)direction='down'if(x==c&&y==bound_y-c+1)direction='right'if(x==bound_x-c+1&&y==bound_y-c+1)direction='up'if(x==bound_x-c+1&&y==c)direction='left'// this is why using [hmap] is easyer, just change [x] and [y] to move the numberswitch(direction){case'up':y--;breakcase'down':y++;breakcase'left':x--;breakcase'right':x++;break}c_rotations--}// store number and it new cordinate to new [hmap]hmap_temp[`${x}:${y}`]=v})// now we got new [hmap] stored rotated matrix info, print itmatrixPrinter(newArray(bound_y).fill(0).map((row,y)=>newArray(bound_x).fill(0).map((_,x)=>hmap_temp[`${x+1}:${y+1}`])));}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Matrix Layer Rotation
You are viewing a single comment's thread. Return to all comments →
My anser in Typescript, explain includes