Skip to content

Latest commit

ย 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
ย 
ย 
ย 
ย 

[2019 KAKAO BLIND RECRUITMENT - 3] ํ›„๋ณดํ‚ค

๋น„ํŠธ๋งˆ์Šคํฌ๋กœ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค

for (int i = 1; i < (1 << cols); i++) {
    System.out.println(i);
}

์ค‘๋ณต ๊ฒ€์‚ฌํ•˜๊ธฐ

์ฒ˜์Œ์—๋Š” isUnique ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ฒ€์‚ฌํ•˜์˜€๋‹ค.

static boolean isUnique(LinkedList<String>[] list) {
    for (int i = 0; i < list.length - 1; i++) {
        for (int j = i+1; j < list.length; j++) {
            if (list[i].equals(list[j])) {
                return false;
            }
        }
    }

    return true;
}

ํ•˜์ง€๋งŒ ์ด๋ณด๋‹ค๋„ HashSet ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ํ›จ์”ฌ ๋น ๋ฅธ ์†๋„์™€ ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ๋กœ ์ค‘๋ณต ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

HashSet์€ Set ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ์ปฌ๋ ‰์…˜์ด๋ฉฐ, Set ์ธํ„ฐํŽ˜์ด์Šค์˜ ํŠน์ง•๋Œ€๋กœ HashSet์€ ์ค‘๋ณต๋œ ์š”์†Œ๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋งŒ์ผ HashSet์— ์ด๋ฏธ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์š”์†Œ์™€ ์ค‘๋ณต๋œ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์ด ๋ฉ”์„œ๋“œ๋“ค์€ false๋ฅผ ๋ฐ˜ํ™˜ํ•จ์œผ๋กœ์จ ์ค‘๋ณต๋œ ์š”์†Œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€์— ์‹คํŒจํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ฆฐ๋‹ค. ๋”ฐ๋ผ์„œ HashSet์— string ํ˜•ํƒœ๋กœ ์„ ํƒํ•œ ์š”์†Œ๋ฅผ ์ €์žฅํ•˜๊ณ , ๋งˆ์ง€๋ง‰์— ๊ทธ set์˜ ํฌ๊ธฐ๊ฐ€ ์ „์ฒด ํŠœํ”Œ๋“ค์˜ ํฌ๊ธฐ์™€ ๋งž์ง€ ์•Š๋‹ค๋ฉด ์ค‘๋ณต๋œ ์š”์†Œ๊ฐ€ ์žˆ์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

for (int i = 1; i < (1 << cols); i++) {
    Set<String> set = new HashSet<>();

    for (String[] row : relation) {
        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < cols; k++) {
            if ((i & (1 << k)) > 0)
                sb.append(row[k]);
        }
        set.add(sb.toString());
    }

    if (set.size() == rows) {
        boolean isExist = false;
        for (Integer c : cand) {
            if ((c & i) == c) {
                isExist = true;
                break;
            }
        }
        if (!isExist) cand.add(i);
    }
}

image