๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[Coding Interview University] ํ : ์ด๋ก  ๋ฐ ๊ตฌํ˜„ ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป ์ปดํ“จํ„ฐ ๊ตฌ์กฐ/Computer Science

[Coding Interview University] ํ : ์ด๋ก  ๋ฐ ๊ตฌํ˜„

์ง•์ง•์•ŒํŒŒ์นด 2023. 7. 3. 15:14
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ์ฝ”๋”ฉ์ธํ„ฐ๋ทฐ๋Œ€ํ•™ ๋‹˜์˜ Git๊ณผ ์„œ์ ์„ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

 

๐Ÿซง ํ

: FIFO (First In First Out) ์ˆœ์„œ

: ํ์— ์ €์žฅ๋˜๋Š” ํ•ญ๋ชฉ๋“ค์€ ํ์— ์ถ”๊ฐ€๋˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ œ๊ฑฐ๋œ๋‹ค

  • ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ (bfs)
    • ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ๋…ธ๋“œ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ €์žฅํ•˜๋Š” ์šฉ๋„๋กœ ํ ์‚ฌ์šฉ
    • ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜ ์ฒ˜๋ฆฌํ•  ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ๋…ธ๋“œ์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์„ ํ์— ๋‹ค์‹œ ์ €์žฅ
    • ๋…ธ๋“œ๋ฅผ ์ ‘๊ทผํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
  • ์บ์‹œ ๊ตฌํ˜„

 

๐Ÿซง ํ ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ’– tail ํฌ์ธํ„ฐ๊ฐ€ ์žˆ๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๊ธฐ

โžก๏ธ enqueue(value) - tail์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ณณ์— value๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค

int main()
{
    int keys[] = { 1, 2, 3, 4, 5 };
    Queue q;
 
    // ์œ„์˜ ํ‚ค ์‚ฝ์ž…
    for (int key: keys) {
        q.enqueue(key);
    }
    return 0;
}

โžก๏ธ dequeue() - value๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ฐ€์žฅ ์ตœ๊ทผ์— ์ถ”๊ฐ€๋œ ์›์†Œ(front)๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

int main()
{
    int keys[] = { 1, 2, 3, 4, 5 };
    Queue q;
 
    // ์œ„์˜ ํ‚ค ์‚ฝ์ž…
    for (int key: keys) {
        q.enqueue(key);
    }
 
    cout << q.dequeue() << endl;    // 1์„ ์ถœ๋ ฅ
    cout << q.dequeue() << endl;    // 2๋ฅผ ์ถœ๋ ฅ
 
    return 0;
}

โžก๏ธ empty()

#include <iostream>
#include <stack>
#include <cstdlib>
using namespace std;
 
// ๋‘ ๊ฐœ์˜ Stack์„ ์‚ฌ์šฉํ•˜์—ฌ Queue ๊ตฌํ˜„
class Queue
{
    stack<int> s1, s2;
 
public:
    // Queue์— ์•„์ดํ…œ ์ถ”๊ฐ€
    void enqueue(int data)
    {
        // ์ฒซ ๋ฒˆ์งธ Stack์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋‘ ๋ฒˆ์งธ Stack์œผ๋กœ ์ด๋™
        while (!s1.empty())
        {
            s2.push(s1.top());
            s1.pop();
        }
 
        // ํ•ญ๋ชฉ์„ ์ฒซ ๋ฒˆ์งธ Stack์— ํ‘ธ์‹œ
        s1.push(data);
 
        // ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋‘ ๋ฒˆ์งธ Stack์—์„œ ์ฒซ ๋ฒˆ์งธ Stack์œผ๋กœ ๋‹ค์‹œ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        while (!s2.empty())
        {
            s1.push(s2.top());
            s2.pop();
        }
    }
 
    // Queue์—์„œ ์•„์ดํ…œ ์ œ๊ฑฐ
    int dequeue()
    {
        // ์ฒซ ๋ฒˆ์งธ Stack์ด ๋น„์–ด ์žˆ๋Š” ๊ฒฝ์šฐ
        if (s1.empty())
        {
            cout << "Underflow!!";
            exit(0);
        }
 
        // ์ฒซ ๋ฒˆ์งธ Stack์˜ ์ตœ์ƒ์œ„ ํ•ญ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
        int top = s1.top();
        s1.pop();
        return top;
    }
};

๐Ÿงก ๊ณ ์ • ๊ธธ์ด ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๊ธฐ

  • enqueue(value) - ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ €์žฅ ๊ณต๊ฐ„์˜ ๋์— item์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
  • dequeue() - value๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ฐ€์žฅ ์ตœ๊ทผ์— ์ถ”๊ฐ€๋œ ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  • empty()
  • full()

 

๐Ÿ’› ๋น„์šฉ

  • a bad implementation using linked list where you enqueue at head and dequeue at tail would be O(n) because you'd need the next to last element, causing a full traversal each dequeue
  • enqueue: O(1) (amortized, linked list and array [probing])
  • dequeue: O(1) (linked list and array)
  • empty: O(1) (linked list and array)

 

728x90
๋ฐ˜์‘ํ˜•
Comments