ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

์ฒ˜์Œ์— ์ƒ๊ฐํ–ˆ์„ ๋•Œ minํ•จ์ˆ˜๋ฅผ ์จ์„œ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์•„์„œ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์„ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ทผ๋ฐ ์ด๋ ‡๊ฒŒํ•˜๋ฉด ๋น„๊ตํ•ด์•ผํ•  ๊ฒƒ๋„ ๋งŽ๊ณ  ์–ด๋–ค ์ ์ด ์ตœ์†Œ๊ฐ’์ด ๋˜๋Š”์ง€ ๋งค๋ฒˆ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งž๋Š” ๋ฐฉ๋ฒ•์ด ์•„๋‹Œ๊ฒƒ๊ฐ™๋‹ค. ๋ ˆ๋ฒจ 1๋ฌธ์ œ์ธ๋ฐ๋„ ํ’€์ด ๋ฐฉ๋ฒ•์ด ๋– ์˜ค๋ฅด์ง€ ์•Š์•˜๋Š”๋ฐ ๊ฐ๊ฐ์˜ ์ˆซ์ž๊ฐ€ ์ „๋ถ€ ์ง์ˆ˜๊ฐ€ ๋˜์–ด์•ผ ์ ์ด ๋„ค๊ฐœ๊ฐ€ ๋‹ค ์žˆ๋Š”๊ฒƒ์ด ๋˜๋‹ˆ๊นŒ ํ™€์ˆ˜๊ฐœ์ธ ์ˆซ์ž๋ฅผ ๋ฝ‘์•„์„œ ๋ฆฌํ„ดํ•˜๋ฉด ๋‚˜๋จธ์ง€ ํ•œ ์ ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

์ €๋ฒˆ ์Šคํ„ฐ๋””๋•Œ ์•Œ๊ฒŒ๋œ collection ๋ชจ๋“ˆ์˜ counter๋ฅผ ์ด์šฉํ•ด์„œ ์ˆซ์ž๋ฅผ ๋ฝ‘์•„๋‚ด๋ณด๊ฒ ๋‹ค!

import collections

def solution(v):
    answer = []
    
    # 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ 1์ฐจ์›์œผ๋กœ ๋งŒ๋“ค๊ธฐ
    v = sum(v, [])
    
    # ๋ฆฌ์ŠคํŠธ์•ˆ์˜ ๊ฐ’๋“ค ์ˆซ์ž์„ธ๊ธฐ
    v = collections.Counter(v)
    
    # ํ™€์ˆ˜๊ฐœ์ธ ๊ฒฝ์šฐ answer์— ๋‹ด๊ธฐ
    for key, value in v.items():
        if value % 2 != 0:
            answer.append(key)
    
    return answer

์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋˜๋ฉด ํ™€์ˆ˜๊ฐœ์ธ ์ ์˜ ์œ„์น˜๊ฐ€ x๊ฐ’์ธ์ง€ y๊ฐ’์ธ์ง€ ์•Œ์ˆ˜๊ฐ€ ์—†๋‹ค.

 

 

import collections

def solution(v):
    answer = []
    xs = []
    ys = []
    
    # x ์ขŒํ‘œ์™€ y์ขŒํ‘œ์˜ ๋ถ„๋ฆฌ
    for x, y in v:
        xs.append(x)
        ys.append(y)
        
    # ์ˆซ์ž ์„ธ๊ธฐ
    xs = collections.Counter(xs)
    ys = collections.Counter(ys)
    
    # ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ answer์— ๋‹ด๊ธฐ
    for key, value in xs.items():
        if value % 2 != 0:
            answer.append(key)
    for key, value in ys.items():
        if value % 2 != 0:
            answer.append(key)

    return answer

x์ขŒํ‘œ๋ž‘ y์ขŒํ‘œ๋ฅผ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ํ™•์ธํ•ด๋ณด๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊ฟจ๋‹ค. ๋ญ”๊ฐ€ ๋” ๊ธธ์–ด์ง€๊ณ  ๋ณต์žกํ•ด๋ณด์ด๊ธดํ•˜๋Š”๋ฐ ํ†ต๊ณผํ•˜๊ธดํ•จ!

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€