๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

โฌ› [Programmers] [Python] [Level2] ํ• ์ธ ํ–‰์‚ฌ

Dbswnstjd 2023. 4. 10. 01:00

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/131127

 

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

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

programmers.co.kr

ํ’€์ด

# ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๋‹จ๊ณ„ - ํ• ์ธ ํ–‰์‚ฌ
from collections import Counter
def solution(want, number, discount):
    answer = 0
    d = dict()

    for i in range(len(want)):
        d[want[i]] = number[i]
    
    #num = sum(number)
    # ์–ด์ฐจํ”ผ ์ตœ๋Œ€ 10์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ๊ตฌํ• ํ•„์š”์—†์ด 10์œผ๋กœ ๋‘ฌ๋„ ๋œ๋‹ค. 
    for i in range(len(discount) - 9): # ์ธ๋ฑ์Šค ๋ฒ”์œ„ ์กฐ์‹ฌ
        if d == Counter(discount[i:i+10]):
            answer += 1
    return answer
print(solution(["banana", "apple", "rice", "pork", "pot"],[3, 2, 2, 2, 1],
["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"]))

Counter ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ. 

์ธ๋ฑ์Šค ์ž๋ฅผ ๋•Œ ๋ฒ”์œ„๋งŒ ์กฐ์‹ฌํ•˜๋ฉด ๋œ๋‹ค.