-
Notifications
You must be signed in to change notification settings - Fork 0
/
teque.java
130 lines (117 loc) · 3.95 KB
/
teque.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Teque {
public static void balance(OwnDeque front, OwnDeque back) {
if(front.getSize() - back.getSize() >= 2) {
back.addFirst(front.removeLast());
//remove front add to back
} else if(back.getSize() - front.getSize() >= 1) {
front.addLast(back.removeFirst());
//remove back, add to front
}
}
public static void main(String[] args) throws IOException {
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(r);
PrintWriter writer = new PrintWriter(System.out);
int n = Integer.parseInt(bf.readLine());
OwnDeque frontQueue = new OwnDeque(n);
OwnDeque backQueue = new OwnDeque(n);
for(int i = 0; i < n; i++) {
String command = bf.readLine();
String[] separateCommand = command.split(" ");
command = separateCommand[0];
int input = Integer.parseInt(separateCommand[1]);
if (command.equals("get")) {
if (input < frontQueue.getSize()) {
writer.println(frontQueue.get(input));
} else {
writer.println(backQueue.get(input - frontQueue.getSize()));
//need to minus the offset as index in backQueue starts from 0
}
} else if (command.equals("push_back")) {
backQueue.addLast(input);
balance(frontQueue, backQueue);
} else if (command.equals("push_front")) {
frontQueue.addFirst(input);
balance(frontQueue, backQueue);
} else if (command.equals("push_middle")) {
if (frontQueue.getSize() <= backQueue.getSize()) {
frontQueue.addLast(input);
} else {
backQueue.addFirst(input);
//dont need to balance here
}
}
}
bf.close();
writer.flush();
writer.close();
}
public static class OwnDeque {
int front;
int rear;
int[] array;
int size;
public OwnDeque(int size) {
array = new int[size];
front = - 1;
rear = - 1;
this.size = 0;
}
public void addFirst(int input){
if(front == -1 && rear == -1) {
front = 0;
rear = 0;
} else if(front == 0) {
front = array.length - 1;
} else {
front--;
}
array[front] = input;
size++;
}
public void addLast(int input) {
if(front == - 1 && rear == -1) {
front = 0;
rear = 0;
} else if(rear == array.length - 1 ){
rear = 0;
} else {
rear++;
}
array[rear] = input;
size++;
}
public int removeFirst(){
int item = array[front];
if(front == array.length - 1) {
front = 0;
} else {
front++;
}
size--;
return item;
}
public int removeLast() {
int item = array[rear];
if(rear == 0) {
rear = array.length - 1;
} else {
rear--;
}
size--;
return item;
}
public int get(int index) {
return array[(front + index) % array.length];
//finding the actual index 0
//offset index base on head index
}
public int getSize() {
return size;
}
}
}