https://school.programmers.co.kr/learn/courses/30/lessons/68645
import java.util.*;
class Solution {
public int[] solution(int n) {
int[][] triangle = new int[n][];
for (int i = 0; i < n; i++) {
triangle[i] = new int[i + 1];
}
int number = 1;
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
// Down
while (x < n && triangle[x][y] == 0) {
triangle[x++][y] = number++;
}
x--; y++;
// Right
while (y < triangle[x].length && triangle[x][y] == 0) {
triangle[x][y++] = number++;
}
x--; y -= 2;
// Up
while (x >= 0 && triangle[x][y] == 0) {
triangle[x--][y--] = number++;
}
x += 2; y++;
}
List<Integer> result = new ArrayList<>();
for (int[] row : triangle) {
for (int val : row) {
result.add(val);
}
}
int[] answer = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
answer[i] = result.get(i);
}
return answer;
}
}