Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao - ํ›„๋ณดํ‚ค (Lv.2)

๊ฐ์ž ๐Ÿฅ” 2022. 8. 31. 18:00
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

ํ›„๋ณดํ‚ค๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์„ ์ƒ๊ฐํ•ด์•ผํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ combination ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. 

//n๊ฐœ์ค‘ m๋ฅผ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ๋ฝ‘์•„์•ผํ•  ์กฐํ•ฉ๋“ค์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜
func combination(n: [Int], m: Int, current index: Int, pickedArray: [Int]) {
    if m == 0 {
        cases.append(pickedArray)
    }else if index == n.count {
        return
    }else {
        var newSelected = pickedArray
        newSelected.append(n[index])
        combination(n: n, m: m-1, current: index+1, pickedArray: newSelected)
        combination(n: n, m: m, current: index+1, pickedArray: pickedArray)
    }
}

์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

// ์‚ฌ์šฉ๋ฒ•
var temp = [1, 2, 3, 4, 5]
for i in 0..<temp.count {
    combination(n: temp, m: i+1, current: 0, pickedArray: [])
}
print(cases)

์ด๋ ‡๊ฒŒ ๋ชจ๋“  ํ›„๋ณดํ‚ค๋“ค์˜ ์กฐํ•ฉ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๊ตฌํ•˜๊ฒŒ ๋˜๋ฉด, ํ•ด๋‹น ํ›„๋ณดํ‚ค์— ํ•ด๋‹นํ•˜๋Š” value๋“ค์„ ๋„ฃ๊ณ , value๋“ค์˜ ๋ชจ์Œ์˜ ๊ธธ์ด๊ฐ€ relation ๊ธธ์ด์™€ ๊ฐ™๋‹ค๋ฉด ์กฐ๊ฑด์—์„œ ์ฃผ์–ด์ง„ '์œ ์ผ์„ฑ'์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (์•„๋ž˜ ์‚ฌ์ง„)

์ด์ œ ์กฐ๊ฑด์—์„œ ์ฃผ์–ด์ง„, ์ตœ์†Œ์„ฑ์„ ๋งŒ์กฑํ•˜๋Š”๊ฐ€๋ฅผ ๋ด์•ผํ•˜๋Š”๋ฐ, ์ด๋ถ€๋ถ„์ด ์‚ด์ง ์ดํ•ด๊ฐ€ ๊ฐ€์งˆ ์•Š์•˜๋‹ค. ์œ„ ์‚ฌ์ง„ ์˜ˆ์‹œ๋กœ ๋ณด๋ฉด [ํ•™๋ฒˆ, ์ด๋ฆ„] ์ด ์ด๋ฏธ ํ›„๋ณดํ‚ค์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์— ํ•ด๋‹นํ•œ๋‹ค๋ฉด, ์ตœ์†Œ์„ฑ์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.  (์•„๋ž˜ ์„ค๋ช…)

๐ŸŸ  ์ •๋‹ต ์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_%ED%9B%84%EB%B3%B4%ED%82%A4/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

import Foundation

//์กฐํ•ฉ๋“ค์„ ๋‹ด์„ ๋ฐฐ์—ด
var cases = [[Int]]()

func solution(_ relation:[[String]]) -> Int {
    var candidateKey = [[Int]]()
    var colsize = [Int]()

    for i in 0..<relation[0].count {
        colsize.append(i)
    }
    // ์ปฌ๋Ÿผ์ˆ˜๋“ค์˜ ์กฐํ•ฉ ์ƒ์„ฑ
    for i in 0..<colsize.count {
        combination(n: colsize, m: i+1, current: 0, pickedArray: [])
    }

    // ์ตœ์†Œ์„ฑ ํ™•์ธ isSuperset() ๋ถ€๋ถ„์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธ
    out: for c in cases {
    //์ตœ์†Œ์„ฑ์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธ
        let set = Set(c)
        //ํ›„๋ณดํ‚ค๋“ค์„ ํƒ์ƒ‰
        for key in candidateKey {
            //ํ›„๋ณดํ‚ค ์•ˆ์— ํ˜„์žฌ key๊ฐ€ ํฌํ•จ๋œ๊ฒŒ ์žˆ๋‹ค๋ฉด ๋„˜์–ด๊ฐ„๋‹ค.
            if set.isSuperset(of: key) {
                continue out
            }
        }

        var rowSet = Set<Array<String>>()
        // row๋ณ„๋กœ combi์— ์žˆ๋Š” ์†์„ฑ๋“ค๋งŒ ๋ฝ‘์•„์„œ ํ™•์ธ
        for row in relation {
            var tuple = [String]()

            for i in c {
                tuple.append(row[i])
            }

            if !rowSet.contains(tuple) {
                rowSet.insert(tuple)
            } else {
                break
            }
        }

        if rowSet.count == relation.count {
            candidateKey.append(c)
        }
    }

    return candidateKey.count
}

//n๊ฐœ์ค‘ m๋ฅผ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ๋ฝ‘์•„์•ผํ•  ์กฐํ•ฉ๋“ค์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜
func combination(n: [Int], m: Int, current index: Int, pickedArray: [Int]) {
    if m == 0 {
        cases.append(pickedArray)
    }else if index == n.count {
        return
    }else {
        var newSelected = pickedArray
        newSelected.append(n[index])
        combination(n: n, m: m-1, current: index+1, pickedArray: newSelected)
        combination(n: n, m: m, current: index+1, pickedArray: pickedArray)
    }
}

 

๋ฐ˜์‘ํ˜•