https://school.programmers.co.kr/learn/courses/30/lessons/250136
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 전수 조사
석유덩어리를 구분하기 위해 같은 덩어리일 경우 같은 숫자로 변경
2. 시추관을 하나씩 내려서 조사하기
import java.util.*;
class Solution {
public int solution(int[][] land) {
int answer = 0;
int index = 2;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 0);
int n = land.length;
int m = land[0].length;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if (land[i][j] == 1) {
int size = 0;
Queue<int[]> queue = new LinkedList<>();
land[i][j] = index;
size += 1;
queue.add(new int[]{i, j});
while(!queue.isEmpty()) {
int[] a = queue.poll();
int x = a[0];
int y = a[1];
if (x - 1 > -1 && land[x - 1][y] == 1) {
land[x - 1][y] = index;
size += 1;
queue.add(new int[]{x - 1, y});
}
if (x + 1 < n && land[x + 1][y] == 1) {
land[x + 1][y] = index;
size += 1;
queue.add(new int[]{x + 1, y});
}
if (y - 1 > -1 && land[x][y - 1] == 1) {
land[x][y - 1] = index;
size += 1;
queue.add(new int[]{x, y - 1});
}
if (y + 1 < m && land[x][y + 1] == 1) {
land[x][y + 1] = index;
size += 1;
queue.add(new int[]{x, y + 1});
}
}
map.put(index, size);
index += 1;
}
}
}
int max = 0;
for(int i = 0; i < m; i++) {
Set<Integer> set = new HashSet<>();
for(int j = 0; j < n; j++) {
set.add(land[j][i]);
}
int temp = 0;
for(int a: set) {
temp += map.get(a);
if (temp > max) {
max = temp;
}
}
}
answer = max;
return answer;
}
}
private int[] dy = new int[]{1,-1,0,0};
private int[] dx = new int[]{0,0,1,-1};
4방향으로 나아가는 부분은
for(int d = 0; d < 4; d++)
현재 x + dx[d]
현재 y + dy[d]
요렇게 변경해두 될듯..?!
'코딩테스트' 카테고리의 다른 글
[programmers] 게임 맵 최단거리 (0) | 2024.03.31 |
---|---|
[programmers][PCCE] 9. 이웃한 칸 (0) | 2024.03.24 |
[programmers][PCCE 기출문제] 10번 / 데이터 분석 (0) | 2024.03.13 |
[programmers] 이모티콘 할인행사 (0) | 2024.03.13 |
33. Search in Rotated Sorted Array (0) | 2024.02.22 |