-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnackTower.java
More file actions
57 lines (57 loc) · 2.13 KB
/
SnackTower.java
File metadata and controls
57 lines (57 loc) · 2.13 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
// this solution presents time-limit exceeded while performing on a large scale of numbers
import java.util.*;
public class SnackTower {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), index = 0;
int[] a = new int[n];
while(index < n) {
a[index] = sc.nextInt();
index++;
}
sc.close();
// min is the latest printed number
// assume the latest printed number is (n + 1)
int min = n + 1;
// store the unprinted numbers
List<Integer> aL = new ArrayList<>();
for(int i = 0; i < n; i++) {
int e = a[i];
if(e + 1 == min) {
System.out.print(e + " ");
// everytime a number has been printed,
// the min changes its value to the last printed number
min = e;
for(int j = 0; j < aL.size(); j++) {
int num = aL.get(j);
if(num + 1 == min) {
aL.remove(j);
j--;
// min changes its value to the previous printed number num
min = num;
System.out.print(num + " ");
} else {
// since num is no suitable for printing and elements in aL
// are stored in descending order, there is no sense to continue the loop
break;
}
}
System.out.println("");
} else {
System.out.println("");
// ensure e will be added properly
boolean added = false;
for(int k = 0; k < aL.size(); k++) {
if(aL.get(k) < e) {
aL.add(k, e);
added = true;
// once e has been added, loop has to stop.
// otherwise it will be added so many times
break;
}
}
if(!added) aL.add(e);
}
}
}
}