Algorithm๐Ÿงถ | ์œ„์žฅ(Hash)

saneeeeeeee_Yaยท2021๋…„ 3์›” 22์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
7/11
post-thumbnail

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

๋ฌธ์ œ ์„ค๋ช…

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

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

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

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

์ œํ•œ์‚ฌํ•ญ

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

์ž…์ถœ๋ ฅ ์˜ˆ

clothesreturn
[["yellowhat", "headgear"], ["bluesunglasses", "eyewear"], ["green_turban", "headgear"]]5
[["crowmask", "face"], ["bluesunglasses", "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

๋ฌธ์ œ ํ’€์ด

์ฒซ๋ฒˆ์งธ ํ’€์ด๐Ÿ˜

def solution(clothes):
    a=0
    two = []
    one = []
    while True:
        if a == len(clothes):
                break
        for i in range(len(clothes)):
            one.append(f"{clothes[i][0]}")
            if i != a and i > a:
                if clothes[a][1] != clothes[i][1]:
                    two.append((clothes[a][0],clothes[i][0])) 
            if i == len(clothes)-1:
                    a += 1
    two = list(set(two))
    one = list(set(one))
    return len(two) + len(one)

์˜์ƒ์˜ ์ข…๋ฅ˜๊ฐ€ 2๊ฐ€์ง€๊นŒ์ง€์˜ ๋ฐฐ์—ด๋งŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค
์‹คํŒจ๐Ÿคช

๋‘๋ฒˆ์งธ ํ’€์ด

def solution(clothes):
    kind_dic={}
    for name, kind in clothes: #dictionary๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ์ข…๋ฅ˜์˜ ๊ฐฏ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ ๋’ค 
        if kind in kind_dic:
            kind_dic[kind] += 1
        else:
            kind_dic[kind] = 1
    count = 1
    for i in kind_dic.values(): #dictionary์˜ value๋ฅผ ๊ฐ€์ ธ์™€ 1์„ ๋”ํ•œ ์ˆ˜๋ฅผ ๊ณฑํ•ด์ค€๋‹ค (1์„ ๋”ํ•œ ์ด์œ ? 1์„ ๋”ํ•˜์ง€ ์•Š์œผ๋ฉด ํ•ด๋‹น๋˜๋Š” ์˜์ƒ์˜ ์ข…๋ฅ˜๋ฅผ ์ž…์ง€์•Š์„ ๊ฒฝ์šฐ๋ฅผ ๋‚˜ํƒ€๋‚ด์ง€ ๋ชปํ•œ๋‹ค)
        count *= i+1
    return count -1 #์•„๋ฌด๊ฒƒ๋„ ์•ˆ ์ž…์„ ๊ฒฝ์šฐ๋Š” ์ œํ•œ๋‹ค

์„ฑ๊ณต๐Ÿ‘

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด 1

import collections
from functools import reduce

def solution(c):
    return reduce(lambda x,y:x*y,[a+1 for a in collections.Counter([x[1] for x in c]).values()])-1

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด 2

def solution(clothes):
    from collections import Counter
    from functools import reduce
    cnt = Counter([kind for name, kind in clothes])
    answer = reduce(lambda x, y: x*(y+1), cnt.values(), 1) - 1
    return answer
profile
๐Ÿœhttps://action2thefuture.github.io/๐Ÿœ

0๊ฐœ์˜ ๋Œ“๊ธ€