This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ1463.java
More file actions
47 lines (40 loc) · 1.41 KB
/
Copy pathBOJ1463.java
File metadata and controls
47 lines (40 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BOJ1463 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int [] intArray = new int[1000001];
Arrays.fill(intArray, -1);
Queue q = new LinkedList();
Queue countQ = new LinkedList();
q.offer(1);
countQ.offer(0);
while (intArray[num] < 0) {
int present = (int) q.poll();
if (intArray[present] < 0) {
int presentCount = (int) countQ.poll();
intArray[present] = presentCount;
if (present + 1 < 1000001 && intArray[present + 1] < 0) {
q.offer(present + 1);
countQ.offer(presentCount + 1);
}
if (present * 2 < 1000001 && intArray[present * 2] < 0) {
q.offer(present * 2);
countQ.offer(presentCount + 1);
}
if (present * 3 < 1000001 && intArray[present * 3] < 0) {
q.offer(present * 3);
countQ.offer(presentCount + 1);
}
}
else
{
countQ.poll();
}
}
System.out.println(intArray[num]);
}
}