๊ธฐ๋ก๋ฐฉ

Lv.2 : [PCCP ๊ธฐ์ถœ๋ฌธ์ œ] 2๋ฒˆ ๋ณธ๋ฌธ

CodingTest/Java

Lv.2 : [PCCP ๊ธฐ์ถœ๋ฌธ์ œ] 2๋ฒˆ

Soom_1n 2023. 11. 24. 15:39

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • n x m ํฌ๊ธฐ์˜ ๋•…์— ์„์œ  ๋ฉ์–ด๋ฆฌ๋“ค์ด ์žˆ๋‹ค. ์–ด๋Š ์ง€์ ์—์„œ ์‹œ์ถ”ํ•ด์•ผ ๊ฐ€์žฅ ๋งŽ์€ ์„์œ ๋ฅผ ์‹œ์ถ”ํ•˜๋Š”์ง€ ๊ตฌํ•œ๋‹ค.
  • ์‹œ์ถ”๋Š” ๊ฐ€์žฅ ์œ„์—์„œ ์ˆ˜์ง์œผ๋กœ ํ•˜๋Š”๋ฐ, ์‹œ์ถ”๊ธฐ๊ฐ€ ์ง€๋‚˜๊ฐ€๋Š” ์„์œ  ๋ฉ์ด๋ฆฌ๋ฅผ ๋ชจ๋‘ ์‹œ์ถ”ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ์„์œ  ๋ฉ์–ด๋ฆฌ๋ฅผ ์ฒ˜์Œ ๋ฐœ๊ฒฌํ–ˆ์„๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•œ๋‹ค.
    • ์„์œ  ๋ฉ์–ด๋ฆฌ ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ธ๋‹ค.
    • ํ•ด๋‹น ๋ฉ์–ด๋ฆฌ์˜ ํฌ๊ธฐ๋ฅผ ํƒ์ƒ‰ํ•ด ํŒŒ์•…ํ•˜๊ณ , ๋ฒˆํ˜ธ์— ๋งž๊ฒŒ ํฌ๊ธฐ๋ฅผ ์ €์žฅํ•ด๋‘”๋‹ค.
  • ๋งˆ์ง€๋ง‰์— ๋ชจ๋“  ์ง€์ ์—์„œ ์‹œ์ถ”ํ•ด๋ณด๋ฉฐ, ์‹œ์ถ” ๋˜๋Š” ์„์œ ๋Ÿ‰์˜ ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Queue;
import java.util.ArrayDeque;

class Solution {
    private static class Point {
        int x, y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    public int solution(int[][] land) {
        int n = land.length;
        int m = land[0].length;
        int[][] map = new int[n][m]; // ์„์œ  ๋ฉ์–ด๋ฆฌ ๋ณ„ ์ˆซ์ž ๋ถ€์—ฌ
        int[] oils = new int[n * m + 1]; // ์„์œ  ๋ฒˆํ˜ธ ๋ณ„ ์„์œ ๋Ÿ‰
        int oileCnt = 0; // ์ฒดํฌํ•œ ๋ฉ์–ด๋ฆฌ ์ˆ˜

        int[] dx = {-1, 0, 1, 0};
        int[] dy = {0, 1, 0, -1};

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (land[i][j] == 1 && map[i][j] == 0) { // ์ฒ˜์Œ ์„์œ  ๋ฉ์–ด๋ฆฌ ๋ฐœ๊ฒฌ

                    oileCnt++;
                    int cnt = 1;

                    // BFS
                    Queue<Point> que = new ArrayDeque<>();
                    que.add(new Point(i, j));
                    map[i][j] = oileCnt;

                    while (!que.isEmpty()) {
                        Point point = que.poll();
                        for (int k = 0; k < dx.length; k++) {
                            int row = point.x + dx[k];
                            int col = point.y + dy[k];

                            if (0 <= row && row < n && 0 <= col && col < m && land[row][col] == 1
                                && map[row][col] == 0) {
                                map[row][col] = oileCnt;
                                cnt++;
                                que.add(new Point(row, col));
                            }
                        }
                    }
                    // ํ˜„์žฌ ๊ธฐ๋ฆ„ ๋ฉ์–ด๋ฆฌ ๋ฒˆํ˜ธ์— ํฌ๊ธฐ ์ €์žฅ
                    oils[oileCnt] = cnt;
                }
            }
        }

        int answer = 0;

        for (int i = 0; i < m; i++) { // ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ์‹œ์ถ” ์ง€์  ์ฐพ๊ธฐ
            boolean[] check = new boolean[oileCnt + 1];
            int sum = 0;

            for (int j = 0; j < n; j++) {
                if (!check[map[j][i]]) {
                    check[map[j][i]] = true;
                    sum += oils[map[j][i]];
                }
            }
            answer = Math.max(answer, sum);
        }

        return answer;
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์„์œ  ๋ฉ์–ด๋ฆฌ ๋ฒˆํ˜ธ๋ฅผ ํ•ด๋‹น ๋ฉ์–ด๋ฆฌ์˜ ๋ชจ๋“  ์ง€์ ์— ๊ธฐ๋กํ•ด๋‘”๋‹ค.
    • ์ฒ˜์Œ ์„์œ  ๋ฉ์–ด๋ฆฌ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ์„ ๋•Œ, BFS๋ฅผ ํ†ตํ•ด ํฌ๊ธฐ๋ฅผ ํŒŒ์•…ํ•œ๋‹ค.
  • ์‹œ์ถ”๋Ÿ‰์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ์„์œ ๋Ÿ‰์„ ๊ตฌํ•œ๋‹ค.
    • boolean ๋ฐฐ์—ด check๋ฅผ ๋ฉ์–ด๋ฆฌ ๊ฐœ์ˆ˜ ๋งŒํผ ๋งŒ๋“ค์–ด, ํ•œ ๋ฒˆ์˜ ์‹œ์ถ”์—์„œ ์ด๋ฏธ ํŒŒ์•…ํ•œ ๋ฉ์–ด๋ฆฌ๋ผ๋ฉด, ํ•ฉ์‚ฐํ•˜์ง€ ์•Š๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. 

๐Ÿ”ธ end ๐Ÿ”ธ

  • PCCP์—์„œ ํ’€์—ˆ๋˜ ๋ฌธ์ œ์ธ๋ฐ, ์ด๋ฒˆ์— ๊ธฐ์ถœ๋กœ ๊ณต๊ฐœ๋˜์–ด์„œ ๋‹ค์‹œ ํ•œ ๋ฒˆ ํ’€๊ฒŒ ๋˜์—ˆ๋‹ค.
    • ์‹œํ—˜์—์„œ ํ’€ ๋•Œ๋„ ๋งž์•˜์—ˆ๋Š”๋ฐ, ๊ทธ๋ƒฅ ๊ธฐ์ดˆ์ ์ธ BFS๋ฌธ์ œ์—ฌ์„œ ๊ฐ„๋‹จํžˆ ํ’€์ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ๋‹ค๋ฅธ ํ’€์ด๋“ค๋„ ๋น„์Šทํ•˜๊ฒŒ ํ’€์ดํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

728x90