๐Ÿ’ป Coding/[Algorithm]Python

์†Œํ”„ํ‹ฐ์–ด | ์šฐ๋ฌผ ์•ˆ ๊ฐœ๊ตฌ๋ฆฌ โญโญโญ - PYTHON

๐Ÿฉท ๋ฏผ์˜ 2024. 3. 22. 00:18

์†Œํ”„ํ‹ฐ์–ด | ์šฐ๋ฌผ ์•ˆ ๊ฐœ๊ตฌ๋ฆฌ โญโญโญ - PYTHON

 

๋ฌธ์ œ

https://softeer.ai/practice/6289

 

Softeer - ํ˜„๋Œ€์ž๋™์ฐจ๊ทธ๋ฃน SW์ธ์žฌํ™•๋ณดํ”Œ๋žซํผ

 

softeer.ai

 

 

๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

๊ฐ€์žฅ ๋จผ์ € ์ฒซ ๋ฒˆ์งธ ์ฃผ์–ด์ง„ ๊ฐ’(f)์™€ ๋‘ ๋ฒˆ์งธ ์ฃผ์–ด์ง„ ๊ฐ’(s) ์„ ์ธ๋ฑ์Šค-1 ๊ฐ’์œผ๋กœ ์ƒ๊ฐํ•˜์—ฌ

f๊ฐ€ ํด ๋•Œ, s๊ฐ€ ํด ๋•Œ, f์™€  s๊ฐ€ ๊ฐ™์„ ๋•Œ ์„ธ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆ„์–ด ๊ณ ๋ฏผํ•˜์˜€๋‹ค.

 

์ด ํ›„, 1๋กœ ์ฑ„์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์œ„ ์„ธ ๊ฒฝ์šฐ์ผ ๋•Œ 0์œผ๋กœ ๋ณ€ํ™˜์‹œ์ผฐ๊ณ , 1์ธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์˜€๋‹ค.

 

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

 

์ฒซ ํ’€์ด

import sys
input = sys.stdin.readline

N, M = map(int, input().split())
members = list(map(int, input().split()))
data = [list(map(int, input().split())) for _ in range(M)]

cnt_list = [1]*N
result = 0

for i in range(M):
    if members[data[i][0] -1] < members[data[i][1] -1]:
        cnt_list[data[i][0] -1] = 0
    elif members[data[i][0] -1] == members[data[i][1] -1]:
        cnt_list[data[i][0]-1] = 0
        cnt_list[data[i][1]-1] = 0
    elif members[data[i][0] -1] > members[data[i][1] -1]:       
        cnt_list[data[i][1]-1] = 0

for i in range(N):
    if cnt_list[i] > 0:
        result += 1

print(result)

 

์ œ์ถœ์„ ํ•˜๊ณ  ๋‚˜๋‹ˆ ์ฝ”๋“œ๊ฐ€ ๋งˆ์Œ์— ๋“ค์ง€ ์•Š์•˜๋‹ค

 

โญ ๋ณด์™„ํ•œ ์ 

โœ… ์ง€์ €๋ถ„ ํ•œ ์ฝ”๋“œ ์ •๋ฆฌ

โœ… ๊ตณ์ด result ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ , ๋ฆฌ์ŠคํŠธ์—์„œ count ํ•˜๊ธฐ

โœ… data ๊ฐ’์„ ๋ฏธ๋ฆฌ ๋ฐ›์ง€ ์•Š๊ณ  ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ ๊ฐ๊ฐ input ํ•˜๊ธฐ

 

 

์ตœ์ข… ํ’€์ด

import sys
input = sys.stdin.readline

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

# ์ตœ๊ณ  ํŒ๋‹จ ๋ฆฌ์ŠคํŠธ (1์ด๋ฉด ์ตœ๊ณ )
cnt_list = [1] * N

for i in range(M):
    # f, s : ๋น„๊ตํ•  ์ธ๋ฑ์Šค + 1
    f, s = map(int, input().split())

    if members[f-1] < members[s-1]:
        cnt_list[f-1] = 0
    elif members[f-1] > members[s-1]:
        cnt_list[s-1] = 0
    elif members[f-1] == members[s-1]:
        cnt_list[f-1] = 0
        cnt_list[s-1] = 0

print(cnt_list.count(1))