๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

2019 KAKAO BLIND RECRUITMENT ํ›„๋ณดํ‚ค Swift

by Fomagran ๐Ÿ’ป 2020. 12. 4.
728x90
๋ฐ˜์‘ํ˜•

Source Code


Solution


1. ์ปฌ๋Ÿผ์˜ ํฌ๊ธฐ์—์„œ 1๊ฐœ๋ถ€ํ„ฐ ์ปฌ๋Ÿผ์˜ ๊ฐฏ์ˆ˜๋งŒํผ ์ˆœ์„œ ์ƒ๊ด€์—†์ด ๋ฝ‘์„ ๋•Œ์˜ ์กฐํ•ฉ์„ ๋ชจ๋‘ ๊ตฌํ•ด์ค€๋‹ค.

 

ex) ์ปฌ๋Ÿผ์ด 4๊ฐœ์ผ ๋•Œ 1๊ฐœ๋ฅผ ๋ฝ‘๋Š” ์กฐํ•ฉ = [0],[1],[2],[3]

      ์ปฌ๋Ÿผ์ด 4๊ฐœ์ผ ๋•Œ 2๊ฐœ๋ฅผ ๋ฝ‘๋Š” ์กฐํ•ฉ = [0,1],[0,2],[0,3],[1,2],[1,3],[2,3]

      ์ปฌ๋Ÿผ์ด 4๊ฐœ์ผ ๋•Œ 3๊ฐœ๋ฅผ ๋ฝ‘๋Š” ์กฐํ•ฉ = [0,1,2],[0,1,3],[0,2,3],[1,2,3]

     ์ปฌ๋Ÿผ์ด 4๊ฐœ์ผ ๋•Œ 3๊ฐœ๋ฅผ ๋ฝ‘๋Š” ์กฐํ•ฉ =  [0,1,2,3]

 

2.์กฐํ•ฉ์— ํ›„๋ณดํ‚ค๊ฐ€ ํฌํ•จ๋˜๋Š”์ง€ ํ™•์ธ (์ตœ์†Œ์„ฑ ๊ฒ€์ฆ)

 

ํ›„๋ณดํ‚ค๊ฐ€ [0],[1,2]๊ฐ€

ํ˜„์žฌ ์กฐํ•ฉ์ด [0,1] ์ด๋ผ๋ฉด ์กฐํ•ฉ ์•ˆ์— ํ›„๋ณดํ‚ค ์ค‘ [0]์ด ํฌํ•จ๋˜๋ฏ€๋กœ ์ตœ์†Œ์„ฑ์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

ํ˜„์žฌ ์กฐํ•ฉ์ด [3] ์ด๋ผ๋ฉด ์กฐํ•ฉ ์•ˆ์— ํ›„๋ณดํ‚ค ์ค‘ ์–ด๋–ค ๊ฒƒ๋„ ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ตœ์†Œ์„ฑ์— ๋ถ€ํ•ฉํ•œ๋‹ค.

 

3.. ์กฐํ•ฉ๋“ค์„ ์ธ๋ฑ์Šค๋กœ Relation์•ˆ์— ๊ฐ ํ–‰์˜ ์†์„ฑ๋“ค์„ ๋ชจ์•„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ณ  ๊ทธ ๋ฐฐ์—ด๋“ค ์ค‘ ์ค‘๋ณต์ด ์žˆ๋Š”์ง€ ํ™•์ธ (์œ ์ผ์„ฑ ๊ฒ€์ฆ)

 

ex) Relation์ด [["100","ryan","music","2"],["200","apeach","math","2"],["300","tube","computer","3"],["400","con","computer","4"],["500","muzi","music","3"],["600","apeach","music","2"]] ์ด๊ณ 

์กฐํ•ฉ์ด [0,1,2]์ผ ๊ฒฝ์šฐ์— ๊ฐ relation์˜ ํ–‰์—์„œ ์ธ๋ฑ์Šค๊ฐ€ 0,1,2์ธ ๊ฒƒ๋“ค์„ ๋ชจ๋‘ ๋ชจ์•„์ค€๋‹ค.

-> ["100","ryan","music"],["200","apeach","math"],["300","tube","computer"],["400","con","computer"],["500","muzi","music"],["600","apeach","music"]

๋งŒ์•ฝ ์ค‘๋ณต์ด ์—†๋‹ค๋ฉด Relation์˜ ํ–‰์˜ ๊ฐฏ์ˆ˜์™€ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ›„๋ณดํ‚ค์— ๋„ฃ์–ด์คŒ

 

Problem


 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ›„๋ณดํ‚ค

[["100","ryan","music","2"],["200","apeach","math","2"],["300","tube","computer","3"],["400","con","computer","4"],["500","muzi","music","3"],["600","apeach","music","2"]] 2

programmers.co.kr

Reference


 

 

 

Combination ์กฐํ•ฉ

- n๊ฐœ์˜ ์›์†Œ์—์„œ r ๊ฐœ์˜ ์›์†Œ๋ฅผ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ๋ฝ‘๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ - nCr = n! / (n-r)! * r! - nCr = n-1Cr-1 + n-1Cr ์กฐํ•ฉ? ์กฐํ•ฉ์€ n ๊ฐœ์˜ ์›์†Œ ์ค‘์—์„œ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ์›ํ•˜๋Š” ๊ฐœ์ˆ˜๋งŒํผ ๋ฝ‘๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ

woongsios.tistory.com

์กฐํ•ฉ์„ ๊ตฌํ•  ๋•Œ ์‚ผ์“ฐ๋‹˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ์กฐํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ๊ฒƒ

 

A๋ฐฐ์—ด์•ˆ์— B๋ฐฐ์—ด์ด ๋ชจ๋‘ ํฌํ•จ๋˜๋Š”์ง€ ์•„๋Š” ๋ฐฉ๋ฒ•

 

A๋ฐฐ์—ด์˜ Set๋กœ ๋งŒ๋“ค์–ด์ค€๋’ค isSuperSet(of:) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ B๊ฐ€ ํฌํ•จ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

 

์ˆœ์„œ์—†์ด ๋ฝ‘๋Š” ์กฐํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋ฒ•

 

1
2
3
4
5
6
7
8
9
10
11
12
13
//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)
    }
}

 

 

2๋‹จ๊ณ„๋ฅผ ๋ชจ๋‘ ํ‘ผ์ค„ ์•Œ์•˜๋”๋‹ˆ ํ›„๋ณดํ‚ค๊ฐ€ ๋‚จ์•„์žˆ์—ˆ๋„ค....ใ…Žใ…Žใ…Ž ์ง„์งœ ๋‹ค ํ’€์—ˆ๋‹ค!!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€