이번 문제는 다른 문제보다 훨씬 더 쉽게 느껴졌다.
바탕화면 분석 : 먼저, 주어진 wallpaper 배열을 순회하며 파일이 위치한 가장 왼쪽, 오른쪽, 위, 아래 지점을 찾는다.
좌표사용 : 이렇게 찾은 좌표를 이용해 드래그의 시작점과 끝점을 결정한다.
class Solution {
public int[] solution(String[] wallpaper) {
int[] answer = new int[4];
int top = 51;
int bottom = 0;
int left = 51;
int right = 0;
for(int i = 0; i < wallpaper.length; i++) {
for(int j = 0; j < wallpaper[0].length(); j++) {
if(wallpaper[i].charAt(j) == '#') {
if(i <= top) {
top = i;
answer[0] = top;
};
if(i >= bottom) {
bottom = i;
answer[2] = bottom + 1;
};
if(j <= left) {
left = j;
answer[1] = left;
};
if(j >= right) {
right = j;
answer[3] = right + 1;
};
}
}
}
return answer;
}
}
이 코드에서 bottom과 right에 1을 더하는 이유는 직사각형의 끝점을 포함시키기 위함이다.
쉬운 문제라 더 이상 어떻게 설명을 해야할지 모르겠다..!