-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_150367.java
More file actions
111 lines (84 loc) ยท 2.85 KB
/
DH_150367.java
File metadata and controls
111 lines (84 loc) ยท 2.85 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.*;
/*
* ํํ ๊ฐ๋ฅํ ์ด์งํธ๋ฆฌ
*/
public class DH_150367 {
static char[] tree;
static int idx;
static int[] solution(long[] numbers) {
int[] answer = new int[numbers.length];
for(int i = 0; i < numbers.length; i++) {
if(numbers[i] == 0) {
answer[i] = 0;
continue;
}
// ์
๋ ฅ๋ ์๋ฅผ ์ด์ง์ ๋ฌธ์์ด๋ก ๋ฐ๊ฟ
String binaryString = numToBinaryStr(numbers[i]);
int totalLength = binaryString.length();
tree = new char[totalLength];
setTree(binaryString, totalLength);
answer[i] = check(tree) ? 1: 0;
}
return answer;
}
static String numToBinaryStr(long number) {
String binaryString = Long.toBinaryString(number);
// ํฌํ ์ด์ง ํธ๋ฆฌ๋ฅผ ๊ตฌ์ฑํ๊ธฐ ์ํด ํ์ํ ๋
ธ๋์ ์ด ๊ฐ์
int totalLength = getTotalLength(binaryString.length());
// ์ถ๊ฐ๋ก ๋ํด์ผ๋๋ 0์ ๊ฐ์
int addZeroLength = totalLength - binaryString.length();
String addZerostr = "0".repeat(addZeroLength);
// ์ด์งํธ๋ฆฌ๋ก ํํํ ์ ์๋๋ก ๋ฌธ์์ด ๊ฐ๊ณตํด์ฃผ๊ธฐ
binaryString = addZerostr + binaryString;
return binaryString;
}
// ํํ ๊ฐ๋ฅํ ์ด์งํธ๋ฆฌ์ธ์ง ํ์ธ
static boolean check(char[] tree) {
boolean available = true;
// ๋ถ๋ชจ๋
ธ๋๊ฐ 0์ธ๋ฐ, ์์ ๋
ธ๋๊ฐ ํ๋๋ผ๋ 1์ด๋ผ๋ฉด ํํ ๋ถ๊ฐ๋ฅํ ์ด์งํธ๋ฆฌ
// ํ์ธํด์ผ ๋๋ ๋ถ๋ชจ ๋
ธ๋ idx: 0 ~ (๋ง์ง๋ง depth - 1) ๋
ธ๋๊น์ง
// ์๋ํ๋ฉด ๋ง์ง๋ง depth ๋
ธ๋๋ค์ ์์์ด ์๊ธฐ ๋๋ฌธ
int depth = (int) (Math.log(tree.length) / Math.log(2)) + 1;
int startIdx = (1 << (depth - 1)) - 1;
for(int i = 0; i < startIdx; i++) {
int currentIdx = i + 1;
int next1 = currentIdx * 2;
int next2 = currentIdx * 2 + 1;
next1 -= 1; next2 -= 1;
if(tree[i] == '0' && (tree[next1] == '1' || tree[next2] == '1')) {
available = false;
break;
}
}
return available;
}
// inorder๋ก ์ฃผ์ด์ง ์ ๋ณด๋ก tree ๊ตฌ์ฑํ๊ธฐ
static void setTree(String s, int totalLength) {
idx = 0;
Queue<int[]> q = new ArrayDeque<>();
q.add(new int[] {0, totalLength + 1});
while(!q.isEmpty()) {
int[] current = q.poll();
int l = current[0], r = current[1];
if(l == r || r - l == 1) continue;
int middle = (l + r) >> 1;
tree[idx] = s.charAt(middle - 1);
idx += 1;
q.add(new int[] {l, middle});
q.add(new int[] {middle, r});
}
}
// ํ์ฌ ํธ๋ฆฌ์ ํฌ๊ธฐ๊ฐ length์ธ ๊ฒฝ์ฐ, ๋ง๋ค ์ ์๋ ํฌํ์ด์งํธ๋ฆฌ์ ํฌ๊ธฐ
static int getTotalLength(int length) {
int n = 1, digit = 0;
while(true) {
digit = (1 << n++) - 1;
if(digit >= length) break;
}
return digit;
}
public static void main(String[] args) throws Exception {
long[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 128, 129, 16512, 2147516555L};
System.out.println(Arrays.toString(solution(numbers)));
}
}