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

The only one you can truly trust is yourself.

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

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

๐ŸŽฎinspirer9 2023. 3. 4. 22:05
728x90
๋ฐ˜์‘ํ˜•

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณ ๋“์  Kit์— ์žˆ๋Š” ์ฒซ๋ฒˆ์งธ ๋ฌธ์ œ!

์ƒ๊ฐ๋ณด๋‹ค ์–ด๋ ต๋‹ค. ํŠนํžˆ ๋‚˜์ฒ˜๋Ÿผ ๋ฐฐ์—ด๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์Šต๊ด€์ด ์žˆ์œผ๋ฉด ใ…Žใ…Žใ…Ž

๋ฌธ์ œ ์„ค๋ช…

n๊ฐœ์˜ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋…ธ๋“œ๋Š” 1๋ถ€ํ„ฐ n๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ์ ํ˜€์žˆ์Šต๋‹ˆ๋‹ค. 1๋ฒˆ ๋…ธ๋“œ์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ๋…ธ๋“œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ๋…ธ๋“œ๋ž€ ์ตœ๋‹จ๊ฒฝ๋กœ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ๋…ธ๋“œ๋“ค์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ n, ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด vertex๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, 1๋ฒˆ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ๋…ธ๋“œ๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ n์€ 2 ์ด์ƒ 20,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ„์„ ์€ ์–‘๋ฐฉํ–ฅ์ด๋ฉฐ ์ด 1๊ฐœ ์ด์ƒ 50,000๊ฐœ ์ดํ•˜์˜ ๊ฐ„์„ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • vertex ๋ฐฐ์—ด ๊ฐ ํ–‰ [a, b]๋Š” a๋ฒˆ ๋…ธ๋“œ์™€ b๋ฒˆ ๋…ธ๋“œ ์‚ฌ์ด์— ๊ฐ„์„ ์ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

n vertex return
6 [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] 3

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์˜ˆ์ œ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๊ณ , 1๋ฒˆ ๋…ธ๋“œ์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ๋…ธ๋“œ๋Š” 4,5,6๋ฒˆ ๋…ธ๋“œ์ž…๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ฒ˜์Œ์— ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ํ•˜๋‹ค๊ฐ€ ๋ญ”๊ฐ€ ์ด์ƒํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋Š๋‚Œ ใ…‹ใ…‹
  • ์—ฌ๊ธฐ๊นŒ์ง€ ์งฐ๋˜ ์ฝ”๋“œ๊ฐ€ ํ…Œ์ผ€3๋ฒˆ์—์„œ 300ms ๊ฑธ๋ฆฌ๋ฉด์„œ ์‹คํŒจํ•จ.

  • ์ผ๋‹จ edge๋ฅผ for๋ฌธ์œผ๋กœ ํ•œ๋ฒˆ ๋Œ๊ณ , for๋ฌธ ์•ˆ์— ์˜๋ฏธ ์—†์ด 1000๋ฒˆ ๋„๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณธ ๊ฒฐ๊ณผ.
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ˆ๋  ๊ฐ ใ…‹ใ…‹ใ…‹
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (0.66ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (1.10ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	์‹คํŒจ (3.33ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (27.39ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (117.88ms, 10.5MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (282.72ms, 10.5MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	์‹คํŒจ (1892.57ms, 15.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (2736.92ms, 17.6MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (2885.05ms, 17.7MB)
  • ํ–‰๋ ฌ ๋ง๊ณ  ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ์จ์•ผ ํ•จ.
    • ์ „์— ๋ณด๋‹ˆ๊นŒ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์‹œ์ž‘๋…ธ๋“œ - ์—ฐ๊ฒฐ๋…ธ๋“œ ๋งŒ๋“ค์–ด์„œ ๊ตฌํ˜„ํ–ˆ๋˜ ๊ฑฐ ๋ณธ ๊ฑฐ ๊ธฐ์–ต๋‚ฌ๋‹ค.
    • ์–‘๋ฐฉํ–ฅ์ด๋‹ˆ๊นŒ 2๊ฐœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๊ณ , ๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์— set์ด ๋˜๋˜๊ฐ€?? ๊ทธ๋ƒฅ 2์ค‘ ๋ฐฐ์—ด...
    • ๊ทธ๋ฆฌ๊ณ  ๋””ํ์™€ ์นด์šดํ„ฐ...
import collections

def solution(n, edge):
    answer = 0
    node = [0 for _ in range(n+1)] # ์ตœ๋‹จ๊ฒฝ๋กœ ์ฒดํฌ
    graph = [[] for _ in range(n+1) ]
    
    for s,e in edge: # ์–‘๋ฐฉํ–ฅ ๋…ธ๋“œ
        graph[s].append(e)
        graph[e].append(s)
    
    queue = collections.deque()
    queue.append(1)
    
    while queue:
        q = queue.popleft()
        gr = graph[q]
        for g in gr:
            if node[g] == 0:
                node[g] = node[q] + 1
                queue.append(g)
    
    node[1] = 1
    answer = max(node)
    cnt_node = collections.Counter(node)
    
    return cnt_node[answer]
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.05ms, 10MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.65ms, 10.5MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (1.84ms, 10.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (20.04ms, 16.9MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (38.01ms, 20.8MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (46.69ms, 19.8MB)
  • ๊ณ ์ˆ˜์˜ ์ฝ”๋“œ
    • ์ฝœ๋ ‰์…˜ ๊ฐ™์€ ๊ฑฐ ์•ˆ์“ฐ๊ณ  ํ•˜๋Š” ๋ฐฉ๋ฒ•...
def solution(n, edge):
    graph =[  [] for _ in range(n + 1) ]
    distances = [ 0 for _ in range(n) ]
    is_visit = [False for _ in range(n)]
    queue = [0]
    is_visit[0] = True
    for (a, b) in edge:
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)

    while queue:
        i = queue.pop(0)

        for j in graph[i]:
            if is_visit[j] == False:
                is_visit[j] = True
                queue.append(j)
                distances[j] = distances[i] + 1

    distances.sort(reverse=True)
    answer = distances.count(distances[0])

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.87ms, 10.6MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (2.49ms, 11MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (29.41ms, 18.9MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (40.27ms, 23.6MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (56.52ms, 23.3MB)

์ƒ๊ฐ๋ณด๋‹ค ์–ด๋ ต๋‹ค.

728x90
๋ฐ˜์‘ํ˜•