넘치게 채우기

[LeetCode] 641. Design Circular Deque 본문

PS/LeetCode

[LeetCode] 641. Design Circular Deque

riveroverflow 2024. 9. 28. 15:46
728x90
반응형

https://leetcode.com/problems/design-circular-deque/description/?envType=daily-question&envId=2024-09-28

leetcode - Design Circular Deque

문제 유형 : 데크, 큐, 스택, 구현

문제 난이도 : Medium

 

문제

Design your implementation of the circular double-ended queue (deque).

Implement the MyCircularDeque class:

  • MyCircularDeque(int k) Initializes the deque with a maximum size of k.
  • boolean insertFront() Adds an item at the front of Deque. Returns true if the operation is successful, or false otherwise.
  • boolean insertLast() Adds an item at the rear of Deque. Returns true if the operation is successful, or false otherwise.
  • boolean deleteFront() Deletes an item from the front of Deque. Returns true if the operation is successful, or false otherwise.
  • boolean deleteLast() Deletes an item from the rear of Deque. Returns true if the operation is successful, or false otherwise.
  • int getFront() Returns the front item from the Deque. Returns -1 if the deque is empty.
  • int getRear() Returns the last item from Deque. Returns -1 if the deque is empty.
  • boolean isEmpty() Returns true if the deque is empty, or false otherwise.
  • boolean isFull() Returns true if the deque is full, or false otherwise.

MyCircularDeque 클래스를 구현하라.

최대 k개의 사이즈를 쓴다.

 

풀이

원형 큐를 그대로 구현해주면 된다.

front와 rear로 인덱스를 지정하고, 삽입 또는 삭제에 대해서 모듈러 k연산을 해주면 된다.

 

코드

C++

#pragma GCC optimize("03", "unroll-loops");
static const int __ = [](){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    return 0;
}();

class MyCircularDeque {
    vector<int> arr;
    int front;
    int rear;
    int count;
    int k;
public:
    MyCircularDeque(int k) {
        this -> k = k;
        arr.resize(k);
        front = 1;
        rear = 0;
        count = 0;
    }
    
    bool insertFront(int value) {
        if(this -> isFull()) return false;        
        front = (front - 1 + k) % k;
        arr[front] = value;
        count++;
        return true;
    }
    
    bool insertLast(int value) {
        if(this -> isFull()) return false;
        rear = (rear + 1) % k;
        arr[rear] = value;
        count++;
        return true;
    }
    
    bool deleteFront() {
        if(isEmpty()) return false;
        arr[front] = -1;
        front = (front + 1) % k;
        count--;
        return true;
    }
    
    bool deleteLast() {
        if(isEmpty()) return false;
        arr[rear] = -1;
        rear = (rear - 1 + k) % k;
        count--;
        return true;
    }
    
    int getFront() {
        if(isEmpty()) return -1;
        return arr[front];
    }
    
    int getRear() {
        if(isEmpty()) return -1;
        return arr[rear];
    }
    
    bool isEmpty() {
        return count == 0;
    }
    
    bool isFull() {
        return count == k;
    }
};

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque* obj = new MyCircularDeque(k);
 * bool param_1 = obj->insertFront(value);
 * bool param_2 = obj->insertLast(value);
 * bool param_3 = obj->deleteFront();
 * bool param_4 = obj->deleteLast();
 * int param_5 = obj->getFront();
 * int param_6 = obj->getRear();
 * bool param_7 = obj->isEmpty();
 * bool param_8 = obj->isFull();
 */
728x90
반응형