class MyCircularDeque {
int size = 0;
int[] nums;
int start = 0;
int end = -1;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
nums = new int[k];
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if (isFull()) return false;
if (isEmpty()) {
start = 0;
end = 0;
} else {
start = (start == 0 ? nums.length - 1 : start - 1);
}
nums[start] = value;
size++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if (isFull()) return false;
end = (end + 1) % nums.length;
nums[end] = value;
size++;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if (isEmpty()) return false;
start = (start + 1) % nums.length;
size--;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if (isEmpty()) return false;
end = (end == 0 ? nums.length-1 : end-1);
size--;
return true;
}
/** Get the front item from the deque. */
public int getFront() {
return isEmpty() ? -1 : nums[start];
}
/** Get the last item from the deque. */
public int getRear() {
return isEmpty() ? -1 : nums[end];
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return size == 0;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return size == nums.length;
}
}