원본 문제 : https://programmers.co.kr/learn/courses/30/lessons/1829
문제 참고 : https://lkhlkh23.tistory.com/48
class Solution {
public static boolean[][] visited;
public static int[][] image;
public static int[] solution(int m, int n, int[][] picture) {
int[] answer = new int[2];
visited = new boolean[m][n];
image = picture.clone();
for(int i = 0; i < m ; i++) {
for(int j = 0; j < n ; j++) {
if(image[i][j]>0) {
int count = getEqualSpaceCount(i,j,image[i][j]);
answer[0] = count > 0 ? answer[0]+1 : answer[0];
answer[1] = Math.max(count, answer[1]);
}
}
}
return answer;
}
public static int getEqualSpaceCount(int i, int j, int value) {
if( i<0
|| i >= image.length
|| j < 0
|| j >= image[0].length
|| visited[i][j]==true
|| value != image[i][j])
return 0;
else {
visited[i][j] = true;
return 1 +
getEqualSpaceCount(i+1,j,value) +
getEqualSpaceCount(i-1,j,value) +
getEqualSpaceCount(i,j+1,value) +
getEqualSpaceCount(i,j-1,value) ;
}
}
}
'프로그래머스 > 카카오 코딩테스트' 카테고리의 다른 글
[프로그래머스] (틀림)후보키 (42890)(java) (0) | 2019.05.30 |
---|---|
[프로그래머스] 카카오 오픈채팅방 (42888)(java) (0) | 2019.05.30 |
[프로그래머스] 카카오 프렌즈 컬리링북 (1829)(java) (0) | 2019.05.29 |
[프로그래머스] 카카오 프렌즈 컬러링북(1829)(java) (0) | 2019.04.26 |
댓글