forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJY_150367.java
More file actions
44 lines (37 loc) · 1.31 KB
/
JY_150367.java
File metadata and controls
44 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
class Solution {
public int[] solution(long[] numbers) {
int[] answer = new int[numbers.length];
for(int i=0; i<numbers.length; i++) {
long num = numbers[i];
// 0 추가하기
String ts = makeTree(Long.toBinaryString(num));
// System.out.println(ts);
// 포화이진트리가 가능한지 체크
// 부모가 없는데 -> 자식은 있음
if(checkS(ts)) answer[i] = 1;
}
return answer;
}
public static String makeTree(String s) {
int size = s.length();
int n = 1;
while(size > (1 << n)-1) {
n++;
}
int cnt = (1 << n) - size - 1;
return "0".repeat(cnt) + s;
}
public static boolean checkS(String now) {
// 리프노드
if(now.length() == 1) return true;
int rIdx = now.length() / 2;
// 부모가 없는데 자식이 존재
String left = now.substring(0, rIdx);
String right = now.substring(rIdx+1);
if(now.charAt(rIdx) == '0'
&& (left.contains("1") || right.contains("1"))) return false;
// 가능하다면 다시 하위 트리 탐색
return checkS(left) && checkS(right);
}
}