[TIL_Carrotww] 15 - 22/09/20

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

TIL

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

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

🧲 μ•Œκ³ λ¦¬μ¦˜

πŸ” 2019 LINE 인턴 μ±„μš© μ½”λ”© ν…ŒμŠ€νŠΈ

❓ Q. 연인 μ½”λ‹ˆμ™€ λΈŒλΌμš΄μ€ κ΄‘ν™œν•œ λ“€νŒμ—μ„œ β€˜λ‚˜ μž‘μ•„ 봐라’ κ²Œμž„μ„ ν•œλ‹€.
이 κ²Œμž„μ€ 브라운이 μ½”λ‹ˆλ₯Ό μž‘κ±°λ‚˜, μ½”λ‹ˆκ°€ λ„ˆλ¬΄ 멀리 λ‹¬μ•„λ‚˜λ©΄ λλ‚œλ‹€.
κ²Œμž„μ΄ λλ‚˜λŠ”λ° κ±Έλ¦¬λŠ” μ΅œμ†Œ μ‹œκ°„μ„ κ΅¬ν•˜μ‹œμ˜€.

쑰건은 λ‹€μŒκ³Ό κ°™λ‹€.
μ½”λ‹ˆλŠ” 처음 μœ„μΉ˜ Cμ—μ„œ 1초 ν›„ 1만큼 움직이고,
μ΄ν›„μ—λŠ” 가속이 λΆ™μ–΄ 맀 μ΄ˆλ§ˆλ‹€ 이전 이동 거리 + 1만큼 움직인닀.
즉 μ‹œκ°„μ— λ”°λ₯Έ μ½”λ‹ˆμ˜ μœ„μΉ˜λŠ” C, C + 1, C + 3, C + 6, …이닀.

λΈŒλΌμš΄μ€ ν˜„μž¬ μœ„μΉ˜ Bμ—μ„œ λ‹€μŒ μˆœκ°„ B – 1, B + 1, 2 * B 쀑 ν•˜λ‚˜λ‘œ 움직일 수 μžˆλ‹€.
μ½”λ‹ˆμ™€ 브라운의 μœ„μΉ˜ pλŠ” 쑰건 0 <= x <= 200,000을 λ§Œμ‘±ν•œλ‹€.
λΈŒλΌμš΄μ€ λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λŠ” μœ„μΉ˜λ‘œλŠ” 이동할 수 μ—†κ³ , μ½”λ‹ˆκ°€ λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λ©΄ κ²Œμž„μ΄ λλ‚œλ‹€

πŸ’‘ 풀이

from collections import deque

c = 11
b = 2


def catch_me(cony, brown):
    MAX = 200000
    time = 0
    queue = deque()
    queue.append((brown, 0))
    visited = [{} for _ in range(MAX + 1)]
    
    while cony <= MAX:
        cony += time
        if time in visited[cony]:
            return time
        for i in range(0, len(queue)):
            current_position, current_time = queue.popleft()

            new_position = current_position - 1
            new_time = current_time + 1
            if new_position >= 0 and new_time not in visited[new_position]:
                visited[new_position][new_time] = True
                queue.append((new_position, new_time))

            new_position = current_position + 1
            if new_position < 200001 and new_time not in visited[new_position]:
                visited[new_position][new_time] = True
                queue.append((new_position, new_time))

            new_position = current_position * 2
            if new_position < 200001 and new_time not in visited[new_position]:
                visited[new_position][new_time] = True
                queue.append((new_position, new_time))

        time += 1

    return -1


print(catch_me(c, b))  # 5
print("μ •λ‹΅ = 3 / ν˜„μž¬ 풀이 κ°’ = ", catch_me(10,3))
print("μ •λ‹΅ = 8 / ν˜„μž¬ 풀이 κ°’ = ", catch_me(51,50))
print("μ •λ‹΅ = 28 / ν˜„μž¬ 풀이 κ°’ = ", catch_me(550,500))

πŸ” 간단 ν•΄μ„€

  • cony 의 μœ„μΉ˜λŠ” μ‹œκ°„μ΄ 지날 수둝 λΉ¨λΌμ§€λ‹ˆ μˆ˜μ‹μ„ 써보면 cony += time 으둜 κ±Έμ–΄λ‘” ν›„ visited 리슀트 μ•ˆμ— BFS λ°©μ‹μœΌλ‘œ κ΅¬ν•œ brown λ…€μ„μ˜ 거리가 index, κ·Έ κ±°λ¦¬μ—μ„œμ˜ ν•΄λ‹Ή μ‹œκ°„μ΄ dict()의 key κ°’μœΌλ‘œ μ„€μ •ν•˜μ—¬ κ΅¬ν•œ 방식이닀.
  • λ‚˜λ¦„ 간단해 λ³΄μ˜€μ§€λ§Œ 잘 풀리지 μ•Šμ•˜λ˜ λ¬Έμ œμ΄λ‹€. μ²˜μŒμ—λŠ” μ‹œκ°„λ³„ μœ„μΉ˜λ₯Ό, λ„μ°©ν–ˆλ˜ μœ„μΉ˜λ₯Ό λ”°λ‘œ μ €μž₯ν•˜λ €κ³  ν–ˆμœΌλ‚˜ κ°•μ˜λ₯Ό λ“£κ³  ν‘Ό 방식이닀.
  • μ½”λ“œ 자체λ₯Ό μ΄ν•΄ν•˜κ³  λ³΄λŠ”λ°λŠ” 많이 λΉ¨λΌμ‘Œμ§€λ§Œ, bfs 계열이 머리속 생각이 μ½”λ“œλ‘œ μ‰½κ²Œ μ“°μ—¬μ§€μ§€μ•Šμ•„ μ’€ μ§œμ¦λ‚¬λ‹€ γ… 

🧲 μ•Œκ³ λ¦¬μ¦˜

πŸ” 2020 kakao λ¬Έμžμ—΄ μ••μΆ• μ˜ˆμ „μ— ν•œ 번 ν’€μ—ˆλ˜ λ¬Έμ œμ΄μ§€λ§Œ λ“£λŠ” μ•Œκ³ λ¦¬μ¦˜ κ°•μ˜μ— λ‚˜μ™€ ν•œ 번 더 ν’€μ–΄λ³Έ λ¬Έμ œμ΄λ‹€.
였늘 머리가 잘 μ•ˆλŒμ•„κ°€μ„œ μ—„μ²­ μ˜€λž˜κ±Έλ Έλ‹€. λ‚œ ν•˜λ£¨μ’…μΌ μ•Œκ³ λ¦¬μ¦˜λ§Œ ν•˜λŠ”κ±΄ 효율이 λ–¨μ–΄μ§€λŠ” 것 κ°™λ‹€. 집쀑이 잘 μ•ˆλœλ‹€...

πŸ’‘ 풀이

πŸ” 이 λ¬Έμ œλŠ” λ‚΄κ°€ λŠλΌκΈ°μ— μ½”λ”©μ—λŒ€ν•œ κΈ°μ΄ˆκ°€ μ–Όλ§ˆλ‚˜ νŠΌνŠΌν•œμ§€λ₯Ό μ•Œ 수 μžˆλŠ” 문제인 것 κ°™λ‹€.
python κΈ°μ€€μœΌλ‘œ forλ¬Έκ³Ό μŠ¬λΌμ΄μ‹±μ„ λŠ₯수 λŠ₯λž€ν•˜κ²Œ λ‹€λ£° 수 μžˆλ‹€λ©΄ ν’€ 수 μžˆλŠ” λ¬Έμ œλ‹ˆ 말이닀.
λ‚œ λ¬Έμžμ—΄ λ‹€λ£¨λŠ” λ¬Έμ œκ°€ μž¬λ°Œλ‹€.
기본적으둜 λ¬Έμžμ—΄ λ‹€λ£¨λŠ” λ¬Έμ œλŠ” λ³΅μž‘ν•˜μ§€κ°€ μ•Šλ‹€. 쑰금 κ³Όμž₯ν•΄μ„œ 문제λ₯Ό μ•ˆ 읽고 μž…μΆœλ ₯ 예제만 봐도 ν’€ 수 있기 λ•Œλ¬Έμ΄λ‹€. λ˜ν•œ λ‚˜μ˜ κΈ°λ³ΈκΈ°κ°€ μ–Όλ§ˆλ‚˜ λΆ€μ‘±ν•œμ§€(?) μ—¬μ‹€νžˆ λ“œλŸ¬λ‚˜κΈ° λ•Œλ¬Έμ΄λ‹€.
μ„€λͺ…은... ν‰μ†Œμ— 잘 적지도 μ•Šμ§€λ§Œ 이 λ¬Έμ œλŠ” λ”μš±μ΄ ν•„μš” 없을 것 κ°™λ‹€ γ…Žγ…Ž
ν’€μ΄λ§Œ 적고 였늘 끝!

def string_compression(string):
    result = []
    if len(string) == 1:
        return 1
    
    for word_len in range(1, len(string) // 2 + 1):
        temp = ''
        word_temp = [string[x:x + word_len] for x in range(0, len(string), word_len)]
        cnt = 1
        for i in range(1, len(word_temp)):
            if word_temp[i - 1] == word_temp[i]:
                cnt += 1
            else:
                if cnt == 1:
                    temp += word_temp[i - 1]
                else:
                    temp += (str(cnt) + word_temp[i - 1])
                    cnt = 1
        if cnt > 1:
            temp += str(cnt) + word_temp[-1]
        else:
            temp += word_temp[-1]

        result.append(len(temp))

    return min(result)
profile
Carrot_hyeong

0개의 λŒ“κΈ€