File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+
4+ class Solution {
5+ private static int MAX = 1_000_001 ;
6+ public int solution (int x , int y , int n ) {
7+ int answer = 0 ;
8+ Queue<int[]> q = new ArrayDeque<int[]> ();
9+ q. offer(new int []{0 , x});
10+ boolean [] visited = new boolean [MAX ];
11+ visited[x] = true ;
12+ boolean hasAnswer = false ;
13+ int count = 0 , value;
14+ while (! q. isEmpty()){
15+ int [] temp = q. poll();
16+ count = temp[0 ];
17+ value = temp[1 ];
18+ if (value == y){
19+ hasAnswer = true ;
20+ break ;
21+ }
22+ for (int new_value: new int []{value+ n, value* 2 , value* 3 }){
23+ if (new_value >= MAX ){
24+ continue ;
25+ }
26+ if (visited[new_value])
27+ continue ;
28+ q. offer(new int []{count+ 1 , new_value});
29+ visited[new_value] = true ;
30+ }
31+
32+ }
33+ return (hasAnswer)? count: - 1 ;
34+ }
35+ }
36+ ```
You can’t perform that action at this time.
0 commit comments