π 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)