[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ด‘๋ฌผ ์บ๊ธฐ (Python, ๊ตฌํ˜„)

    ๋ฐ˜์‘ํ˜•

     

     

     

    ๐ŸŒฑ ๋ฌธ์ œ

     

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

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

    programmers.co.kr

    • ์ž‘์—…์„ ๋๋‚ด๊ธฐ๊นŒ์ง€ ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ ํ”ผ๋กœ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
    • ๊ด‘๋ฌผ์„ ๋ชจ๋‘ ์บ๊ฑฐ๋‚˜ ๊ณก๊ฐฑ์ด๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๋ฉด ์ž‘์—… ๋๋‚จ
    • ์–ด๋–ค ๊ด‘๋ฌผ์„ ์–ด๋–ค ๊ณก๊ฐฑ์ด๋กœ ์บ๋Š”์ง€์— ๋”ฐ๋ผ ์†Œ๋ชจ๋˜๋Š” ํ”ผ๋กœ๋„๊ฐ€ ๋‹ค๋ฆ„
    • ํ•˜๋‚˜์˜ ๊ณก๊ฐฑ์ด๋กœ 5๊ฐœ์˜ ๊ด‘๋ฌผ์„ ์บ˜ ์ˆ˜ ์žˆ์Œ

     

    ๐ŸŒฑ ํ’€์ด1 ์™„์ „ ํƒ์ƒ‰ → ์‹œ๊ฐ„ ์ดˆ๊ณผ

    ์ฒ˜์Œ์—๋Š” ๊ณก๊ฐฑ์ด(picks)์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ๋Œ€ 15๊ฐœ ์ด๊ณ , ๊ด‘๋ฌผ(minerals)์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 50์ด๋ผ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ณก๊ฐฑ์ด 1๊ฐœ๋‹น ๊ด‘๋ฌผ 5๊ฐœ๋ฅผ ์บ˜ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ตœ๋Œ€๋กœ ํ•„์š”ํ•œ ๊ณก๊ฐฑ์ด ์ˆ˜๋Š” 10๊ฐœ์ž…๋‹ˆ๋‹ค. ๋˜, ๊ณก๊ฐฑ์ด ์ข…๋ฅ˜๋Š” 3๊ฐœ์ด๋ฏ€๋กœ ๋ชจ๋“  ๊ณก๊ฐฑ์ด ์ˆœ์„œ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ์ตœ๋Œ€ $3^{10} = 59049$ ๋ผ๊ณ  ์ƒ๊ฐํ•˜์—ฌ ์™„์ „ ํƒ์ƒ‰์œผ๋กœ ํ’€์ด๋ฅผ ํ•˜์˜€๋Š”๋ฐ, 4๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ํ„ฐ์กŒ์Šต๋‹ˆ๋‹ค. (์™œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์•„์ง ๋ชจ๋ฅด๊ฒ ์Œ,,ใ…œใ…œ)

    # ์‹œ๊ฐ„ ์ดˆ๊ณผ ์ฝ”๋“œ
    def solution(picks, minerals):
        answer = 0
    
        n = len(minerals) // 5
        if len(minerals) % 5 != 0:
            n += 1
    
        pick_list = []
        pick_list += ['dia' for _ in range(picks[0])]
        pick_list += ['iron' for _ in range(picks[1])]
        pick_list += ['stone' for _ in range(picks[2])]
    
        answer = 25 * 50
    
        if len(pick_list) < n:
            n = len(pick_list)
    
        for pick in set(permutations(pick_list, n)):
            tiredness = 0
            minerals_ = deepcopy(minerals)
            for p in pick:
                for _ in range(5):
                    if minerals_:
                        tiredness += table[p][mineral_dict[minerals_.pop(0)]]
    
            if tiredness < answer:
                answer = tiredness
    
        return answer

     

     

    ๐ŸŒฑ ํ’€์ด2 ๊ตฌํ˜„

    ์ฃผ์–ด์ง„ ํ‘œ๋ฅผ ์กฐ๊ธˆ๋งŒ ์ž์„ธํžˆ ๋ณด๋ฉด ์™„์ „ ํƒ์ƒ‰์„ ํ•˜์ง€ ์•Š๊ณ ๋„ ์ตœ์†Œํ•œ์˜ ํ”ผ๋กœ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ํ‘œ์—์„œ ๋‹ค์ด์•„๋ชฌ๋“œ ๊ณก๊ฐฑ์ด๊ฐ€ ๊ฐ€์žฅ ์ ์€ ํ”ผ๋กœ๋„๋ฅผ ์†Œ๋ชจํ•˜๊ณ  ๋Œ ๊ณก๊ฐฑ์ด๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ํ”ผ๋กœ๋„๋ฅผ ์†Œ๋ชจํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ€์žฅ ๋งŽ์€ ํ”ผ๋กœ๋„๋ฅผ ์†Œ๋ชจํ•˜๋Š” ๊ด‘๋ฌผ๋“ค์„ ๊ฐ€์žฅ ์ ์€ ํ”ผ๋กœ๋„๋ฅผ ์†Œ๋ชจํ•˜๋Š” ๊ณก๊ฐฑ์ด ์ˆœ์„œ๋Œ€๋กœ ๋งคํ•‘(?)ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์ฆ‰, ๊ฐ€์žฅ ํ”ผ๋กœ๋„๋ฅผ ๋งŽ์ด ์†Œ๋ชจํ•˜๋Š” ๊ด‘๋ฌผ๊ณผ ๊ฐ€์žฅ ํ”ผ๋กœ๋„๋ฅผ ์ ๊ฒŒ ์†Œ๋ชจํ•˜์—ฌ ๊ด‘๋ฌผ์„ ์บ˜ ์ˆ˜ ์žˆ๋Š” ๊ณก๊ฐฑ์ด๋ฅผ ๋งคํ•‘!

    1. ์บ˜ ์ˆ˜ ์žˆ๋Š” ๊ด‘๋ฌผ์˜ ๊ฐœ์ˆ˜

    # ์บ˜ ์ˆ˜ ์žˆ๋Š” ๊ด‘๋ฌผ์˜ ๊ฐœ์ˆ˜
    n = min(len(minerals), sum(picks) * 5)

    ๋จผ์ € ์บ˜ ์ˆ˜ ์žˆ๋Š” ๊ด‘๋ฌผ์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธํ•ฉ๋‹ˆ๋‹ค. ๊ด‘๋ฌผ์„ ๋ชจ๋‘ ์บ๊ฑฐ๋‚˜ ๊ณก๊ฐฑ์ด๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๋ฉด ์ž‘์—… ๋๋‚˜๋ฏ€๋กœ ๊ด‘๋ฌผ์˜ ๊ฐœ์ˆ˜์™€ ๊ณก๊ฐฑ์ด์˜ ๊ฐœ์ˆ˜ * 5 ์ค‘์— ๋” ์ž‘์€ ๊ฐ’์ด ์บ˜ ์ˆ˜ ์žˆ๋Š” ๊ด‘๋ฌผ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

     

    2. ๊ด‘๋ฌผ ์กฐ์‚ฌ

    mineral_count = []
    tmp = []
    for idx in range(n):
        if idx % 5 == 0:  # 5๊ฐœ๋งˆ๋‹ค ์ดˆ๊ธฐํ™”
            if tmp:
                mineral_count.append(tmp)
            tmp = [0, 0, 0]
    
        if minerals[idx] == "diamond":
            tmp[0] += 1
        elif minerals[idx] == "iron":
            tmp[1] += 1
        else:
            tmp[2] += 1
        if idx == n - 1:
            mineral_count.append(tmp)
            break

    ๊ณก๊ฐฑ์ด ํ•˜๋‚˜ ๋‹น ๊ด‘๋ฌผ์„ 5๊ฐœ์”ฉ ์บ˜ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์–ด์ง„ ๊ด‘๋ฌผ๋“ค์„ 5๊ฐœ์”ฉ ๋ฌถ์–ด์„œ [diamond ๊ฐœ์ˆ˜, iron ๊ฐœ์ˆ˜, stone ๊ฐœ์ˆ˜] ํ˜•ํƒœ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    mineral์ด ["diamond", "diamond", "diamond", "iron", "iron", "diamond", "iron", "stone"]์ธ ๊ฒฝ์šฐ, 5๊ฐœ์”ฉ ๋ฌถ์œผ๋ฉด
    [["diamond", "diamond", "diamond", "iron", "iron"], ["diamond", "iron", "stone"]] ์œผ๋กœ ๋ฌถ์ด๊ณ ,
    mineral_count
    ๋Š” [[3, 2, 0], [1, 1, 1]]์ด ๋‹ด๊น๋‹ˆ๋‹ค.

     

    3. ํ”ผ๋กœ๋„๋ฅผ ๋งŽ์ด ํ•„์š”๋กœํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ๊ด‘๋ฌผ์„ ์ •๋ ฌ

    mineral_count.sort(key=lambda x: (x[0], x[1], x[2]), reverse=True)

    ํ”ผ๋กœ๋„๋ฅผ ๋งŽ์ด ์†Œ๋ชจํ•˜๋Š” ๊ด‘๋ฌผ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. diamons → iron stone ์ˆœ์œผ๋กœ ํ”ผ๋กœ๋„๋ฅผ ๋งŽ์ด ์†Œ๋ชจํ•˜๋ฏ€๋กœ diamond๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ๋ฌถ์Œ, ๊ทธ๋ฆฌ๊ณ  iron์ด ๋งŽ์€ ๋ฌถ์Œ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. sort() ๋ฉ”์„œ๋“œ์˜ key ์ธ์ž์™€ reverse ์ธ์ž๋ฅผ ์ง€์ •ํ•ด์„œ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

     

    4. ํ”ผ๋กœ๋„ ๊ณ„์‚ฐ

    while mineral_count:
        mineral = mineral_count.pop(0)
        if picks[0] > 0:
            answer += mineral[0] * 1 + mineral[1] * 1 + mineral[2] * 1
            picks[0] -= 1
        elif picks[1] > 0:
            answer += mineral[0] * 5 + mineral[1] * 1 + mineral[2] * 1
            picks[1] -= 1
        else:
            answer += mineral[0] * 25 + mineral[1] * 5 + mineral[2] * 1
            picks[2] -= 1

    ์ •๋ ฌ๋œ ์ˆœ์„œ๋Œ€๋กœ ์ฐจ๋ก€๋กœ ๋‹ค์ด์•„๋ชฌ๋“œ ๊ณก๊ฐฑ์ด, ์ฒ  ๊ณก๊ฐฑ์ด, ๋Œ ๊ณก๊ฐฑ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์บ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ”ผ๋กœ๋„๋Š” ํ‘œ์—์„œ ์ค€ ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

     

    ๐ŸŒฑ ์ •๋‹ต ์ฝ”๋“œ

    def solution(picks, minerals):
        answer = 0
    
        # ์บ˜ ์ˆ˜ ์žˆ๋Š” ๊ด‘๋ฌผ์˜ ๊ฐœ์ˆ˜
        if len(minerals) <= sum(picks) * 5:
            n = len(minerals)
        else:
            n = sum(picks) * 5
    
        mineral_count = []
        tmp = []
        for idx in range(n):
            if idx % 5 == 0:  # 5๊ฐœ๋งˆ๋‹ค ์ดˆ๊ธฐํ™”
                if tmp:
                    mineral_count.append(tmp)
                tmp = [0, 0, 0]
    
            if minerals[idx] == "diamond":
                tmp[0] += 1
            elif minerals[idx] == "iron":
                tmp[1] += 1
            else:
                tmp[2] += 1
            if idx == n - 1:
                mineral_count.append(tmp)
                break
    
        mineral_count.sort(key=lambda x: (x[0], x[1], x[2]), reverse=True)
    
        while mineral_count:
            mineral = mineral_count.pop(0)
            if picks[0] > 0:
                answer += mineral[0] * 1 + mineral[1] * 1 + mineral[2] * 1
                picks[0] -= 1
            elif picks[1] > 0:
                answer += mineral[0] * 5 + mineral[1] * 1 + mineral[2] * 1
                picks[1] -= 1
            else:
                answer += mineral[0] * 25 + mineral[1] * 5 + mineral[2] * 1
                picks[2] -= 1
    
        return answer
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€