๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 1453 - ํ”ผ์‹œ๋ฐฉ ์•Œ๋ฐ”

by YWTechIT 2021. 4. 28.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 1453 - ํ”ผ์‹œ๋ฐฉ ์•Œ๋ฐ”

๋ฐฑ์ค€ 1453 - ํ”ผ์‹œ๋ฐฉ ์•Œ๋ฐ”


โšก๏ธ ๋‚˜์˜ ํ’€์ด

defaultdict(int)๋ฅผ ์„ ์–ธํ•˜๊ณ  arr์˜ ์ธ๋ฑ์Šค๋“ค์„ ํ•˜๋‚˜์”ฉ ๋”ํ•ด์คฌ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์— lambda x: x-1์„ ์‚ฌ์šฉํ•ด์„œ ์ „์ฒด 1์”ฉ ๋นผ์ฃผ๊ณ  sum์„ ์‚ฌ์šฉํ–ˆ๋‹ค.


๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ๊นŒ ์ด๋ ‡๊ฒŒ ์–ด๋ ต๊ฒŒ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์ „์ฒด ๋ฒ”์œ„๋ฅผ False์ฒ˜๋ฆฌ ํ•ด๋‘๊ณ  ํ•ด๋‹น index๊ฐ€ ๋“ค์–ด์˜ค๋ฉด True์ฒ˜๋ฆฌ, ์ดํ›„์—๋„ ๋˜ ๋“ค์–ด์˜ค๋ฉด cnt+=1์„ ํ•ด์คฌ๋‹ค.

 

์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์‰ฝ๊ฒŒ ํ’€์ž!

# ๋‚˜์˜ ํ’€์ด
import sys
from collections import defaultdict
input = sys.stdin.readline

n = int(input())
arr = list(map(int, input().split()))
computer = defaultdict(int)

for i in range(len(arr)):
    computer[arr[i]] += 1

print(sum(map(lambda x: x-1, computer.values())))

# ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด
n = int(input())
arr = list(map(int, input().split()))
check = [False] * 101

cnt = 0
for i in arr:
    if check[i]:
        cnt +=1
    else:
        check[i] = True
print(cnt)
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€