Created
July 27, 2023 03:33
-
-
Save ByeongsuPark/4ca808d9c9bf1e094dc83e849ec0c0be to your computer and use it in GitHub Desktop.
programmers_inquiry_kakao_friends_coloring_book_submission_code_rejected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Solution { | |
private final int NON_COLORING_AREA = 0; | |
private final int VISITED = -1; | |
public int[] solution(int m, int n, int[][] picture) { | |
int numberOfArea = 0; | |
int maxSizeOfOneArea = 0; | |
for (int row = 0; row < picture.length; row++) { | |
for (int col = 0; col < picture[row].length; col++) { | |
if (picture[row][col] == NON_COLORING_AREA) continue; | |
if (picture[row][col] == VISITED) continue; | |
numberOfArea++; | |
int sizeOfArea = visitArea(picture, row, col); | |
if (maxSizeOfOneArea < sizeOfArea) maxSizeOfOneArea = sizeOfArea; | |
} | |
} | |
int[] answer = new int[2]; | |
answer[0] = numberOfArea; | |
answer[1] = maxSizeOfOneArea; | |
return answer; | |
} | |
private int visitArea(int[][] map, int row, int col) { | |
int areaCount = 0; | |
int areaColor = map[row][col]; | |
int[] dRow = {0, 0, 1, -1}; | |
int[] dCol = {1, -1, 0, 0}; | |
Queue<Pos> queue = new LinkedList<>(); | |
Pos startPos = new Pos(row, col); | |
queue.add(startPos); | |
while (!queue.isEmpty()) { | |
Pos pos = queue.poll(); | |
if (!isValidPos(map, pos)) continue; | |
if (map[pos.row][pos.col] == NON_COLORING_AREA) continue; | |
if (map[pos.row][pos.col] == VISITED) continue; | |
if (map[pos.row][pos.col] != areaColor) continue; | |
areaCount++; | |
map[pos.row][pos.col] = VISITED; | |
for (int dir = 0; dir < dRow.length; dir++) { | |
int movedRow = pos.row + dRow[dir]; | |
int movedCol = pos.col + dCol[dir]; | |
Pos movedPos = new Pos(movedRow, movedCol); | |
queue.add(movedPos); | |
} | |
} | |
return areaCount; | |
} | |
private boolean isValidPos(int[][] map, Pos pos) { | |
return 0 <= pos.row && pos.row < map.length && | |
0 <= pos.col && pos.col < map[pos.row].length; | |
} | |
private class Pos { | |
int row; | |
int col; | |
Pos (int row, int col) { | |
this.row = row; | |
this.col = col; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment