๐ŸŒฑ → ๐ŸŒณ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜์ƒ - python ๋ณธ๋ฌธ

Algorithms

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜์ƒ - python

BAY 2023. 9. 28. 14:52
728x90

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

 

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

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

programmers.co.kr

 

๋ฌธ์ œ ์ •๋ณด

๋ฌธ์ œ ์œ ํ˜• ๋‚œ์ด๋„ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„ ํ•ด๊ฒฐ ์œ ๋ฌด ์Šค์Šค๋กœ ๊ตฌํ˜„ ์„ฑ๊ณต
ํ•ด์‹ฑ level2 45m O X

์„ค๊ณ„ ๋ฐฉ๋ฒ•

1. dictionary์— “์˜ท์˜ ๋ถ„๋ฅ˜ : ์ข…๋ฅ˜ ๊ฐฏ์ˆ˜” ์ด๋ ‡๊ฒŒ ๋„ฃ์–ด์ฃผ๊ณ 

2. (์ข…๋ฅ˜+1) ์ „๋ถ€ ๊ณฑํ•œ ํ›„ -1 ํ•ด์ฃผ๋ฉด ๋จ

 

(์ข…๋ฅ˜ + 1) ๊ณฑํ•˜๊ณ  1 ๋นผ๋ฉด ๋˜๋Š” ์ด์œ : ex. (a + 1)(b + 1)(c + 1) - 1 = (a + b + c) + (ab + bc + ca) + abc

์ฝ”๋“œ

def solution(clothes):
    ans = 1
    dict = {}
    for i in clothes:
        if i[1] not in dict:
            dict[i[1]] = 0
        dict[i[1]]+=1

    for i in dict.values():
        ans=(i+1)*ans
    return ans-1

์‹œ๊ฐ„ ๋ณต์žก๋„

O(N)

์–ด๋ ค์› ๋˜ ์ 

for i in dict.values():
        ans=(i+1)*ans

์ด ๋ถ€๋ถ„์€ ํ’€์ด ๋ณด๊ธฐ ์ „๊นŒ์ง€๋„ ๋ชฐ๋ž์Œ

์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•ด๋‚ด์ง€

 

(์ข…๋ฅ˜ + 1) ๊ณฑํ•˜๊ณ  1 ๋นผ๋ฉด ๋˜๋Š” ์ด์œ : ex. (a + 1)(b + 1)(c + 1) - 1 = (a + b + c) + (ab + bc + ca) + abc

728x90