๐Ÿ’ป Coding/[Algorithm]Python

์ด์ฝ”ํ…Œ | ๋ณผ๋ง๊ณต ๊ณ ๋ฅด๊ธฐ

๐Ÿฉท ๋ฏผ์˜ 2024. 1. 31. 16:35

๐Ÿ’ป์ด์ฝ”ํ…Œ | ๋ณผ๋ง๊ณต ๊ณ ๋ฅด๊ธฐ

 

** ๊ต์žฌ์— ์ฒจ๋ถ€๋œ ๋ฌธ์ œ๋ผ ๋ฌธ์ œ๋Š” ์ž‘์„ฑํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค

 

์‰ฌ์šด ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ์˜€์ง€๋งŒ, ๋‚ด ํ’€์ด์™€ ๊ต์žฌ์˜ ํ’€์ด๊ฐ€ ๋‹ฌ๋ผ์„œ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ตฌ ํ•œ๋‹ค!

 

โœ๏ธ ๋‚ด ํ’€์ด

N, M = map(int, input().split())
data = list(map(int, input().split()))

result = 0

for i in range(N):
    for j in range(i+1, N):
        if data[i] != data[j]:
            result += 1

print(result)

 

๋‚œ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ˜๋ณต์„ ํ•˜๋ฉฐ, ์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด j์˜ ๋ฒ”์œ„๋ฅผ i+1 ๋ถ€ํ„ฐ N ๊นŒ์ง€ ์žก์•„์ฃผ๊ณ  ๊ฐ™์€ ๋ฌด๊ฒŒ๊ฐ€ ์•„๋‹ ๋•Œ, result ๊ฐ’์— +1 ํ•ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ“™๊ต์žฌ ํ’€์ด

n,m = map(int, input().split())
data = list(map(int, input().split()))

array = [0] * 11
for x in data:
    array[x] += 1

result = 0
for i in range(1, m-1):
    n -= array[i]
    result += array[i] * n

print(result)

 

๊ต์žฌ์˜ ๊ฒฝ์šฐ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ƒ๊ฐํ•˜์˜€๋‹ค.

๋จผ์ € ๋ฌด๊ฒŒ๋งˆ๋‹ค ๋ถˆ๋ง๊ณต์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€๋ฅผ ๊ณ„์‚ฐํ•œ ํ›„, A๊ฐ€ ํŠน์ •ํ•œ ๋ฌด๊ฐœ์˜ ๋ณผ๋ง๊ณต์„ ์„ ํƒํ–ˆ์„ ๋•Œ, ์ด์–ด์„œ B๊ฐ€ ๋ณผ๋ง๊ณต์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๊ณ„์‚ฐ์„ ํ•˜์˜€๋‹ค. 

 


๐Ÿค” ๋‚ด์ƒ๊ฐ

๋‚ด ํ’€์ด๊ฐ€ ํ›จ์”ฌ ์ง๊ด€์ ์ด๊ณ  ๊ฐ„๋‹จํ•œ ๊ฒƒ ๊ฐ™์€ ๋Š๋‚Œ์ด ๋“ค์ง€๋งŒ, ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋”ฐ์ ธ๋ณด๋ฉด ๊ต์žฌ์˜ ํ’€์ด๊ฐ€ ๋” ์ž‘์œผ๋‹ˆ๊นŒ ๊ต์žฌ์˜ ์‹์œผ๋กœ๋„ ํ’€์–ด๋ณด๊ณ  ์ดํ•ดํ•ด๋ณด์•˜๋‹ค!