Java

프로그래머스: 바탕화면 정리

Daeryuk Kim 2023. 11. 16. 09:07
class Solution {
    public int[] solution(String[] wallpaper) {
        int[] answer = new int[4];

        int lux = wallpaper.length-1; // 행
        int luy = wallpaper[0].length()-1;
        int rdx = 0; // 열
        int rdy = 0;
      
        for (int r = 0; r < wallpaper.length; r++) {
            for (int c = 0; c < wallpaper[r].length(); c++) {
                if (wallpaper[r].charAt(c) == '#') {
                    if (lux > r) lux = r;
                    if (luy > c) luy = c;
                    if (rdx < r) rdx = r;
                    if (rdy < c) rdy = c;
                }
            }
        }
        answer[0] = lux; 
        answer[1] = luy;
        answer[2] = rdx + 1;
        answer[3] = rdy + 1;
        return answer;
    }


    public static void main(String[] args) {
            String[] wallpaper = {".#...", "..#..", "...#."};
            // Solution 클래스의 인스턴스 생성
            Solution sol = new Solution();

            // 샘플 데이터로 solution 메소드 호출하고 결과 저장
            int[] result = sol.solution(wallpaper);

            // 결과 출력
            System.out.print(result[0]+ ", ");
            System.out.print(result[1]+ ", ");
            System.out.print(result[2]+ ", ");
            System.out.print(result[3]);
    }
}