We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 913f289 commit 723bed0Copy full SHA for 723bed0
1 file changed
live10/test102/문제3/박희경.py
@@ -1,20 +1,16 @@
1
from collections import *
2
3
def solution(x, y, n):
4
- answer = 0
5
- result = []
+ visited = set([x])
6
7
- q = deque([x])
+ q = deque([(0, x)])
8
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
+ cnt, x = q.popleft()
+ if x == y:
+ return cnt
+ for nx in (x + n, x * 2, x * 3):
+ if nx <= y and nx not in visited:
+ visited.add(nx)
+ q.append((cnt + 1, nx))
+
+ return -1
0 commit comments