[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„์žฅ /ํŒŒ์ด์ฌ /Python /ํ•ด์‹œ
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„์žฅ /ํŒŒ์ด์ฌ /Python /ํ•ด์‹œ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’กsolutions )

โœ… clothes_dic์ด๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ ๋งŒ๋“ค์–ด์„œ ์ข…๋ฅ˜๋ณ„๋กœ ์˜์‚ฌ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ ๋‹จ, ์˜์ƒ์„ ์„ ํƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์œผ๋‹ˆ ๋งจ ์ฒ˜์Œ ๋”•์…”๋„ˆ๋ฆฌ์— value๊ฐ’์„ ๋„ฃ์„ ๋•Œ๋Š” +1 ํ‚ค์›Œ์„œ 2๋ฅผ ์ €์žฅ

โœ… ๋งˆ์ง€๋ง‰์—๋Š” ๋ชจ๋‘ ์•ˆ ์ž…์€ ๊ฒฝ์šฐ ํ•˜๋‚˜๋ฅผ ๋นผ์„œ return

๐ŸŽซcode )

def solution(clothes):
    clothes_dic = {}
    cnt = 1
    for i in clothes:
        tmp = i[1]
        if clothes_dic.get(tmp):    
            clothes_dic[tmp] += 1
        else:
            clothes_dic[tmp] = 2
    for i in clothes_dic.values():
        cnt *= i
    return cnt - 1

๐Ÿ“Œ description )

๋ฌธ์ œ์ถœ์ฒ˜ : https://programmers.co.kr/learn/courses/30/lessons/42578?language=python3

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์œ„์žฅ

 

programmers.co.kr

 

๋ฌธ์ œ ์„ค๋ช…

์ŠคํŒŒ์ด๋“ค์€ ๋งค์ผ ๋‹ค๋ฅธ ์˜ท์„ ์กฐํ•ฉํ•˜์—ฌ ์ž…์–ด ์ž์‹ ์„ ์œ„์žฅํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜ท์ด ์•„๋ž˜์™€ ๊ฐ™๊ณ  ์˜ค๋Š˜ ์ŠคํŒŒ์ด๊ฐ€ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ธด ์ฝ”ํŠธ, ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ ๋ฅผ ์ž…์—ˆ๋‹ค๋ฉด ๋‹ค์Œ๋‚ ์€ ์ฒญ๋ฐ”์ง€๋ฅผ ์ถ”๊ฐ€๋กœ ์ž…๊ฑฐ๋‚˜ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ ๋Œ€์‹  ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค๋ฅผ ์ฐฉ์šฉํ•˜๊ฑฐ๋‚˜ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ข…๋ฅ˜์ด๋ฆ„
์–ผ๊ตด ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค
์ƒ์˜ ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ 
ํ•˜์˜ ์ฒญ๋ฐ”์ง€
๊ฒ‰์˜ท ๊ธด ์ฝ”ํŠธ

์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ๋“ค์ด ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด clothes๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์„œ๋กœ ๋‹ค๋ฅธ ์˜ท์˜ ์กฐํ•ฉ์˜ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • clothes์˜ ๊ฐ ํ–‰์€ [์˜์ƒ์˜ ์ด๋ฆ„, ์˜์ƒ์˜ ์ข…๋ฅ˜]๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ์˜ ์ˆ˜๋Š” 1๊ฐœ ์ด์ƒ 30๊ฐœ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์˜์ƒ์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • clothes์˜ ๋ชจ๋“  ์›์†Œ๋Š” ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ด๊ณ  ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž ๋˜๋Š” '_' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŒŒ์ด๋Š” ํ•˜๋ฃจ์— ์ตœ์†Œ ํ•œ ๊ฐœ์˜ ์˜์ƒ์€ ์ž…์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

clothesreturn
[[yellow_hat, headgear], [blue_sunglasses, eyewear], [green_turban, headgear]] 5
[[crow_mask, face], [blue_sunglasses, face], [smoky_makeup, face]] 3

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์˜ˆ์ œ #1
headgear์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด yellow_hat, green_turban์ด๊ณ  eyewear์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด blue_sunglasses์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด 5๊ฐœ์˜ ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

1. yellow_hat 2. blue_sunglasses 3. green_turban 4. yellow_hat + blue_sunglasses 5. green_turban + blue_sunglasses

์˜ˆ์ œ #2
face์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด crow_mask, blue_sunglasses, smoky_makeup์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด 3๊ฐœ์˜ ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

1. crow_mask 2. blue_sunglasses 3. smoky_makeup

 

๋ฐ˜์‘ํ˜•