본문 바로가기
Dev/Algorithm

[프로그래머스] 바탕화면 정리 - Java

by VIPeveloper 2024. 5. 20.
반응형

문제

https://school.programmers.co.kr/learn/courses/30/lessons/172928

 

생각

  • 뭘 물어보려한거지..? 걍 좌표 문제인 것 같고, 어렵지 않게 해결할 수 있었다.
// 1. 2차원 배열을 생성하여 넣기
// 2. maxH,maxW,minH,minW 를 초기화하기
// 3. 배열을 순회하며, 해당 값 채우기
// 4. 리턴

코드

public class Main {
    public static void main(String[] args) {
        System.out.println(solution(new String[] {".#...", "..#..", "...#."}));
        System.out.println(solution(new String[] {"..........", ".....#....", "......##..", "...##.....", "....#....."}));
    }
    public static int[] solution(String[] wallpaper) {
        int maxH = -1,maxW = -1,minH = 55,minW = 55;
        for (int i = 0; i < wallpaper.length; i++) {
            String [] row = wallpaper[i].split("");
            for (int j = 0; j < wallpaper[0].length(); j++) {
                if("#".equals(row[j])){
                    maxH = Math.max(maxH,i);
                    maxW = Math.max(maxW,j);
                    minH = Math.min(minH,i);
                    minW = Math.min(minW,j);
                }
            }
        }

        return new int[]{minH,minW,maxH+1,maxW+1};
    }
}

다른 사람 코드

다른 사람의 코드도 비슷하다.

반응형