1. 문제
https://programmers.co.kr/learn/courses/30/lessons/1829?language=java
코딩테스트 연습 - 카카오프렌즈 컬러링북
6 4 [[1, 1, 1, 0], [1, 2, 2, 0], [1, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 3], [0, 0, 0, 3]] [4, 5]
programmers.co.kr
2. 나의 풀이
class Solution {
boolean [][] imsi;
int [][] picture1;
public int[] solution(int m, int n, int[][] picture) {
int numberOfArea = 0;
int maxSizeOfOneArea = 0;
imsi = new boolean [picture.length][picture[0].length];
picture1 = picture;
for(int i = 0; i < picture1.length; i = i + 1) {
for(int j = 0; j < picture1[i].length; j = j + 1) {
if(picture1[i][j] != 0 && imsi[i][j] == false) {
numberOfArea += 1;
int result = maxHamsu(i, j);
if(result > maxSizeOfOneArea) {
maxSizeOfOneArea = result;
}}
}
}
int[] answer = new int[2];
answer[0] = numberOfArea;
answer[1] = maxSizeOfOneArea;
return answer;
}
int maxHamsu(int i, int j) {
int answer = 1;
imsi[i][j] = true;
if(i > 0 && imsi[i-1][j] == false && picture1[i][j] == picture1[i-1][j]) {
answer += maxHamsu(i-1, j);
}
if(j < picture1[i].length - 1 && imsi[i][j+1] == false && picture1[i][j] == picture1[i][j+1]) {
answer += maxHamsu(i, j+1);
}
if(i < picture1.length - 1 && imsi[i+1][j] == false && picture1[i][j] == picture1[i+1][j]) {
answer += maxHamsu(i+1, j);
}
if(j > 0 && imsi[i][j-1] == false && picture1[i][j] == picture1[i][j-1]) {
answer += maxHamsu(i, j-1);
}
return answer;
}
}
3. 다른 사람 풀이
import java.util.ArrayList;
import java.util.Collections;
class Solution {
int span = 0;
public int[] solution(int m, int n, int[][] picture) {
int numberOfArea = 0;
int maxSizeOfOneArea = 0;
boolean[][] pathBool = new boolean[m][n];
ArrayList<Integer> answerList = new ArrayList<>();
int[] answer = new int[2];
for (int i = 0; i < m; i++) {
for (int j = 0; j <n; j++) {
if (picture[i][j] > 0) {
findPath(i, j, picture, pathBool);
if(span > 0){
answerList.add(span);
span = 0;
}
}
}
}
numberOfArea = answerList.size();
if(numberOfArea == 0){
return new int[]{0, 0};
}
Collections.sort(answerList, Collections.reverseOrder());
maxSizeOfOneArea = answerList.get(0);
answer[0] = numberOfArea;
answer[1] = maxSizeOfOneArea;
return answer;
}
public void findPath(int m, int n, int[][] picture, boolean[][] pathBool) {
if (pathBool[m][n] == true)
return; // 이미 거친 경로일 경우
long su = picture[m][n]; // 이미지 값
int column = picture[0].length;
int row = picture.length;
pathBool[m][n] = true;
span++;
// 오른쪽 이동
if ((n + 1 < column) && (pathBool[m][n + 1] == false && su == picture[m][n + 1])) {
findPath(m, n + 1, picture, pathBool);
}
// 아래쪽 이동
if ((m + 1 < row) && (pathBool[m + 1][n] == false && su == picture[m + 1][n])) {
findPath(m + 1, n, picture, pathBool);
}
// 왼쪽 이동
if ((n - 1 >= 0) && (pathBool[m][n - 1] == false && su == picture[m][n - 1])) {
findPath(m, n - 1, picture, pathBool);
}
// 위쪽 이동
if ((m - 1 >= 0) && (pathBool[m - 1][n] == false && su == picture[m - 1][n])) {
findPath(m - 1, n, picture, pathBool);
}
}
'알고리즘 공부 > 프로그래머스' 카테고리의 다른 글
(Java)프로그래머스 코딩테스트 연습 - 힙(Heap) - 더 맵게 (0) | 2020.10.19 |
---|---|
(Java)프로그래머스 코딩테스트 연습 - 월간 코드 챌린지 시즌1 - 삼각 달팽이 (0) | 2020.10.19 |
프로그래머스 코딩테스트 연습 - Summer/Winter Coding(~2018) - 스킬트리 (0) | 2020.10.13 |
프로그래머스 코딩테스트 연습 - 스택/큐 - 주식가격 (0) | 2020.10.13 |
프로그래머스 코딩테스트 연습 - 연습문제 - 124 나라의 숫자 (0) | 2020.10.12 |