๊ธฐ๋ก๋ฐฉ

Lv.2 : ๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ ๋ณธ๋ฌธ

CodingTest/Python

Lv.2 : ๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ

Soom_1n 2022. 9. 20. 12:01

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

 

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

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

programmers.co.kr


 


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋œ ์ฃผ๋ฌธ ๋งˆ๋‹ค ์ฝ”์Šค ๊ธธ์ด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์„ ์ฐพ๋Š”๋‹ค.
  • ์กฐํ•ฉ์„ ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅํ•˜๋ฉฐ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
  • ์ €์žฅ๋œ ์กฐํ•ฉ์—์„œ ๊ธธ์ด๋ณ„ ์ตœ๋Œ€๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • ์ตœ๋Œ€๊ฐ’์ด ์ค‘๋ณต์ด๋ฉด ๋ชจ๋‘ ์„ ํƒํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

def solution(orders, course):
    dic = {}
    def find_str(s, pick, d, j): # ์žฌ๊ท€๋กœ ๋ฌธ์ž ์กฐํ•ฉ
        if len(pick) >= d:          # ์ฐพ๋Š” ๊ธธ์ด(d)์˜ ๋ฌธ์ž๊ฐ€ ์™„์„ฑ๋˜๋ฉด
            pick = ''.join(sorted(list(pick))) # ๋ฌธ์ž์—ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
            if pick in dic:             # ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅ
                dic[pick] += 1
            else:
                dic[pick] = 0
            return
        
        for i in range(j,len(s)):   # ๋ฌธ์ž ์กฐํ•ฉ์„ ์œ„ํ•ด ์žฌ๊ท€
            find_str(s, pick+s[i], d, i+1)

    for c in course: # ๋ฌธ์ž์—ด ๊ธธ์ด
        for i in orders: # ๋ถ„ํ•ด ํ•  ๋ฌธ์ž์—ด
            find_str(i, "", c, 0)

    temp = []
    for c in course: # ๋ฌธ์ž์—ด ๊ธธ์ด๋ณ„๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ถ„ํ•ด
        temp.append({k:v for k, v in dic.items() if len(k) == c})

    answer = []
    for d in temp:
        for k, v in d.items(): # ๊ธธ์ด๋ณ„ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ value ์ตœ๋Œ€๊ฐ’์˜ key๋ฅผ ์ €์žฅ
            if max(d.values()) == v and max(d.values()) != 0:
                answer.append(k)
    return sorted(answer) # ์˜ค๋ฆ„์ฐจ์ˆœ ๋ฐ˜ํ™˜

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์žฌ๊ท€ ํ˜•์‹์œผ๋กœ ๋ฌธ์ž ์กฐํ•ฉ์„ ๋งŒ๋“ค์–ด๊ฐ€๋ฉฐ ๋”•์…”๋„ˆ๋ฆฌ์— ์นด์šดํŠธํ•œ๋‹ค.
    • ์ด๋•Œ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ํ›„ ์ธ๋ฑ์‹ฑํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด ๊ธธ์ด๋ณ„๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋‚˜๋ˆ ์„œ temp ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค.
  • ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ณ„ value ์ตœ๋Œ€๊ฐ’์˜ key๋ฅผ answer ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค.
  • answer๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ฉฐ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

from collections import defaultdict, Counter
from itertools import combinations

def solution(orders, course):
    
    cntr = defaultdict(Counter) # {2:{'AB': 3, 'CD': 1, ...}, ...}
    
    for order in orders: # 1 : ๊ฐ order์— ๋Œ€ํ•ด combinations๋กœ ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ ๋ฝ‘์•„์„œ Counter์— ์ €์žฅ
        for n in course:
            if n <= len(order):
                cntr[n].update(combinations(sorted(order), n))
            
    answer = []
    
    for n in cntr:
        max_cnt = max(cntr[n].values()) # 2 : order ์ˆ˜์— ๋Œ€ํ•ด ์ตœ๋Œ€ ๋นˆ๋„์ˆ˜ ๊ตฌํ•˜๊ธฐ
        if max_cnt > 1:
            # 3 : 2์—์„œ ์–ป์€ ๋นˆ๋„์ˆ˜๋ž‘ ๊ฐ™์€ ์ฝค๋ณด์„ธํŠธ ๊ตฌํ•˜๊ธฐ
            answer += [''.join(k) for k, v in cntr[n].items() if v == max_cnt]
    
    return sorted(answer) # 4 : ์ •๋ ฌํ•ด์„œ ๋ฐ˜ํ™˜
  • ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ Counter ํ˜•์‹์œผ๋กœ ์„ ์–ธํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
    • combinations๋กœ ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ๋ฝ‘์•„๋‚ธ๋‹ค.
    • ๋”•์…”๋„ˆ๋ฆฌ์— ์ฃผ๋ฌธ ๊ธธ์ด๋ณ„๋กœ ์กฐํ•ฉ์„ ์นด์šดํŠธํ•ด ์ €์žฅํ•œ๋‹ค.
  • ๋”•์…”๋„ˆ๋ฆฌ์—์„œ value ์ตœ๋Œ€๊ฐ’์˜ key๋ฅผ ๋ฝ‘์•„๋‚ธ๋‹ค.
    • ์ตœ๋Œ€๊ฐ’์€ ์ž๊ธฐ์ž์‹ ์„ ์„ธ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•ด 1๋ณด๋‹ค ์ปค์•ผํ•œ๋‹ค.
    • combinations์—์„œ ํŠœํ”Œ ํ˜•์‹์œผ๋กœ ์ชผ๊ฐœ์ ธ ๋‚˜์™”๊ธฐ ๋•Œ๋ฌธ์— join์„ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด๋กœ ํ•ฉ์นœ๋‹ค.
  • ์ •๋ ฌํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ข€ ๊ผฌ์ธ ๋ถ€๋ถ„์ด ๋งŽ์•„ ํ‘ธ๋Š”๋ฐ ์‹œ๊ฐ„์ด ๊ฝค ๊ฑธ๋ ธ๋‹ค.
  • ๊ทธ๋ž˜๋„ ์ž˜ ํ’€์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ํ˜„ํƒ€๊ฐ€ ์กฐ๊ธˆ ์™”๋‹ค...
    • ์กฐํ•ฉ ์ฐพ๊ธฐ๋ฅผ ์žฌ๊ท€๋กœ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ combinations() ํ•จ์ˆ˜๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    • defalutdict()๋กœ ํŠน์ •ํ•œ ํ˜•์‹(์—ฌ๊ธฐ์„œ๋Š” Counter)์„ ์ง€์ •ํ•ด์„œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ๋˜ ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ Counter์˜ most_common() ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ตœ๋Œ€๊ฐ’ ์ฐพ๊ธฐ๋ฅผ ๋” ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
  • ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋ฉฐ ๋”•์…”๋„ˆ๋ฆฌ ๋ถ„ํ•ด, ๋ฌธ์ž์—ด ์กฐํ•ฉ์„ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค.
  • ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ทธ๋Ÿฐ์ง€ ๋‚ด ์ฝ”๋“œ์™€ combinations() ์‚ฌ์šฉ ์ฝ”๋“œ์˜ ์‹œ๊ฐ„์ฐจ์ด๊ฐ€ ์ปธ๋‹ค.

728x90

'CodingTest > Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_18238 : ZOAC 2  (0) 2022.10.19
BOJ_1789 : ์ˆ˜๋“ค์˜ ํ•ฉ  (0) 2022.10.13
Lv.2 : ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ  (0) 2022.09.19
Lv.1 : ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ  (0) 2022.09.19
Lv.2 : ์Šคํ‚ฌํŠธ๋ฆฌ  (0) 2022.09.18