๋ฌธ์ ์ค๋ช
n๋ช ์ ๊ถํฌ์ ์๊ฐ ๊ถํฌ ๋ํ์ ์ฐธ์ฌํ๊ณ ๊ฐ๊ฐ 1๋ฒ๋ถํฐ n๋ฒ๊น์ง ๋ฒํธ๋ฅผ ๋ฐ์์ต๋๋ค. ๊ถํฌ ๊ฒฝ๊ธฐ๋ 1๋1 ๋ฐฉ์์ผ๋ก ์งํ์ด ๋๊ณ , ๋ง์ฝ A ์ ์๊ฐ B ์ ์๋ณด๋ค ์ค๋ ฅ์ด ์ข๋ค๋ฉด A ์ ์๋ B ์ ์๋ฅผ ํญ์ ์ด๊น๋๋ค. ์ฌํ์ ์ฃผ์ด์ง ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ง๊ณ ์ ์๋ค์ ์์๋ฅผ ๋งค๊ธฐ๋ ค ํฉ๋๋ค. ํ์ง๋ง ๋ช๋ช ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ๋ฅผ ๋ถ์คํ์ฌ ์ ํํ๊ฒ ์์๋ฅผ ๋งค๊ธธ ์ ์์ต๋๋ค.
์ ์์ ์ n, ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ๋ฅผ ๋ด์ 2์ฐจ์ ๋ฐฐ์ด results๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋ ์ ํํ๊ฒ ์์๋ฅผ ๋งค๊ธธ ์ ์๋ ์ ์์ ์๋ฅผ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ ์ฌํญ
- ์ ์์ ์๋ 1๋ช ์ด์ 100๋ช ์ดํ์ ๋๋ค.
- ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ๋ 1๊ฐ ์ด์ 4,500๊ฐ ์ดํ์ ๋๋ค.
- results ๋ฐฐ์ด ๊ฐ ํ [A, B]๋ A ์ ์๊ฐ B ์ ์๋ฅผ ์ด๊ฒผ๋ค๋ ์๋ฏธ์ ๋๋ค.
- ๋ชจ๋ ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ์๋ ๋ชจ์์ด ์์ต๋๋ค.
์ ์ถ๋ ฅ ์
n | results | return |
5 | [[4, 3], [4, 2], [3, 2], [1, 2], [2, 5]] | 2 |
์ ์ถ๋ ฅ ์ ์ค๋ช
2๋ฒ ์ ์๋ [1, 3, 4] ์ ์์๊ฒ ํจ๋ฐฐํ๊ณ 5๋ฒ ์ ์์๊ฒ ์น๋ฆฌํ๊ธฐ ๋๋ฌธ์ 4์์ ๋๋ค.
5๋ฒ ์ ์๋ 4์์ธ 2๋ฒ ์ ์์๊ฒ ํจ๋ฐฐํ๊ธฐ ๋๋ฌธ์ 5์์ ๋๋ค.
ํ์ด
- ๋ฉ์ฒญ์ด ๊ฐ์ ๋ฌด์ง์ฑ ์ฝ๋ฉ์ผ๋ก ์ธํด ์ฃผ๋ง์ด ๋ชจ๋ ์ฌ๋ผ์ก๋ค.
- ์ฒ์์ ํ๋ ฌ๋ก ๊ทธ๋ํ ๋ง๋ค์ด์ win/lose๋ฅผ ํ๋์ ๊ทธ๋ํ๋ก ํด๋ณด๋ ค๊ณ ํ๋๋ฐ
- ํ์ํ๋๋ฐ ์ ๋ฌดํ๋ฃจํ๊ฐ ๋์ง...? ใ ใ ใ
- ๋ญ์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง ํ ์ผ 4๋ฒ๋ง ํ๋ฆฌ๊ณ ๋์ ํ ์๋์
- ๊ทธ ๋ค์์ ๊ทธ๋ฅ ๋ฐฐ์ด์ ์์๋๋ก ๋ฃ์ผ๋ฉด์ ์ ๋ ฌํด์ ์์ ๋ง์ถฐ๋ณด๋ ค๊ณ ํ๋ค.
- 8,[3,4,5], 1, [2,6,7]
- ์ด๋ฐ ์์ผ๋ก ๋์ค๋ฉด 8๊ณผ 1๋ง ์์๋ฅผ ์ ์ ์๊ณ , ๋๋จธ์ง๋ ๋ชจ๋ฅด๋ ๊ฑธ๋ก
- ๊ทผ๋ฐ ์ด๊ฒ๋ ํ๋๋ณด๋ ๋ฌดํ๋ฃจํ๊ฐ ๋์ด ๋ฒ๋ฆฌ๋ ๊ฒ ๊ฐ๊ณ ...
- ๊ฒฐ๊ตญ ํ๋ ํ๋ ๋ฏ์ด์ ๊ทธ๋ฆฌ๋ฉด์ ํ์ค ํ์ค ใ ก.ใ ก;;;
def solution(n, results):
answer = 0
wingraph = [[0 for _ in range(n+1)] for _ in range(n+1)]
losegraph = [[0 for _ in range(n+1)] for _ in range(n+1)]
sumgraph = [[0 for _ in range(n+1)] for _ in range(n+1)]
for win,lose in results:
wingraph[win][lose] += 1
losegraph[lose][win] += 1
for i in range(1,n+1):
for j in range(1,n+1):
if wingraph[i][j] == 1:
for k in range(1,n+1):
if wingraph[k][i] == 1:
wingraph[k][j] = 1
if losegraph[i][j] == 1:
for k in range(1,n+1):
if losegraph[k][i] == 1:
losegraph[k][j] = 1
# print("wingraph")
# for i in wingraph: print(i)
# print("wingraph")
# for i in losegraph: print(i)
for i in range(1,n+1):
for j in range(1,n+1):
sumgraph[i][j] = wingraph[i][j] + losegraph[i][j]
if sum(sumgraph[i]) == n-1:
answer += 1
#print("sumgraph")
#for i in sumgraph: print(i)
return answer
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.05ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.08ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.21ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.19ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (2.08ms, 10.2MB)
ํ
์คํธ 6 ใ ํต๊ณผ (4.38ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (22.46ms, 10.4MB)
ํ
์คํธ 8 ใ ํต๊ณผ (57.77ms, 10.6MB)
ํ
์คํธ 9 ใ ํต๊ณผ (68.58ms, 10.5MB)
ํ
์คํธ 10 ใ ํต๊ณผ (110.54ms, 10.7MB)
- ์ ์ ๋์ด๋๊ฐ ์ฌ๋ผ๊ฐ๋ฉด์ ์ ๋๋ก ๋ชป ํธ๋ ๊ฒ ๊ฐ๋ค.
- ๊ณ ์์ ํ์ด๋ฅผ ๋ณด๋๋ก ํ์.
- ์ ์ฉ๋ค. ์ด๊ฒ ๋ญ์ง??
- ์ ์ด๋ ๊ฒ ํ๋๊ฑฐ๊ตฌ๋... ใ ก.ใ ก
- ์ ์ฉ๋ค. ์ด๊ฒ ๋ญ์ง??
from collections import defaultdict
def solution(n, results):
answer = 0
win, lose = defaultdict(set), defaultdict(set)
for result in results:
lose[result[1]].add(result[0])
win[result[0]].add(result[1])
for i in range(1, n + 1):
for winner in lose[i]: win[winner].update(win[i])
for loser in win[i]: lose[loser].update(lose[i])
for i in range(1, n+1):
if len(win[i]) + len(lose[i]) == n - 1: answer += 1
return answer
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.05ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.29ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.57ms, 10.5MB)
ํ
์คํธ 7 ใ ํต๊ณผ (1.83ms, 10.6MB)
ํ
์คํธ 8 ใ ํต๊ณผ (3.64ms, 10.8MB)
ํ
์คํธ 9 ใ ํต๊ณผ (4.86ms, 11.2MB)
ํ
์คํธ 10 ใ ํต๊ณผ (5.10ms, 11.4MB)
- ๊น๋ํ๋ค.
- ๋๋ ์ด๋ ๊ฒ ํด๋ณผ๊ฑธ...
def solution(n, results):
total = [['?' for i in range(n)] for j in range(n)]
for i in range(n):
total[i][i] = 'SELF'
for result in results:
total[result[0]-1][result[1]-1] = 'WIN'
total[result[1]-1][result[0]-1] = 'LOSE'
for k in range(n):
for i in range(n):
for j in range(n):
if total[i][k] == 'WIN' and total[k][j] == 'WIN':
total[i][j] = 'WIN'
elif total[i][k] == 'LOSE' and total[k][j] == 'LOSE':
total[i][j] = 'LOSE'
answer = 0
for i in total:
if '?' not in i:
answer += 1
return answer
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.06ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.18ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.98ms, 10.4MB)
ํ
์คํธ 5 ใ ํต๊ณผ (4.18ms, 10.4MB)
ํ
์คํธ 6 ใ ํต๊ณผ (9.66ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (51.06ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (111.27ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (151.06ms, 10.7MB)
ํ
์คํธ 10 ใ ํต๊ณผ (152.25ms, 10.6MB)