Skip to content

Commit d9d33e9

Browse files
committed
nsun9505 solved 후보키
1 parent 1eea179 commit d9d33e9

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# [2019 KAKAO BLIND RECRUITMENT] 후보키 - JAVA
2+
3+
## 분류
4+
> 해시
5+
>
6+
> 부분집합
7+
8+
## 코드
9+
```java
10+
import java.util.ArrayList;
11+
import java.util.HashSet;
12+
13+
public class Solution {
14+
static HashSet<String> keyCountSet = new HashSet<>();
15+
public int solution(String[][] relation) {
16+
int answer = 0;
17+
18+
int N = relation[0].length;
19+
ArrayList<Key> keys = new ArrayList<>();
20+
ArrayList<Integer> tmp = new ArrayList<>();
21+
// 부분 집합 구하기 -> 모든 컬럼 조합 구하기
22+
for(int i=1; i<(1<<N); i++){
23+
tmp.clear();
24+
for(int j=0; j<N; j++){
25+
if((i & (1 << j)) > 0)
26+
tmp.add(j);
27+
}
28+
29+
Integer[] tmpArr = tmp.toArray(new Integer[tmp.size()]);
30+
keys.add(new Key(i, tmpArr));
31+
}
32+
33+
// 키를 만들 수 있는지 보기
34+
for(int i=0; i<keys.size(); i++){
35+
Key key = keys.get(i);
36+
if(!key.isUnique)
37+
continue;
38+
39+
// i번째 컬럼 조합이 키가 될 수 있는지 체크
40+
boolean ret = check(key, relation);
41+
// 키가 될 수 없으면 해당 조합은 key가 될 수 없음을 표시
42+
if(!ret)
43+
key.isUnique = false;
44+
45+
// i번째 컬럼 조합이 키가 될 수 있으면, i번째 컬럼을 포함하는 조합들을 후보키에서 탈락시킨다.
46+
// 왜냐하면 i번째 컬럼이 키가 될 수 있는데 다른 컬럼이 붙어도 식별이 가능하지만, 최소성을 만족시키지 못해 탈락
47+
else{
48+
for(int j=i+1; j<keys.size(); j++){
49+
if((keys.get(j).num & keys.get(i).num) == keys.get(i).num)
50+
keys.get(j).isUnique = false;
51+
}
52+
}
53+
}
54+
55+
// 후보키 카운트
56+
answer = 0;
57+
for(Key key : keys){
58+
if(key.isUnique)
59+
answer++;
60+
}
61+
62+
return answer;
63+
}
64+
65+
public boolean check(Key key, String[][] relation) {
66+
keyCountSet.clear();
67+
for(int i=0; i<relation.length; i++){
68+
String candidateKey = "";
69+
// 컬럼의 값을 연결해서 하나의 키로 만든다.
70+
for(int idx : key.arr)
71+
candidateKey += " " + relation[i][idx];
72+
73+
// 이미 존재하는 컬럼 조합의 값이 있다면 후보키 탈락
74+
if(keyCountSet.contains(candidateKey))
75+
return false;
76+
keyCountSet.add(candidateKey);
77+
}
78+
return true;
79+
}
80+
81+
static class Key{
82+
int num;
83+
Integer[] arr;
84+
boolean isUnique;
85+
86+
public Key(int num, Integer[] arr) {
87+
this.num = num;
88+
this.arr = arr;
89+
this.isUnique = true;
90+
}
91+
}
92+
}
93+
```
94+
95+
## 문제 풀이
96+
부분집합을 사용해서 문제를 풀었습니다.
97+
98+
후보키를 알아내기 위해서는 컬럼의 부분집합을 통해 컬럼 조합을 알아낼 수 있습니다.
99+
- 부분집합을 알아내기 위해서 비트마스킹을 사용했습니다.
100+
- 부분집합은 작은 수(0001, 0010, 0011, ...)에서 점차 증가하는 방식으로 저장되므로
101+
- 작은 수에서 유일성을 만족한다면, 이후에 유일성을 만족한 수를 포함하는 수는 최소성을 만족하지 못하게 됩니다.
102+
- 예를 들어 0011(컬럼 0번과 컬럼 1번 조합)이 유일성을 만족한다고 할 때 1011, 1111은 0011이 포함되므로 최소성을 만족하지 못합니다.
103+
104+
알아낸 각 조합이 키의 조건 중 하나인 유일성을 만족하는지 테스트합니다.
105+
106+
유일성을 만족한다면, 해당 조합을 포함하는 다른 컬럼 조합들은 최소성을 만족하지 못하므로
107+
108+
유일성을 만족한 컬럼 조합을 포함하는 다른 컬럼 조합은 유일성을 만족하겠지만, 최소성을 만족하지 못하므로 후보키에서 탈락시키면 됩니다.
109+
- 포함하는지를 확인하기 위해 부분집합을 구할 때 알아낸 비트로 & 연산을 해서 포함하는지 아닌지를 검사하면 됩니다.
110+
111+
## 후기
112+
할만해유!
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.util.ArrayList;
2+
import java.util.HashSet;
3+
4+
public class Solution {
5+
static HashSet<String> keyCountSet = new HashSet<>();
6+
public int solution(String[][] relation) {
7+
int answer = 0;
8+
9+
int N = relation[0].length;
10+
ArrayList<Key> keys = new ArrayList<>();
11+
ArrayList<Integer> tmp = new ArrayList<>();
12+
for(int i=1; i<(1<<N); i++){
13+
tmp.clear();
14+
for(int j=0; j<N; j++){
15+
if((i & (1 << j)) > 0)
16+
tmp.add(j);
17+
}
18+
19+
Integer[] tmpArr = tmp.toArray(new Integer[tmp.size()]);
20+
keys.add(new Key(i, tmpArr));
21+
}
22+
23+
for(int i=0; i<keys.size(); i++){
24+
Key key = keys.get(i);
25+
if(!key.isUnique)
26+
continue;
27+
28+
boolean ret = check(key, relation);
29+
if(!ret)
30+
key.isUnique = false;
31+
else{
32+
for(int j=i+1; j<keys.size(); j++){
33+
if((keys.get(j).num & keys.get(i).num) == keys.get(i).num)
34+
keys.get(j).isUnique = false;
35+
}
36+
}
37+
}
38+
39+
answer = 0;
40+
for(Key key : keys){
41+
if(key.isUnique)
42+
answer++;
43+
}
44+
45+
return answer;
46+
}
47+
48+
public boolean check(Key key, String[][] relation) {
49+
keyCountSet.clear();
50+
for(int i=0; i<relation.length; i++){
51+
String candidateKey = "";
52+
for(int idx : key.arr)
53+
candidateKey += " " + relation[i][idx];
54+
if(keyCountSet.contains(candidateKey))
55+
return false;
56+
keyCountSet.add(candidateKey);
57+
}
58+
return true;
59+
}
60+
61+
static class Key{
62+
int num;
63+
Integer[] arr;
64+
boolean isUnique;
65+
66+
public Key(int num, Integer[] arr) {
67+
this.num = num;
68+
this.arr = arr;
69+
this.isUnique = true;
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)