-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMinimality.java
64 lines (51 loc) · 1.8 KB
/
Minimality.java
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
import java.util.*;
class Solution {
public ArrayList<Integer> key;
public int solution(String[][] relation) {
key = new ArrayList<Integer>();
boolean[] visited = new boolean[relation[0].length];
for(int i = 0; i < relation[0].length; i++) {
dfs(0, 0, i + 1, visited, relation);
}
return key.size();
}
public void dfs(int start, int cnt, int len, boolean[] visited, String[][] relation) {
if(cnt == len) {
ArrayList<Integer> colIdxList = new ArrayList<>();
for(int i = 0; i < visited.length; i++) {
if(visited[i]) colIdxList.add(i);
}
checkCandidateKey(colIdxList, relation);
}
for(int i = start; i < visited.length; i++) {
if(!visited[i]) {
visited[i] = true;
dfs(i, cnt + 1, len, visited, relation);
visited[i] = false;
}
}
}
public void checkCandidateKey(ArrayList<Integer> colIdxList, String[][] relation) {
for(int i = 0; i < relation.length - 1; i++) {
for(int j = i + 1; j < relation.length; j++) {
boolean isDup = true;
for(int idx: colIdxList) {
if(!relation[i][idx].equals(relation[j][idx])) isDup = false;
}
if(isDup) return;
}
}
int keyValue = 0;
for(int i = 0; i < colIdxList.size(); i++) {
keyValue += Math.pow(2, colIdxList.get(i));
}
if(isMinimal(keyValue)) key.add(keyValue);
}
//비트 연산을 통해 최소성 만족 여부 체크
public boolean isMinimal(int keyValue) {
for(int i: key) {
if((keyValue & i) == i) return false;
}
return true;
}
}