function solution(n) {
let arr = [];
for(let i=1;i<=n;i++){
arr.push(Array.from({length:i},()=>0));
}
let col_s=0;
let col_e=n-1;
let row_s=0;
let row_e=n-1;
let num=1;
let cnt=0;
let step=1;
for(let i=n;i>=1;i--){
if(step===1){
for(let a=row_s;a<=row_e;a++){
arr[a][col_s]=num;
num++
}
row_s+=1;
col_s+=1;
}
else if(step===2){
for(let b=col_s;b<=col_e;b++){
arr[row_e][b]=num;
num++
}
col_e-=1;
row_e-=1;
}
else if(step===3){
for(let c=row_e;c>=row_s;c--){
arr[c][arr[c].length-1-cnt]=num
num++
}
row_s+=1;
col_e-=1
cnt+=1
}
step++;
if(step>3)step=1;
}
return arr.flat();
}