DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ด๋ชจํ‹ฐ์ฝ˜ ํ• ์ธํ–‰์‚ฌ (Python) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ด๋ชจํ‹ฐ์ฝ˜ ํ• ์ธํ–‰์‚ฌ (Python)

gimewn 2023. 2. 1. 23:00

link ๐Ÿ”— https://school.programmers.co.kr/learn/courses/30/lessons/150368

 

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

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

programmers.co.kr

 

๐Ÿ”ฅ ์œ ์ €๊ฐ€ ์ตœ๋Œ€ 100๋ช…, ์ด๋ชจํ‹ฐ์ฝ˜์ด ์ตœ๋Œ€ 7๊ฐœ๋กœ ๋ฒ”์œ„๊ฐ€ ๊ทธ๋ฆฌ ํฌ์ง€ ์•Š์•„ ์™„์ „ํƒ์ƒ‰์œผ๋กœ๋„ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์ง€ ์•Š์•˜๋‹ค.

๐Ÿ”ฅ ํ•˜์ง€๋งŒ ์‚ผ์ค‘for๋ฌธ์„ ํ™œ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ํ’€ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ...๐Ÿค”

 

from itertools import product

def solution(users, emoticons):
    answer = [0, 0]
    discount = [10, 20, 30, 40]
    emo_len = len(emoticons)
    # ์ค‘๋ณต ์ˆœ์—ด๋กœ ๊ฒฝ์šฐ์˜ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    pro_list = list(product(discount, repeat=len(emoticons)))
    
    for production in pro_list:
        # ์ด๋ชจํ‹ฐ์ฝ˜ ํ”Œ๋Ÿฌ์Šค ๊ฐ€์ž…์ž ์ˆ˜, ์ด ๋งค์ถœ
        emoticon_plus, sales = 0, 0
        for user in users:
            limit_rate, limit_money = user[0], user[1]
            spent = 0
            for idx in range(emo_len):
                # ํ• ์ธ์œจ์ด ์œ ์ € ์ง€์ • ํ• ์ธ์œจ๋ณด๋‹ค ํฌ๋ฉด ๊ตฌ๋งค
                if production[idx] >= limit_rate:
                    spent += int(emoticons[idx] - emoticons[idx]*(production[idx]/100))
            # ์ด ์‚ฌ์šฉํ•œ ๊ธˆ์•ก์ด ์œ ์ € ์ง€์ • ์ด์•ก ์ด์ƒ์ด๋ฉด
            if spent >= limit_money:
                # ์ด๋ชจํ‹ฐ์ฝ˜ ํ”Œ๋Ÿฌ์Šค ๊ฐ€์ž…
                emoticon_plus += 1
            else:
                # ์ด์•ก ๋ฏธ๋งŒ์ด๋ฉด ๋งค์ถœ์— ๋”ํ•จ
                sales += spent
                
        # ์ด๋ชจํ‹ฐ์ฝ˜ ํ”Œ๋Ÿฌ์Šค ๊ฐ€์ž…์ž๊ฐ€ ๊ธฐ์กด ์ตœ๋Œ€๊ฐ’๋ณด๋‹ค ๋” ๋งŽ์œผ๋ฉด
        if emoticon_plus > answer[0]:
            # ์ด๋ชจํ‹ฐ์ฝ˜ ํ”Œ๋Ÿฌ์Šค ๊ฐ€์ž…์ž, ์ด ๋งค์ถœ ๋ชจ๋‘ ๊ต์ฒด
            answer[0], answer[1] = emoticon_plus, sales
        # ์ด๋ชจํ‹ฐ์ฝ˜ ํ”Œ๋Ÿฌ์Šค ๊ฐ€์ž…์ž๊ฐ€ ๊ธฐ์กด ์ตœ๋Œ€๊ฐ’๊ณผ ๊ฐ™๊ณ , ๋งค์ถœ์ด ๋” ๋†’์œผ๋ฉด
        elif emoticon_plus == answer[0] and sales > answer[1]:
            # ์ด ๋งค์ถœ๋งŒ ๊ต์ฒด
            answer[1] = sales
                    
    return answer

 

product

- ํŒŒ์ด์ฌ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ itertools์— ์žˆ๋Š” ์ค‘๋ณต์ˆœ์—ด ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜

from itertools import permutations, combinations, product, combination_with_replacement

# ์ˆœ์—ด
permu_list = permutations(data, 5)

# ์กฐํ•ฉ
comb_list = combinations(data, 5)

# ์ค‘๋ณต์ˆœ์—ด
product_list = product(data, repeat=5)

# ์ค‘๋ณต ์กฐํ•ฉ
comb_rep_list = combinations_with_replacement(data, 5)