2022 KAKAO BLIND RECRUITMENT > 양과늑대
양과 늑대
https://programmers.co.kr/learn/courses/30/lessons/92343
코딩테스트 연습 - 양과 늑대
[0,0,1,1,1,0,1,0,1,0,1,1] [[0,1],[1,2],[1,4],[0,8],[8,7],[9,10],[9,11],[4,3],[6,5],[4,6],[8,9]] 5 [0,1,0,1,1,0,1,0,0,1,0] [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6],[3,7],[4,8],[6,9],[9,10]] 5
programmers.co.kr
https://tech.kakao.com/2022/01/14/2022-kakao-recruitment-round-1/
2022 카카오 신입공채 1차 온라인 코딩테스트 for Tech developers 문제해설
지난 2021년 9월 11일 토요일 오후 2시부터 7시까지 5시간 동안 2022 KAKAO BLIND RECRUITMENT 1차 코딩 테스트가 진행되었습니다. 테스트에는 총 7개의 문제가 출제되었으며, 개발 언어는 C++, Java, JavaScript, K
tech.kakao.com
문제에 대한 해설은 카카오 신입공채 테크블로그의 해설이 워낙 잘 설명되어 있으므로, 이 글로 대체하겠습니다.
static int[] Info;
static int[][] Edges;
static int max = 0;
static List<Tree> trees = new ArrayList<>();
public static int solution(int[] info, int[][] edges) {
Info = info;
Edges = edges;
for (int i = 0; i < info.length; i++) {
int left = -1;
int right = -1;
for (int[] edge : edges) {
if (edge[0] == i) {
if (left == -1) {
left = edge[1];
} else {
right = edge[1];
break;
}
}
}
trees.add(new Tree(info[i] == 0, left, right));
}
checkMaxSheep(0, 0, 0, new ArrayList<>(), new boolean[info.length]);
return max;
}
private static void checkMaxSheep(int now, int sheep, int wolf, ArrayList<Integer> canMove,
boolean[] visit) {
if (Info[now] == 0) {
sheep++;
} else {
wolf++;
}
ArrayList<Integer> tmp = new ArrayList<>(canMove);
boolean[] tmpVisit = Arrays.copyOf(visit, visit.length);
if (sheep > wolf) {
max = Math.max(max, sheep);
tmpVisit[now] = true;
int left = trees.get(now).left;
if (left != -1) {
tmp.add(left);
}
int right = trees.get(now).right;
if (right != -1) {
tmp.add(right);
}
for (int go : tmp) {
if (tmpVisit[go]) {
continue;
}
checkMaxSheep(go, sheep, wolf, tmp, tmpVisit);
}
}
}
static class Tree {
boolean isSheep;
int left;
int right;
public Tree(boolean isSheep, int left, int right) {
this.isSheep = isSheep;
this.left = left;
this.right = right;
}
}