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

The only one you can truly trust is yourself.

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์„ฌ ์—ฐ๊ฒฐํ•˜๊ธฐ

๐ŸŽฎinspirer9 2023. 2. 27. 17:31
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

n๊ฐœ์˜ ์„ฌ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๋ฅผ ๊ฑด์„คํ•˜๋Š” ๋น„์šฉ(costs)์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ์†Œ์˜ ๋น„์šฉ์œผ๋กœ ๋ชจ๋“  ์„ฌ์ด ์„œ๋กœ ํ†ตํ–‰ ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ์ตœ์†Œ ๋น„์šฉ์„ return ํ•˜๋„๋ก solution์„ ์™„์„ฑํ•˜์„ธ์š”.

๋‹ค๋ฆฌ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ฑด๋„ˆ๋”๋ผ๋„, ๋„๋‹ฌํ•  ์ˆ˜๋งŒ ์žˆ์œผ๋ฉด ํ†ตํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋ด…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด A ์„ฌ๊ณผ B ์„ฌ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๊ณ , B ์„ฌ๊ณผ C ์„ฌ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๊ฐ€ ์žˆ์œผ๋ฉด A ์„ฌ๊ณผ C ์„ฌ์€ ์„œ๋กœ ํ†ตํ–‰ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • ์„ฌ์˜ ๊ฐœ์ˆ˜ n์€ 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • costs์˜ ๊ธธ์ด๋Š” ((n-1) * n) / 2์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ž„์˜์˜ i์— ๋Œ€ํ•ด, costs[i][0] ์™€ costs[i] [1]์—๋Š” ๋‹ค๋ฆฌ๊ฐ€ ์—ฐ๊ฒฐ๋˜๋Š” ๋‘ ์„ฌ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋“ค์–ด์žˆ๊ณ , costs[i] [2]์—๋Š” ์ด ๋‘ ์„ฌ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋‹ค๋ฆฌ๋ฅผ ๊ฑด์„คํ•  ๋•Œ ๋“œ๋Š” ๋น„์šฉ์ž…๋‹ˆ๋‹ค.
  • ๊ฐ™์€ ์—ฐ๊ฒฐ์€ ๋‘ ๋ฒˆ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋”๋ผ๋„ ๊ฐ™์€ ์—ฐ๊ฒฐ๋กœ ๋ด…๋‹ˆ๋‹ค. ์ฆ‰ 0๊ณผ 1 ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋น„์šฉ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, 1๊ณผ 0์˜ ๋น„์šฉ์ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์„ฌ ์‚ฌ์ด์˜ ๋‹ค๋ฆฌ ๊ฑด์„ค ๋น„์šฉ์ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋‘ ์„ฌ ์‚ฌ์ด์˜ ๊ฑด์„ค์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ๋ด…๋‹ˆ๋‹ค.
  • ์—ฐ๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ์„ฌ์€ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

n costs return
4 [[0,1,1],[0,2,2],[1,2,5],[1,3,1],[2,3,8]] 4

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

costs๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ์ด๋•Œ ์ดˆ๋ก์ƒ‰ ๊ฒฝ๋กœ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ ์€ ๋น„์šฉ์œผ๋กœ ๋ชจ๋‘๋ฅผ ํ†ตํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

ํ’€์ด

  • ์š”๊ฑด ์†Œ ๋’ท๊ฑธ์Œ์งˆ ์น˜๋‹ค ๋งž์ถค.
  • ์ง„์งœ ์—ฌ๋Ÿฌ๋ฒˆ ํ‹€๋ ธ๋‹ค.
def solution(n, costs):
    cost = 0
    island = [i for i in range(n)]
    visited = [0 for i in range(n)]
    
    costs = [c for c in costs if c[2] != 0]
    costs.sort(key=lambda x:x[2])
    
    for i in range(len(costs)):
        isl1 = min(costs[i][0],costs[i][1])
        isl2 = max(costs[i][0],costs[i][1])
        
        #print(island, "cost:",cost)
        #print(visited, "find:", isl1, "->", isl2)
        
        if visited[isl1] == 1 and visited[isl2] == 1:
            if island[isl1] != island[isl2]:
                source = island[isl2]
                target = island[isl1]
                for z in range(len(island)):
                    if island[z] == source:
                        island[z] = target
                cost += costs[i][2]
        elif visited[isl1] == 0 or visited[isl2] == 0:
            visited[isl1] = 1
            visited[isl2] = 1
            source = island[isl2]
            target = island[isl1]
            for z in range(len(island)):
                    if island[z] == source:
                        island[z] = target
            cost += costs[i][2]
    
    return cost

์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด. ๋ญ”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค ใ…‹ใ…‹ใ…‹
import heapq as hq

def solution(n, costs):
    answer = 0
    from_to = list(list() for _ in range(n))
    visited = [False] * n
    priority = []

    for a, b, cost in costs:
        from_to[a].append((b, cost))
        from_to[b].append((a, cost))

    hq.heappush(priority, (0, 0))
    while False in visited:
        cost, start = hq.heappop(priority)
        if visited[start]: continue

        visited[start] = True
        answer += cost
        for end, cost in from_to[start]:
            if visited[end] : continue
            else:
                hq.heappush(priority, (cost, end))

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.02ms, 10MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
  • ๊ทธ๋‚˜๋งˆ ์ด์ชฝ์ด ์ข€ ๋” ์ดํ•ด๊ฐ€ ์‰ฝ๋‹ค.
    • ๋‚˜๋ž‘ ๋น„์Šทํ•œ ๊ฑฐ ๊ฐ™์€๋ฐ, Visited๋ž‘ sortedCost๋ฅผ ์ง€์šฐ๋ฉด์„œ ํ•˜๋Š”๋ฐ๋„ ๋น ๋ฅด๋„ค...
def solution(n, costs):
    answer = 0

    V = set()
    for v1, v2, cost in costs:
        V.add(v1)
        V.add(v2)
    sortedCosts = sorted(costs, key = lambda x: x[2])

    visited = set()

    visited.add(V.pop())
    while V:
        for i in range(len(sortedCosts)):
            v1, v2, cost = sortedCosts[i]
            if v1 in visited and v2 in visited:
                sortedCosts.pop(i)
                break
            elif v1 in visited or v2 in visited:
                if v1 in V:
                    V.remove(v1)
                if v2 in V:
                    V.remove(v2)
                visited.add(v1)
                visited.add(v2)
                answer += cost
                sortedCosts.pop(i)
                break

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
  • ๋‚ด ๋ฌธ์ œ์ ์ด ๋ญ”๊ฐ€ ๊ธธ์ฐพ๊ธฐ๋‚˜ ์ง€๋„๋‚˜ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ ๋‚˜์˜ค๋ฉด 2์ฐจ์› ๋ฐฐ์—ด๋ถ€ํ„ฐ ๊ทธ๋ฆด๋ ค๊ณ  ํ•ด์„œ ์‹œ๊ฐ„์„ ๋‚ญ๋น„ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
    • ๊ณ ์น ์ ์ธ ๋“ฏ...
728x90
๋ฐ˜์‘ํ˜•