Skip to content

Conversation

@kokeunho
Copy link
Collaborator

@kokeunho kokeunho commented Aug 9, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์„์œ  ์‹œ์ถ” https://school.programmers.co.kr/learn/courses/30/lessons/250136

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

50min

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

pccp ๊ธฐ์ถœ ํ’€์–ด๋ณด๊ณ  ์žˆ๋Š”๋ฐ
์–ด์ฉŒ๋‹ค๋ณด๋‹ˆ 3์—ฐ์† ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ๋ฌธ์ œ๋ฅผ ์˜ฌ๋ฆฌ๊ฒŒ ๋๋„ค์š”.
๋‹ค์Œ๋ฒˆ์—๋Š” ๋‹ค๋ฅธ ์œ ํ˜•์— ์ข€ ๋” ๋‚œ์ด๋„ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ ์˜ฌ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค...

์‹œ์ถ”๊ด€์„ ๊ฝ‚์„ ์—ด์„ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ ๋ชจ๋“  ์—ด์€ ํƒ์ƒ‰ํ•ด๋ณด์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ฒ˜์Œ์—๋Š” ๊ฐ ์—ด์„ ํƒ์ƒ‰ํ•  ๋•Œ๋งˆ๋‹ค visited๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์„์œ ๊ฐ€ ์žˆ๋Š” ์นธ์„ ๋งŒ๋‚˜๋ฉด
bfs๋กœ ์„์œ  ์˜์—ญ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜์—ฌ ์—ด์—์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์„์œ ์— ๋”ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋™์ผํ•œ ์—ด์—์„œ๋Š” ๋™์ผํ•œ ์˜์—ญ์— ๋Œ€ํ•ด bfs๋ฅผ ์ค‘๋ณต ์‹คํ–‰ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ O(500^4) ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
์ผ๋ฐ˜ ํ…Œ์ŠคํŠธ๋Š” ๋‹ค ํ†ต๊ณผํ•˜๋Š”๋ฐ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ๋Š” ๋‹ค ๋–จ์–ด์กŒ์Šต๋‹ˆ๋‹ค.
์‚ฌ์‹ค O(500^3)์œผ๋กœ ์ฐฉ๊ฐํ•ด์„œ ๊ฐ„๋‹น๊ฐ„๋‹นํ•˜๊ธด ํ•ด๋„ ํ†ต๊ณผ๋˜์ง€ ์•Š๋‚˜ ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค;

ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ ์ณค์Šต๋‹ˆ๋‹ค.
visited[][]๋ฅผ ๊ฐ ์—ด๋งˆ๋‹ค ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์„ ๊ฒƒ.
๊ฐ ์—ด๋งˆ๋‹ค ๊ฑธ์น˜๋Š” ์„์œ  ์˜์—ญ์˜ ํฌ๊ธฐ๋ฅผ ์ €์žฅํ•œ ๋ฐฐ์—ด sumAreaPerCol[]์„ ๋งŒ๋“ค๊ณ 
bfs๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, ์„์œ  ์˜์—ญ์˜ ํฌ๊ธฐ๋ฅผ ์ฐพ๊ณ  ๊ฑธ์น˜๋Š” ์—ด i์— ๋Œ€ํ•ด ์„์œ  ์˜์—ญ์„ ๋”ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํƒ์ƒ‰์ด ๋๋‚˜๋ฉด
sumAreaPerCol์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ์ด ๋ฌธ์ œ๋ฅผ 3๋ฒˆ์งธ ํ’€์–ด๋ณด๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.
๋ฐฐ์šธ๊ฒŒ ๋งŽ์€ ๋ฌธ์ œ์˜€๋˜๊ฒƒ ๊ฐ™์•„์š”.

์ €๋„ ์ฒ˜์Œ์—๋Š” ๋งค๋ฒˆ bfs๋ฅผ ํ•˜๋ฉฐ visited์ดˆ๊ธฐํ™” ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.
๊ทธ๋žฌ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์„œ ํšจ์œจ์„ฑํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋”๊ตฐ์š”.

๊ทธ๋ž˜์„œ ์ €๋„ ๊ทผํ˜ธ๋‹˜๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์„œ ํ’€์—ˆ๋˜๊ฒƒ ๊ฐ™๋„ค์š”.

ํ•œ๊ฐ€์ง€ ๋‹ค๋ฅธ ์ ์ด๋ผ๋ฉด, ์ €๋Š” set์„ ์‚ฌ์šฉํ•ด์„œ ์„์œ  ์‹œ์ถ”์˜ ์—ด์„ํŒŒ์•…ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
min ~ max๋ฅผ ์„ค์ •ํ•ด์„œ ๊ทธ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ํ’€์—ˆ์–ด์š”.

set์˜ ๋‹จ์ ์ด๋ผ๋ฉด ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์‚ฌ์šฉ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋Š˜์–ด๋‚œ๋‹ค๋Š” ๊ฒƒ์ด๊ณ ,
max์˜ ๋‹จ์ ์€ ๋งค๋ฒˆ max๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋น„์šฉ์ด ๋“ ๋‹ค๋Š” ๊ฒƒ์ด๊ฒ ์ฃ .
(hash set์€ add() ์†๋„๊ฐ€ O(1)์ด๋‹ˆ ๋ฌด์‹œ)

ํฐ ์ฐจ์ด๋Š” ์—†์–ด๋ณด์—ฌ์„œ ์ž…๋ง›์— ๋งž๊ฒŒ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๊ฒ ๋„ค์š”.
์ข‹์€ ๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const int EMPTY = 0;
const int OIL = 1;
const int OFFSET[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

int N, M;
vector<int> result;

bool isIn(int y, int x) { return 0 <= y && y < N && 0 <= x && x < M; }

void bfs(vector<vector<int>>& land, vector<vector<bool>>& visited, int a, int b){
    queue<pair<int, int>> q;
    q.push({a, b});
    visited[b][a] = true;
    
    int minX = a;
    int maxX = a;
    
    int cnt = 1;
    while (!q.empty()){
        auto [x, y] = q.front();
        q.pop();
        
        for (int dir = 0; dir < 4; dir++){
            int x_ = x + OFFSET[dir][1];
            int y_ = y + OFFSET[dir][0];
            if (isIn(y_, x_) && !visited[y_][x_] && land[y_][x_] == OIL){
                q.push({x_,y_});
                visited[y_][x_] = true;
                cnt++;
                maxX = max(maxX, x_);
            }
        }
    }
    
    for (int i = minX; i <= maxX; i++) result[i] += cnt;
}

int solution(vector<vector<int>> land) {
    N = land.size();
    M = land[0].size();
    
    vector<vector<bool>> visited(N, vector<bool> (M, false));
    result = vector<int> (M, 0);
    for (int x = 0; x < M; x++){
        for (int y = 0; y < N; y++){
            if (!visited[y][x] && land[y][x] != EMPTY) 
                bfs(land, visited, x, y);
        }
    }
    
    return *max_element(result.begin(), result.end());
}

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ˆ์ „์— ํ’€์–ด๋ณธ์  ์žˆ๋Š” PCCP ๊ธฐ์ถœ์ด๋„ค์š”. ๋ฌธ์ œ๋ฅผ ๋ช‡๋ฒˆ ๋ณธ์  ์žˆ์–ด์„œ ์†”๋ฃจ์…˜์„ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ๊ณผ ๊ตฌํ˜„์€ ์‰ฌ์› ์Šต๋‹ˆ๋‹ค.

๊ทผ๋ณธ์ ์œผ๋กœ ์„์œ ์˜ ์œ„์น˜๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„  ์ด์ค‘ for๋ฌธ์œผ๋กœ ๋ชจ๋“  ์นธ์„ ํƒ์ƒ‰ํ•ด์•ผ๋งŒ ํ–ˆ๊ณ ,

์ตœ๋Œ€ํ•œ ํšจ์œจ์ ์œผ๋กœ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ์„์œ ๊ฐ€ ์—†๋Š” ๊ณณ์ด๋‚˜ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์ ์žˆ๋Š” ์„์œ (์นธ)์€ ์ œ์™ธํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ €๋„ ๊ทผํ˜ธ๋‹˜ ์ฒ˜๋Ÿผ Set๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํƒ์ƒ‰ํ•œ ์—ด์„ ์ €์žฅํ•˜์˜€๊ณ , ์„์œ ์˜ ํฌ๊ธฐ๋ฅผ ๋”ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

+) ์—ด์˜ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ๋ฐฉ๋ฌธํ•œ ์—ด์„ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์€ ์ƒ๊ฐ์น˜ ๋ชปํ–ˆ๋Š”๋ฐ ์ข‹์•„๋ณด์ด๋„ค์š”:)

import java.util.*;

class Solution {
    
    static int[][] offset = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; // ๋™ ๋‚จ ์„œ ๋ถ
    static int[] cols;
    static boolean[][] visited;
    
    public int solution(int[][] land) {
        int n = land.length; // row
        int m = land[0].length; // col
        cols = new int[m];
        visited = new boolean[n][m];
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                // ์„์œ ๋ฅผ ๋ฝ‘๊ณ  cols์— ์ €์žฅํ•จ.
                extract(j, i, land); // x, y
            }
        }
        
        int answer = 0;
        for (int i = 0; i < m; i++) {
            if (answer < cols[i]) answer = cols[i];
        }
        return answer;
    }
    
    private static void extract(int x, int y, int[][] land) {
        if (visited[y][x]) return;
        if (land[y][x] == 0) return;
        
        Set<Integer> visitedCols = new HashSet<>();
        Queue<int[]> q = new ArrayDeque<>();
        int n = land.length;
        int m = land[0].length;
        int count = 1;
        
        visited[y][x] = true;
        q.add(new int[]{x, y});
        visitedCols.add(x);
        
        while (!q.isEmpty()) {
            int cx = q.peek()[0];
            int cy = q.poll()[1];
            
            for (int[] o : offset) {
                int nx = cx + o[0];
                int ny = cy + o[1];
                
                if (nx < 0 || nx >= m || ny < 0 || ny >= n || visited[ny][nx]) continue;
                if (land[ny][nx] == 0) continue;
                
                q.offer(new int[]{nx, ny});
                visited[ny][nx] = true;
                visitedCols.add(nx);
                count += 1;
            }
        }
        for (Integer i : visitedCols) {
            cols[i] += count;
        }
    }
}

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ฌธ์ œ ์˜ค๋žœ๋งŒ์— ๋ณด๋‹ˆ ๋ฐ˜๊ฐ‘๊ตฐ์š”!
์ฒ˜์Œ์— ํ’€๋–„ ๋„ˆ๋ฌด์‰ฌ์› ๋Š”๋ฐ ์‹œ๊ฐ„์ดˆ๊ณผ๋‚˜์„œ BFS๋ฅผ ์–ผ๋งˆ๋‚˜ ์ตœ์†Œ๋กœ ์“ธ ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ–ˆ๋˜ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

๊ทธ๊ธฐ์–ต๋งŒ ๊ฐ€์ง€๊ณ  ๋‹ค์‹œ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋ชจ๋“ ๋ธ”๋ก์„ ๊ธฐ์ค€์œผ๋กœ BFS๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผํ•จ์€ ๋ณ€ํ•จ์ด ์—†๊ฒ ๋”๋ผ๊ตฌ์š”
๋”ฐ๋ผ์„œ 0์ด ์•„๋‹Œ ๋ธ”๋ก์—์„œ BFS๋ฅผ ์‹คํ–‰ํ•ด์ฃผ์—ˆ๊ณ , ๊ฐ ๋ฐ˜๋ณต๋งˆ๋‹ค 1๋ถ€ํ„ฐ์‹œ์ž‘ํ•˜๋Š” ๋ผ๋ฒจ์„ ๋ถ™์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
๋ถ™์ธ๊ฒŒ์•„๋‹ˆ๋ผ land์— ๊ทธ๋Œ€๋กœ ๋ผ๋ฒจ์„ ๋ฐ•์•„์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด์œ ๋Š” ๋‚˜์ค‘์— ์—ด๋‹จ์œ„๋กœ ๊ณ„์‚ฐํ• ๋–„ ๋ฏธ๋ฆฌ ์ธ๋ฑ์Šค๋ณ„๋กœ ์ •ํ•ด๋‘” ์ „์ฒด ํฌ๊ธฐ๊ฐ’์„ ๋”ํ•ด์ฃผ๊ธฐ๋งŒ ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.

์ฆ‰ BFS๋Š” ๋ฉ์–ด๋ฆฌ ๊ฐœ์ˆ˜๋งŒํผ๋งŒ ์‹คํ–‰๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋‹ต์„ ๊ตฌํ• ๋•Œ๋Š” ๊ฐ ์—ด์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ์ธ๋ฑ์Šค๋งŒ ์ถ”์ถœํ•˜์—ฌ ๊ทธ ์ธ๋ฑ์Šค ๋ฉ์–ด๋ฆฌ์˜ ๊ฐ’๋งŒ ๋ˆ„์ ํ•ฉํ•ด์ฃผ๊ณ  ๊ฐ€์žฅํฐ ๊ฐ’์„ ๊ฐ€์ง„ ์ธ๋ฑ์Šค๋ฅผ returnํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

from collections import deque

def solution(land):
    dx = [1, -1, 0, 0]
    dy = [0, 0, 1, -1]
    
    queue = deque()
    n = len(land)
    m = len(land[0])
    
    visited = [[False]*m for _ in range(n)]
    
    col_oil = [0]*m
    
    for x in range(n):
        for y in range(m):
            if land[x][y] == 1 and not visited[x][y]:
                start = (x, y)
                queue.append(start)
                count = 1
                visited[x][y] = True
                col = set()
                col.add(y)
                
                while queue:
                    curr_x, curr_y = queue.popleft()
                    for i in range(4):
                        nx = curr_x + dx[i]
                        ny = curr_y + dy[i]
                        if 0 <= nx < n and 0 <= ny < m and land[nx][ny] == 1 and not visited[nx][ny]:
                            queue.append((nx, ny))
                            visited[nx][ny] = True
                            count += 1
                            col.add(ny)
                for c in col:
                    col_oil[c] += count
                    
    return max(col_oil)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants