-
Notifications
You must be signed in to change notification settings - Fork 1
32-kokeunho #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
32-kokeunho #129
Conversation
kangrae-jo
left a comment
There was a problem hiding this 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());
}
g0rnn
left a comment
There was a problem hiding this 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;
}
}
}
wnsmir
left a comment
There was a problem hiding this 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)
๐ ๋ฌธ์ ๋งํฌ
์์ ์์ถ 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์์ ๊ฐ์ฅ ํฐ ๊ฐ์ ๋ฐํํฉ๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ