-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeque.java
More file actions
116 lines (98 loc) · 2.89 KB
/
Teque.java
File metadata and controls
116 lines (98 loc) · 2.89 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
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
// IO init
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int numLines = Integer.parseInt(in.readLine());
Teque teque = new Teque();
while (numLines-- > 0) {
StringTokenizer tk = new StringTokenizer(in.readLine());
String op = tk.nextToken();
int val = Integer.parseInt(tk.nextToken());
switch(op) {
case "push_back":
teque.pushBack(val);
break;
case "push_front":
teque.pushFront(val);
break;
case "push_middle":
teque.pushMid(val);
break;
case "get":
out.write(Integer.toString(teque.get(val)));
out.write("\n");
break;
}
}
in.close();
out.close();
}
}
class Teque {
Deque front;
Deque back;
public Teque() {
front = new Deque();
back = new Deque();
}
public void balance() {
if (front.size() < back.size()) { // back to front
front.put(front.tail, back.get(back.head + 1));
back.remove(back.head + 1);
front.tail++;
back.head++;
} else if (front.size() > back.size() + 1) { // front to back
back.put(back.head, front.get(front.tail - 1));
front.remove(front.tail - 1);
back.head--;
front.tail--;
}
}
public void pushBack(int val) {
back.put(back.tail, val);
back.tail++;
balance();
}
public void pushFront(int val) {
front.put(front.head, val);
front.head--;
balance();
}
public void pushMid(int val) {
front.put(front.tail, val);
front.tail++;
balance();
}
public Integer get(int index) {
if (index < front.size()) { // front deque
return front.get(index + front.head + 1);
} else { // back deque
return back.get(index - front.size() + back.head + 1);
}
}
}
class Deque {
public HashMap<Integer, Integer> hashmap;
public int head;
public int tail;
public Deque() {
hashmap = new HashMap<Integer, Integer>();
head = 0;
tail = 1;
}
public Integer get(Integer index) {
return hashmap.get(index);
}
public void put(Integer index, Integer val) {
hashmap.put(index, val);
}
public int size() {
return hashmap.size();
}
public void remove(Integer index) {
hashmap.remove(index);
}
}