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

[Swift] 2022 KAKAO BLIND RECRUITMENT ์–‘๊ณผ ๋Š‘๋Œ€

by Fomagran ๐Ÿ’ป 2022. 1. 19.
728x90
๋ฐ˜์‘ํ˜•

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์–‘๊ณผ ๋Š‘๋Œ€

[0,0,1,1,1,0,1,0,1,0,1,1] [[0,1],[1,2],[1,4],[0,8],[8,7],[9,10],[9,11],[4,3],[6,5],[4,6],[8,9]] 5 [0,1,0,1,1,0,1,0,0,1,0] [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6],[3,7],[4,8],[6,9],[9,10]] 5

programmers.co.kr


Solution


ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์™„์ „ํƒ์ƒ‰๊ณผ DFS๋กœ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

1. edge๋ฅผ ์ด์šฉํ•ด ๋ถ€๋ชจ์™€ ์ž์‹์„ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

 

func connectEdge(_ edges:[[Int]]) {
	for edge in edges {
    	if pc[edge[0]] == nil {
        	pc[edge[0]] = [edge[1]] 
        }else {
        	pc[edge[0]]!.append(edge[1]) 
        } 
    } 
}

 

2. ๋Š‘๋Œ€์™€ ์–‘์˜ ์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋ฉฐ DFS๋กœ ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•œ ๋…ธ๋“œ๋“ค์„ ์žฌ๊ท€ํ•ด์ค๋‹ˆ๋‹ค.


์—ฌ๊ธฐ์„œ ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•œ ๋…ธ๋“œ๋“ค์€ ๊ฐ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋“ค์˜ ์ž์‹๋“ค์„ ์ถ”๊ฐ€ํ•ด์ค€ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ฃผ์˜ํ•  ์ ์€ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ ์‚ญ์ œํ•ด ์ฃผ์–ด์•ผ ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งค๋ฒˆ ์–‘์˜ ์ตœ๋Œ€ ์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค๋‹ˆ๋‹ค.

์–‘์˜ ์ˆ˜๊ฐ€ ๋Š‘๋Œ€์˜ ์ˆ˜ ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์—๋งŒ dfs๋กœ ํƒ์ƒ‰ํ•ด์ค๋‹ˆ๋‹ค.

 

 maxCount = max(count.0,maxCount) 
 
 for node in visitableNodes {
    var newVisitableNodes = visitableNodes 
    var newCount = count 
    let index = newVisitableNodes.firstIndex(of:node)! 
    newVisitableNodes.remove(at: index) 
    newVisitableNodes.append(contentsOf:pc[node] ?? []) 
    newCount = info[node] == 0 ? (count.0+1,count.1) : (count.0,count.1+1) 
    
    if newCount.0 > newCount.1 {
    	dfs(newCount, newVisitableNodes, info) 
    } 
}

 

3. ๊ฐ€์žฅ ํฐ ์–‘์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

 

func solution(_ info:[Int], _ edges:[[Int]]) -> Int {
	connectEdge(edges) dfs((1, 0), pc[0]!, info) 
    return maxCount 
}

Source Code

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€