๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฉ”๋‰ด๋ฆฌ๋‰ด์–ผ - Python

[๋ฌธ์ œ๋งํฌ] (https://programmers.co.kr/learn/courses/30/lessons/72411)

1. ๋ฌธ์ œ ์„ค๋ช…

  • ์ž…๋ ฅ orders : ๊ฐ ์†๋‹˜๋“ค์ด ์ฃผ๋ฌธํ•œ ๋‹จํ’ˆ๋ฉ”๋‰ด๋“ค์ด ๋ฌธ์ž์—ด ํ˜•์‹์œผ๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด course : "์Šค์นดํ”ผ"๊ฐ€ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์–ดํ•˜๋Š” ์ฝ”์Šค์š”๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋‹จํ’ˆ๋ฉ”๋‰ด๋“ค์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด
  • ์ถœ๋ ฅ "์Šค์นดํ”ผ"๊ฐ€ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋  ์ฝ”์Šค์š”๋ฆฌ์˜ ๋ฉ”๋‰ด ๊ตฌ์„ฑ์„ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ return ์‚ฌ์ „์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
  • ๊ทœ์น™ (1) ๊ฐ€์žฅ ๋งŽ์ด ์ฃผ๋ฌธํ•œ ๋‹จํ’ˆ ๋ฉ”๋‰ด๋“ค์„ ์ฝ”์Šค์š”๋ฆฌ ๋ฉ”๋‰ด๋กœ ๊ตฌ์„ฑ ex1 ) AC๊ฐ€ ๋„ค๋ฒˆ, CD๋Š” ์„ธ๋ฒˆ ์ผ ๋•Œ → AC๋ฅผ ๋‘๊ฐœ๊นŒ์ง€ ์š”๋ฆฌ2๊ฐœ์ฝ”์Šค๋กœ ์ฑ„ํƒ ex2) BCFG๊ฐ€ ๋‘๋ฒˆ, ACDE๊ฐ€ ๋‘ ๋ฒˆ ์ฃผ๋ฌธ๋จ → ๋‘˜ ๋‹ค ์š”๋ฆฌ4๊ฐœ์ฝ”์Šค๋กœ ์ฑ„ํƒ

(2) ์ฝ”์Šค์š”๋ฆฌ๋Š” ์ตœ์†Œ 2๊ฐ€์ง€ ์ด์ƒ์˜ ๋‹จํ’ˆ ๋ฉ”๋‰ด๋กœ ๊ตฌ์„ฑ (3) ์ตœ์†Œ ๋‘ ๋ช… ์ด์ƒ์˜ ์†๋‹˜์ด ์ฃผ๋ฌธํ•œ ๋‹จํ’ˆ์š”๋ฆฌ๋งŒ ์ฝ”์Šค์š”๋ฆฌ์˜ ํ›„๋ณด๊ฐ€ ๋จ

2. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

orders course result
["ABCFG", "AC", "CDE", "ACDE", "BCFG", "ACDEH"] [2,3,4] ["AC", "ACDE", "BCFG", "CDE"]
["ABCDE", "AB", "CD", "ADE", "XYZ", "XYZ", "ACD"] [2,3,5] ["ACD", "AD", "ADE", "CD", "XYZ"]
["XYZ", "XWY", "WXA"] [2,3,4] ["WX", "XY"]

3. ์ฝ”๋“œ

from itertools import combinations

def solution(orders, course):
    answer = []
    foodMap = [{} for _ in range(11)] #๋ฉ”๋‰ด ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉ(2~10๊นŒ์ง€ 11๊ฐœ์‚ฌ์šฉ)
    maxCnt = [0 for _ in range(11)] #๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ณ„ ์ตœ๋Œ€๋“ฑ์žฅํšŸ์ˆ˜ ์ €์žฅ 

    for str in orders:
    	#์ฝ”์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฉ”๋‰ด์˜ ๊ฐฏ์ˆ˜๋Š” ๋‘๊ฐœ ์ด์ƒ, ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๋ฉ”๋‰ด๊ฐœ์ˆ˜๋ฅผ ์ง„ํ–‰ AC๋ฉด ๋‘๊ฐœ, CDE๋ฉด ๋‘๊ฐœํ˜น์€์„ธ๊ฐœ
        for num in range(2,len(str)+1): 
        	#input์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์„ sortํ•ด์„œ ์ „๋‹ฌ, ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ๊ฐœ์ˆ˜(num)๋งŒํผ ์กฐํ•ฉ์„ ๋งŒ๋“ค์–ด์„œ ํŠœํ”Œํ˜•ํƒœ๋กœ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜
            for i in combinations(sorted(str),num): 
                key = ''.join(i) #string์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉ
                if key in foodMap[num]: #๋ฉ”๋‰ด๊ตฌ์„ฑ์ด ์ด๋ฏธ์žˆ์œผ๋ฉด
                    foodMap[num][key] +=1
                    maxCnt[num] = max(maxCnt[num],foodMap[num][key])
                else:
                    foodMap[num][key] = 1
    
    for num in course:
        for key, value in foodMap[num].items():
            if value >=2 and value == maxCnt[num]: #๋‘๊ฐœ ์ด์ƒ์ธ๊ฒƒ, ์ตœ๋Œ€๊ฐ’๊ณผ ๊ฐ™์€ ๋ชจ๋“  ๊ฒƒ
                answer.append(key)

    return sorted(answer)

์„ธ ๋ฒˆ์งธ ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ ๊ธฐ์ค€ ๋ณ€์ˆ˜ ์„ค๋ช…

  • foodMap : ๋‘๊ฐœ์กฐํ•ฉ์ฝ”์Šค์š”๋ฆฌ, ์„ธ๊ฐœ์กฐํ•ฉ์ฝ”์Šค์š”๋ฆฌ ๋“ฑ๋“ฑ ๊ฐ ๋ฉ”๋‰ด๊ฐฏ์ˆ˜๋ณ„ ํ›„๋ณด๋“ค์„ ๋‹ด๊ณ  ๊ทธ ํ›„๋ณด๋“ค์˜ orders์—์„œ์˜ ๋“ฑ์žฅํšŸ์ˆ˜๋ฅผ count
    • [{}, {}, {'XY': 2, 'XZ': 1, 'YZ': 1, 'WX': 2, 'WY': 1, 'AW': 1, 'AX': 1}, {'XYZ': 1, 'WXY': 1, 'AWX': 1}, {}, {}, {}, {}, {}, {}, {}]
  • maxCnt : ๋ฉ”๋‰ด๊ฐฏ์ˆ˜๋ณ„ ์ตœ๋Œ€ ๋“ฑ์žฅํšŸ์ˆ˜๋ฅผ count
    • [0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0]
  • answer
    • ['XY', 'WX']

4. ์–ด๋ ต๊ฑฐ๋‚˜ ํ—ท๊ฐˆ๋ ธ๋˜ ์ 

  1. ๋ฌธ์ œ์ดํ•ดํ•˜๊ธฐ

๊ฐ€์žฅ ๋งŽ์ด ์ฃผ๋ฌธํ•œ ๋‹จํ’ˆ ๋ฉ”๋‰ด๋“ค์„ ์ฝ”์Šค์š”๋ฆฌ ๋ฉ”๋‰ด๋กœ ๊ตฌ์„ฑ

์˜ˆ๋ฅผ ๋“ค์–ด, AC๊ฐ€ ๋„ค๋ฒˆ, CD๋Š” ์„ธ๋ฒˆ ์ผ ๋•Œ, AC๋ฅผ ๋‘๊ฐœ๊นŒ์ง€ ์ฝ”์Šค์š”๋ฆฌ๋กœ ์ฑ„ํƒํ•ด์•ผ ํ•œ๋‹ค.

๊ธ€์„ ์ฝ๋‹ค๊ฐ€ ๋‚œ๋…์ฆ์ด์™€์„œ ์ด ๋ถ€๋ถ„์„ ์บ์น˜ํ•˜์ง€ ๋ชปํ–ˆ๋‹ค..

  1. ์กฐํ•ฉ์„ ์–ด๋–ป๊ฒŒ ์กฐ์‚ฌํ• ์ง€

from itertools import combinations ์„ ์‚ฌ์šฉ

์กฐํ•ฉ์€ ์„ ํƒํ•˜๊ณ  ์„ ํƒ์•ˆํ•˜๊ณ ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

5. ์ฐธ๊ณ  ์ž๋ฃŒ

https://www.youtube.com/watch?v=PnXovk2JtU4