Skip to content

Commit 723bed0

Browse files
committed
102차 3번 문제 다시 풀이
1 parent 913f289 commit 723bed0

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
from collections import *
22

33
def solution(x, y, n):
4-
answer = 0
5-
result = []
4+
visited = set([x])
65

7-
q = deque([x])
6+
q = deque([(0, x)])
87
while q:
9-
x = q.popleft()
10-
calculate = [x + n, x * 2, x * 3]
11-
cnt = 0
12-
for i in range(3):
13-
cnt += 1
14-
nx = calculate[i]
15-
if nx == y:
16-
return cnt
17-
else:
18-
q.append(nx)
19-
20-
return answer
8+
cnt, x = q.popleft()
9+
if x == y:
10+
return cnt
11+
for nx in (x + n, x * 2, x * 3):
12+
if nx <= y and nx not in visited:
13+
visited.add(nx)
14+
q.append((cnt + 1, nx))
15+
16+
return -1

0 commit comments

Comments
 (0)