[TIL_Carrotww] 38 - 22/10/26

μœ ν˜•μ„Β·2022λ…„ 10μ›” 26일
0

TIL

λͺ©λ‘ 보기
46/138
post-thumbnail

πŸ“Carrotww의 μ½”λ”© 기둝μž₯

🧲 python algorithm

πŸ” programmers λ„€νŠΈμ›Œν¬ level3 dfs 문제
μš”μ¦˜ μ•Œκ³ λ¦¬μ¦˜μ„ λ„ˆλ¬΄ μ•ˆ ν’€μ–΄ 머리가 ꡳ어진 것 κ°™λ‹€. μ›λž˜ μ•Œκ³ λ¦¬μ¦˜ ν•˜λ©΄ 머리도 λ§λž‘ν•œ λŠλ‚Œμ΄ λ“€κ³  집쀑도 잘 λμ—ˆλŠ”λ°... 생각보닀 였래 κ±Έλ Έλ‹€. μ‰¬μš΄κ±° 같은데 λ¨Έλ¦¬μ†μ—μ„œ 자꾸 κΌ¬μ—¬μ„œ... μž¬κ·€λ₯Ό ν’€λ•Œ ν•œ 번 생각이 꼬여버리면 λ‹€μ‹œ κ°€λ‹₯을 작기 μ–΄λ €μš΄ 것 κ°™λ‹€ γ… γ…œ

  • 풀이
def solution(n, computers):
    cnt, visited = 0, []

    def dfs(index):
        visited.append(index)
        for i in range(n):
            if computers[index][i] == 1 and i not in visited:
                dfs(i)

    for i in range(n):
        if i not in visited:
            dfs(i)
            cnt += 1

    return cnt

🧲 python algorithm

πŸ” programmers μ—¬ν–‰κ²½λ‘œ μ˜ˆμ „μ— λΉ„μŠ·ν•œ 문제λ₯Ό leetcodeμ—μ„œ ν’€μ—ˆμ—ˆλŠ”λ° κΈ°μ–΅ λͺ»ν•˜λŠ”κ±° λ³΄λ‹ˆ λ‚œ μ—­μ‹œ γ…ƒ...
μ˜ˆμ „μ— ν’€λ•ŒλŠ” 5μ›”? 6월인가? μ§„μ§œ μ• μ†‘μ΄μΌλ•Œ ν’€μ—ˆμ–΄μ„œ λ§‰λ§‰ν–ˆμ—ˆλŠ”λ° μ§€κΈˆμ€ μ ‘κ·Ό 방식이 쑰금 잘λͺ»λλ‹€. μ•„λž˜ μ½”λ“œκ°€ μ‹€νŒ¨ν•˜λŠ” μ΄μœ λŠ” λ‹€μŒκ³Ό κ°™λ‹€.
ICN -> AAA
ICN -> BBB
BBB -> ICN
μœ„ μΌ€μ΄μŠ€κ°€ μ•ˆλ˜λŠ”λ° λ‚΄κ°€ x[0]x[1] κΈ°μ€€μœΌλ‘œ 정렬을 μ«™ ν•˜κ³  result에 λ„£κΈ° λ•Œλ¬Έμ— κ²°κ³Όκ°€ λ‹€λ₯Ό 수 밖에 μ—†λ‹€. ν‹€λ¦¬λŠ” μ΄μœ λŠ” μ•Œμ•˜μœΌλ‹ˆ μ‹œκ°„μ΄ λŠ¦μ€ κ΄€κ³„λ‘œ 내일.. ν’€μ–΄μ„œ 올리렀고 ν•œλ‹€ γ…Žγ…Ž

  • 첫 번째 풀이
from collections import deque

def solution(tickets):
    queue = deque()
    start = tickets[0][0]
    visited = list()
    result = [start]

    tickets.sort(key=lambda x:(x[0],x[1]))

    for i in tickets:
        if i[0] == start:
            queue.append(start)
            break

    while queue:
        temp = queue.pop()
        for ti in tickets:
            if ti[0] == temp and ti not in visited:
                visited.append(ti)
                queue.append(ti[1])
                result.append(ti[1])
                break
    return result
profile
Carrot_hyeong

0개의 λŒ“κΈ€