๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆœ์œ„ (์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณ ๋“์  Kit, ๊ทธ๋ž˜ํ”„)

๐ŸŽฎinspirer9 2023. 3. 6. 01:36
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

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)
728x90
๋ฐ˜์‘ํ˜•