-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStepsTo1.java
More file actions
138 lines (122 loc) · 3.5 KB
/
Copy pathMinStepsTo1.java
File metadata and controls
138 lines (122 loc) · 3.5 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Implements algorithms to get 1 from any number with a set of operations.
*/
package dp;
import java.util.ArrayList;
/**
* Class for each item in the queue.
*/
class QueueItem {
// Current value in an item.
int value;
// Number of steps to get to this value.
int curStep;
// How did we get here in a string.
String calcs;
/**
* Constructor of an item, sets class properties.
* @param value Value of the item.
* @param curStep How many steps did it take to get to this value.
* @param calcs String representing how did we get here exactly.
*/
QueueItem(int value, int curStep, String calcs) {
this.value = value;
this.curStep = curStep;
this.calcs = calcs;
}
}
class MinStepsTo1 {
/**
* Sets the number and calls the algorithm.
* @param args Program arguments, unused.
*/
public static void main(String[] args) {
int a = 510;
System.out.println("Steps to break " + a + ": " + getMinStepsTopDown(a));
System.out.println("Steps to break " + a + ": " + getMinStepsBottomUp(a));
}
/**
* Recursion wrapper for the top down approach:
* creates a queue and calls the recursive function.
* @param a Value to transfrom to one.
* @return Number of steps to get to 1.
*/
public static int getMinStepsBottomUp(int a) {
ArrayList<QueueItem> queue = new ArrayList<QueueItem>();
queue.add(new QueueItem(1, 0, "1"));
return getMinStepsBottomUp(queue, a);
}
/**
* Recursive function to calculate minimum steps using
* top down approach.
* @param queue The queue of elements to analyze.
* @return Number of steps to get to 1.
*/
private static int getMinStepsBottomUp(ArrayList<QueueItem> queue, int finish) {
QueueItem item = queue.remove(0);
if (item.value == finish) {
System.out.println(item.calcs);
return item.curStep;
}
queue.add(new QueueItem(
item.value * 3,
item.curStep + 1,
item.calcs + " * 3"
));
queue.add(new QueueItem(
item.value * 2,
item.curStep + 1,
item.calcs + " * 2"
));
queue.add(new QueueItem(
item.value + 1,
item.curStep + 1,
'(' + item.calcs + " + 1)"
));
return getMinStepsBottomUp(queue, finish);
}
/**
* Recursion wrapper for the top down approach:
* creates a queue and calls the recursive function.
* @param a Value to transfrom to one.
* @return Number of steps to get to 1.
*/
public static int getMinStepsTopDown(int a) {
ArrayList<QueueItem> queue = new ArrayList<QueueItem>();
queue.add(new QueueItem(a, 0, Integer.toString(a)));
return getMinStepsTopDown(queue);
}
/**
* Recursive function to calculate minimum steps using
* top down approach.
* @param queue The queue of elements to analyze.
* @return Number of steps to get to 1.
*/
private static int getMinStepsTopDown(ArrayList<QueueItem> queue) {
QueueItem item = queue.remove(0);
if (item.value == 1) {
System.out.println(item.calcs);
return item.curStep;
}
if (item.value % 3 == 0) {
queue.add(new QueueItem(
item.value / 3,
item.curStep + 1,
item.calcs + " / 3"
));
}
if (item.value % 2 == 0) {
queue.add(new QueueItem(
item.value / 2,
item.curStep + 1,
item.calcs + " / 2"
));
}
queue.add(new QueueItem(
item.value - 1,
item.curStep + 1,
'(' + item.calcs + " - 1)"
));
return getMinStepsTopDown(queue);
}
}