[프로그래머스 LV1] 1차 비밀지도

popolarburr·2023년 3월 21일
0
post-thumbnail

- 문제


문제가 길어 링크로 대체하겠습니다.


- 풀이

import java.util.Arrays;
import java.util.stream.Collectors;

class Solution {
    public String[] solution(int n, int[] arr1, int[] arr2) {

        String[] answer = new String[n];
        String[][] arr = new String[n][n];

        for (int i = 0; i < n; i++) {
            String width_tmp = Integer.toString(arr1[i], 2);
            width_tmp = String.format("%" + n + "s", width_tmp).replaceAll(" ", "0");

            String height_tmp = Integer.toString(arr2[i], 2);
            height_tmp = String.format("%" + n + "s", height_tmp).replaceAll(" ", "0");

            System.out.println(width_tmp + " " + height_tmp);

            for (int j = 0; j < n; j++) {
                if (width_tmp.charAt(j) == '1' || height_tmp.charAt(j) == '1') {
                    arr[i][j] = "#";
                } else {
                    arr[i][j] = " ";
                }
            }
        }


        int count = 0;
        for (String[] outerArray : arr) {
            answer[count++] = Arrays.stream(outerArray).collect(Collectors.joining());
        }

        return answer;
    }
}

- 정리

처음에 어떻게 풀지는 바로 감이 와서 시작했다. 하지만 그 감이 틀렸다는 것은 바로 알아차렸다. 1차원 배열인 정답배열과 각 지도를 합칠 2차원 배열이 필요하다는 것을 늦게 캐치하였고, 이를 생각하다보니 내가 생각해놨던 방향을 다시 잡아야했다.
게다가 풀이에 급급하여 코드가 좀 비효율적으로 흘러가게 됐는데, 리팩토링하는 과정을 통해 효율성을 챙기는 연습을 해야겠다.


[링크] : [개인저장소](https://github.com/DongWooKim97/Java_CodingTest/tree/898724b611403399a8e51cafa8d258a733d9b450/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/lv1/17681.%E2%80%85%EF%BC%BB1%EC%B0%A8%EF%BC%BD%E2%80%85%EB%B9%84%EB%B0%80%EC%A7%80%EB%8F%84)
profile
차곡차곡

0개의 댓글