후보키

LJM·2023년 8월 20일
0

programmers

목록 보기
69/92

https://school.programmers.co.kr/learn/courses/30/lessons/42890

어떻게 풀어야할지 찾아보았고 이해한것을 토대로 직접 작성하여서 풀었다
비트마스크라는 개념은 어렵지 않다 현업에서도 사용했던 것이라서 다만 문제에 응용할 수 잇는 발상이 중요하다

그리고 헷갈렸던것은 3(11) & 2(10) 이 1이라고 잘못생각하였던 것이었다
3&2 는 2(10) 이다

import java.util.*;

class Solution {
    
    ArrayList<ArrayList<Integer>> arr = new ArrayList<>();
    
    public int solution(String[][] relation) {
        
        int colsize = relation[0].length;
            
        for(int i = 1; i < (1<<colsize); ++i)
        {
            int cankey = i;
            if(checkunique(cankey, relation) && checkminimal(cankey))
            {
                ArrayList<Integer> bitmask = new ArrayList<>();
                int pos = 0;
                while(cankey > 0)
                {
                    if(cankey % 2 > 0)
                        bitmask.add(1 << pos);

                    cankey/=2;
                    pos++;
                }
                arr.add(bitmask);
            }
                
        }
        
        int answer = arr.size();
        return answer;
    }
    //기존의 조합이 새로운 후보키의 부분집합인가?
    public boolean checkminimal(int cankey)
    {
        for(ArrayList<Integer> bitmask : arr)
        {
            boolean isAllMatch = true;
            for(int bit : bitmask)
            {
                if((bit & cankey) == 0)
                {
                    isAllMatch = false;
                }
            }
            if(isAllMatch)
                return false;
        }
        //System.out.println(cankey);
        return true;
    }
    
    //후보키의 유일성 체크
    public boolean checkunique(int cankey, String[][] relation)
    {
        
        HashSet<String> set = new HashSet<>();
        
        StringBuilder sb = new StringBuilder();
        
        for(int i= 0; i < relation.length; ++i)
        {
            String[] tuple = relation[i];
            
            for(int j= 0; j < tuple.length; ++j)
            {
                if((1 << j)==((1 << j) & cankey))
                {
                    //System.out.println(i + "," + j +"," +cankey);
                    sb.append(tuple[j]);    
                }
            }
            
            if(set.contains(sb.toString()))
            {
                //System.out.println(sb.toString());
                return false;
            }
            
            set.add(sb.toString());
            sb.setLength(0);
        }
        //System.out.println(cankey);
        return true;
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글