-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlideOrJump.java
More file actions
419 lines (317 loc) · 9.62 KB
/
SlideOrJump.java
File metadata and controls
419 lines (317 loc) · 9.62 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package cs3005;
import java.util.ArrayList;
/***
*
* @author Sanjeeb Sangraula. This class emulates a game called Slide or Jump.
* You stand at one end of a board consisting of several cells, with
* each cell containing a non-negative integer that represents the cost
* of visiting that cell.
*
*/
public class SlideOrJump {
// To store the cost associated with the cells of the board
private ArrayList<Integer> board = new ArrayList<Integer>();
private int recMethodCount = 0, dpIterationCount = 0;
/**
* Argument constructor that takes the array containing the cost of the
* cells. Note that board will have at least two elements, the first one
* always being zero
*
* @param board
* the integer array that contains the values in the board
*/
public SlideOrJump(int[] board) {
// Initialize the board array with the given values
this.addScoresToBoard(board);
}
/**
* It is a method to initialize the board ArrayList with the given values in
* an int[]
*
* @param board
* the int[] with the values that the ArrayList board is to be
* initialized with
*/
private void addScoresToBoard(int[] board) {
for (int i = 0; i < board.length; i++) {
this.board.add(board[i]);
}
}
/**
* A method to compute overall cost recursively
*
* @return the cost of going through the cells, calculated recursively.
*/
public long recSolution() {
return calcCostRecursively(this.getScoresList());
}
/**
* A helper method to calculate overall cost recursively
*
* @param list
* the list with the elements that were in the board
* @return the overall cost of the moving to the last cell from the front of
* the cell
*/
private long calcCostRecursively(ArrayList<Integer> list) {
this.recMethodCount++;
if (list.size() == 1) {
// if there is only one element, then return the first element of
// the ArrayList
return list.remove(0);
} else if (list.size() == 2) {
return list.get(0) + list.get(1);
} else if (list.size() == 3) {
return list.get(0) + list.get(2);
} else if (list.size() > 1) {
if (this.isAscending(list)) {
list.remove(1);
return list.remove(0) + this.calcCostRecursively(list);
} else if (list.get(1) <= list.get(2)) {
return list.remove(0) + this.calcCostRecursively(list);
} else {
list.remove(1);
return list.remove(0) + this.calcCostRecursively(list);
}
} else {
return 0;
}
}
/**
* A method to compute overall cost using dynamic programming (required for
* A-B credit)
*
* @return the overall cost using dynamic programming, calculated using
* dynamic programming.
*/
public long dpSolution() {
return this.calcCostDynamically(this.getScoresList());
}
/**
* A method to calculate the cost of moving through the cells dynamically
*
* @param list
* the list containing the cost of the cells
* @return the total cost while moving through the cells, calculated
* dynamically
*/
private long calcCostDynamically(ArrayList<Integer> list) {
int index = 0, total = 0;
if (this.isAscending(list) && list.size() > 3) {
index = 1;
total = 0;
}
while (index < list.size()) {
this.dpIterationCount++;
total += list.get(index);
if (index == (list.size() - 3)) {
// at the third from last index or last element
index += 2;
} else if (index == (list.size() - 2)) {
// at the second last index
index += 1;
} else if (index == (list.size() - 1)) {
// at the last index
break;
} else {
if (this.isAscending(list)) {
index += 2;
} else if (list.get(index + 1) <= list.get(index + 2)) {
index += 1;
} else {
index += 2;
}
}
}
return total;
}
/**
* A method to get the cost of moving from cell n
*
* @param index
* the index of the board corresponding to the cell n
* @return the cost of moving from cell n. This corresponds to the value of
* the board at index i
*/
private int cost(int i) {
return this.board.get(i);
}
/**
* A method to return sequence of moves required
*
* @return the best moves from the first to the last cell
*/
public String getMoves() {
return this.getMovesDP(this.getScoresList());
}
/**
* A helper method to calculate the best sequence of moves to move across
* the cells with the minimum costs
*
* @param list
* the ArrayList with the costs associated with the cells
* @return a String containing the costs after moving through the cells
*/
private String getMovesDP(ArrayList<Integer> list) {
int index = 0;
String moves = "";
if (this.isAscending(list) && list.size() > 3) {
index = 1;
moves += "S";
}
while (index < list.size()) {
if (index == (list.size() - 3)) {
// at the third from last index or last element
index += 2;
moves += "J";
} else if (index == (list.size() - 2)) {
// at the second last index
index += 1;
moves += "S";
} else if (index == (list.size() - 1)) {
// at the last index
break;
} else if (list.size() > 3) {
if (this.isAscending(list)) {
index += 2;
moves += "J";
} else if (list.get(index + 1) <= list.get(index + 2)) {
index += 1;
moves += "S";
} else {
index += 2;
moves += "J";
}
} else {
return "";
}
}
return moves;
}
/**
* A method to get the scores ArrayList. It sends a copy of the board
* ArrayList, that contains the costs associated with the cells.
*
* @return a replica of the ArrayList with the cost associated with the
* cells.
*/
private ArrayList<Integer> getScoresList() {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < this.board.size(); i++) {
list.add(this.board.get(i));
}
return list;
}
/**
* A public method to get the number of operations that the recursive method
* performs
*
* @return the number of operations that the recursive algorithm performs
*/
public long countRecOperations() {
return this.getRecOperationsCount(this.getScoresList());
}
/**
* A method to count the number of operations that the dynamic programming
* method performs while finding the solution
*
* @return the number of operations performed by the dynamic programming
* algorithm
*/
public long countDpOperations() {
return this.getDpOperationsCount(this.getScoresList());
}
/**
* A public method to get the number of operations required for the
* recursive algorithm to find the cost of the cells, calculated recursively
*
* @return the number of operations performed by the countRecOperations()
* method
*/
public long recOperationsCount() {
return this.getRecOperationsCount(this.board);
}
/**
* A public method to count the number of operations performed by the
* dpOperationsCount() method
*
* @return the number of operations performed by the countDpOperations
* method
*/
public long dpOperationsCount() {
return this.getDpOperationsCount(this.board);
}
/**
* A method to calculate the number of operations performed by the
* getMoves() method to find the moves to take in the cells
*
* @return the number of operations performed
*/
public long movesOperationsCount() {
return this.getMovesOperationsCount(this.board);
}
/**
* A helper method to get the number of operations for the dynamic
* programming algorithm
*
* @param list
* the list array that contains the costs for the cells
* @return the number of operations performed by the dynamic programming
* operation
*/
private long getDpOperationsCount(ArrayList<Integer> list) {
return this.dpIterationCount;
}
/**
* A helper method to find the number of recursive operations performed by
* the recursive algorithm
*
* @param list
* the array containing the costs of all the cells
* @return the number of operations that the recursive algorithm performs
*/
private int getRecOperationsCount(ArrayList<Integer> list) {
return this.recMethodCount;
}
/**
* A method to get the number of operations in finding the number of
* operations required to find the moves
*
* @param list
* the list with the costs of the cells
* @return the number of operations the method executes while finding the
* moves required to move in the cells
*/
private long getMovesOperationsCount(ArrayList<Integer> list) {
if (list.size() == 2) {
return 1;
}
if (list.size() > 1) {
list.remove(0);
if (list.get(0) <= list.get(1)) {
return 1 + this.getMovesOperationsCount(list);
} else {
list.remove(0);
return 1 + this.getMovesOperationsCount(list);
}
}
return 0;
}
/**
* A private helper method to find if an ArrayList<Integer> is ascending
*
* @param list
* the ArrayList which is to be checked if it is ascending
* @return true if the ArrayList<Integer> is ascending, false otherwise
*/
private boolean isAscending(ArrayList<Integer> list) {
boolean ascending = true;
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) > list.get(i + 1)) {
ascending = false;
break;
}
}
return ascending;
}
}