Algorithms/Programmers

ํ•ด์‹œ - lv2 ์œ„์žฅ

ํƒฑ์ ค 2021. 2. 25. 14:18

์ถœ์ฒ˜: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ์—ฐ์Šต, https://programmers.co.kr/learn/challenges

 

๋ฌธ์ œ ์„ค๋ช…

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

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

 

์ข…๋ฅ˜, ์ด๋ฆ„

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

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

 

์ œํ•œ์‚ฌํ•ญ

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

์ž…์ถœ๋ ฅ ์˜ˆ

clothes, return

[[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


import collections
def solution(clothes):
    answer = 1
    hash_cloth = collections.Counter([i[1] for i in clothes])
    for i in hash_cloth:
        answer *= (hash_cloth[i]+1)
    return answer - 1



########### ๋‹ค๋ฅธ ํ’€์ด
# 2์ฐจ์› ๋ฆฌ์ŠคํŠธ ํ•ด์‹œ๋กœ ๋งŒ๋“ค๊ธฐ
def solution(clothes):
    answer = 1
    
    d = {}
    for val, key in clothes:
        # key๊ฐ€ ๊ฐ™์œผ๋ฉด val ์ถ”๊ฐ€
        if key in d.keys():
            d[key].append(val)
        # key์— val ํ• ๋‹น
        else:
            d[key] = [val]
    
    for val in d.values():
        # ์กฐํ•ฉ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด +1 ํ•ด์ค€ ๊ฐ’ ๊ณฑํ•˜๊ธฐ
        answer *= (len(val)+1)
    
    # ๋งˆ์ง€๋ง‰์— ์•„๋ฌด๊ฒƒ๋„ ์ฐฉ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์˜ ์ˆ˜ ๋นผ์ฃผ๊ธฐ
    return answer - 1

# collections.Counter() ์ด์šฉ
from collections import Counter

def solution(clothes):
    answer = 1
    # ์ด์ฐจ์›๋ฆฌ์ŠคํŠธ clothes์—์„œ clothes[:][0]์€ value
    # cloth[1] ํ˜•ํƒœ --> ['yellow_hat', 'headgear'][1]
    kind = Counter([cloth[1] for cloth in clothes])
    # {'headgear': 2, 'eyewear': 1}

    for i in kind:
           answer *= (kind[i] + 1)
           
    answer -= 1
    
    return answer

- counter ๋ชจ๋“ˆ ์ด์šฉํ•˜๊ธฐ

- ์•„๋‹ˆ๋ฉด dict ์ž๋ฃŒํ˜• ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• ์ต์ˆ™ํ•ด์ง€๊ธฐ

- 2์ฐจ์› ๋ฆฌ์ŠคํŠธ ํ•ด์‹œ๋กœ ๋งŒ๋“ค๊ธฐ!!!

- keys(), values() ์ž˜ ์จ๋ณด๊ธฐ

728x90