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
Reference
์กฐํฉ์ ๊ตฌํ ๋ ์ผ์ฐ๋ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ์กฐํ์ต๋๋ค.
์๋กญ๊ฒ ์๊ฒ ๋๊ฒ
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๋จ๊ณ๋ฅผ ๋ชจ๋ ํผ์ค ์์๋๋ ํ๋ณดํค๊ฐ ๋จ์์์๋ค....ใ ใ ใ ์ง์ง ๋ค ํ์๋ค!!
'๐ Problem Solution > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค ๋คํธ์ํฌ Swift (0) | 2020.12.10 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค SQL (0) | 2020.12.06 |
ํ๋ก๊ทธ๋๋จธ์ค ์๊ฐ ์ฝ๋ ์ฑ๋ฆฐ์ง ์์ฆ1 ์ผ๊ฐ ๋ฌํฝ์ด Swift (0) | 2020.12.01 |
2018 KAKAO BLIND RECRUITMENT[3์ฐจ] ๋ฐฉ๊ธ๊ทธ๊ณก Swift (0) | 2020.11.29 |
2018 KAKAO BLIND RECRUITMENT [3์ฐจ] ํ์ผ๋ช ์ ๋ ฌ Swift (0) | 2020.11.19 |
๋๊ธ