문제 설명
https://school.programmers.co.kr/learn/courses/30/lessons/181832

제한 사항

입출력 예


풀이
class Solution {
public int[][] solution(int n) {
int[][] answer = new int[n][n];
int nextIndex = 1;
int count = 0;
int row = 0, col = -1;
while (true) {
for (int j = 0; j < n; j++) {
count++;
col += nextIndex;
answer[row][col] = count;
}
if (count == answer.length * answer.length) break;
n--;
for (int j = 0; j < n; j++) {
count++;
row += nextIndex;
answer[row][col] = count;
}
nextIndex *= -1;
}
return answer;
}
}
후기
레벨 0을 정답률 낮은 순으로 정렬하면 레벨 1 같은 문제를 만날 수 있다.
이 문제는 방향 전환만 잘 하면 풀 수 있다. 어떤 패턴이 반복되는지 생각해 내는 것이 문제이다.
'코딩테스트 (프로그래머스) > Java' 카테고리의 다른 글
| [프로그래머스][JAVA][Lv. 2] 리코쳇 로봇 (0) | 2023.08.30 |
|---|---|
| [프로그래머스][JAVA][Lv. 0] 평행 (0) | 2023.08.30 |
| [프로그래머스][JAVA][Lv. 2] H-Index (0) | 2023.08.28 |
| [프로그래머스][JAVA][Lv. 2] n^2 배열 자르기 (0) | 2023.08.28 |
| [프로그래머스][JAVA][Lv. 2] 연속 부분 수열 합의 개수 (0) | 2023.08.28 |