From 3a8cb0d6fd6d7b6b0e7f9d7d31db47e511a693f8 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Thu, 17 Oct 2024 16:08:02 +0800 Subject: [PATCH] 225th Commit --- README.md | 17 +----- algorithms/dynamic-programming/README.md | 2 +- algorithms/queue/Queue.ts | 0 algorithms/queue/README.md | 69 ++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 algorithms/queue/Queue.ts create mode 100644 algorithms/queue/README.md diff --git a/README.md b/README.md index e404371..f98c471 100644 --- a/README.md +++ b/README.md @@ -72,25 +72,12 @@ $ pnpm bench twoSum.bench.ts - 雜湊表 (Hash Table) - 堆積 (Heap) - 樹 (Tree) - - 二元樹 (Binary Tree) - - 二元搜尋樹 (Binary Search Tree) - [圖 (Graph)](./algorithms/graph/README.md) - 字典樹 (Trie) - 排序 (Sorting) - - 冒泡排序 (Bubble Sort) - - 選擇排序 (Selection Sort) - - 插入排序 (Insertion Sort) - - 合併排序 (Merge Sort) - - 快速排序 (Quick Sort) - - 堆積排序 (Heap Sort) -- 搜尋 (Searching) - - [二分搜尋 (Binary Search)](./algorithms/searching/README.md) -- 動態規劃 (Dynamic Programming) - - [0-1 背包問題 (0-1 Knapsack Problem)](./algorithms/dynamic-programming/README.md#0-1-背包問題-0-1-knapsack-problem) - - 完全背包問題 (Unbounded Knapsack Problem) - - 多維背包問題 (Multidimensional Knapsack Problem) +- [搜尋 (Searching)](./algorithms/searching/README.md) +- [動態規劃 (Dynamic Programming)](./algorithms/dynamic-programming/README.md) - 貪婪 (Greedy) - - 分數背包問題 (Fractional Knapsack Problem) - 回溯 (Backtracking) - 分治 (Divide and Conquer) - 位元操作 (Bit Manipulation) diff --git a/algorithms/dynamic-programming/README.md b/algorithms/dynamic-programming/README.md index fcc3c41..739fe03 100644 --- a/algorithms/dynamic-programming/README.md +++ b/algorithms/dynamic-programming/README.md @@ -104,7 +104,7 @@ const capacity = 4; console.log(knapsack(weights, values, capacity)); // 20 ``` -## 完全背包問題 (Unbounded Knapsack Problem) +## 無界背包問題 (Unbounded Knapsack Problem) 遞推公式: diff --git a/algorithms/queue/Queue.ts b/algorithms/queue/Queue.ts new file mode 100644 index 0000000..e69de29 diff --git a/algorithms/queue/README.md b/algorithms/queue/README.md new file mode 100644 index 0000000..9b6da08 --- /dev/null +++ b/algorithms/queue/README.md @@ -0,0 +1,69 @@ +# 佇列 (Queue) + +以先進先出 (First In First Out,FIFO) 為原則。 + +- [`Queue.ts`](./Queue.ts) + +```ts +class Queue { + private items: T[] = []; + + // 加入元素到佇列的尾端 + enqueue(item: T): void { + this.items.push(item); + } + + // 從佇列的頭部移除元素 + dequeue(): T | undefined { + return this.items.shift(); + } + + // 查看佇列的第一個元素(不移除) + peek(): T | undefined { + return this.items[0]; + } + + // 檢查佇列是否為空 + isEmpty(): boolean { + return this.items.length === 0; + } + + // 取得佇列的長度 + size(): number { + return this.items.length; + } + + // 清空佇列 + clear(): void { + this.items = []; + } +} +``` + +```ts +const queue = new Queue(); + +queue.enqueue(10); +queue.enqueue(20); +queue.enqueue(30); + +console.log(queue.peek()); // 10 +console.log(queue.dequeue()); // 10 +console.log(queue.size()); // 2 +console.log(queue.isEmpty()); // false + +queue.clear(); +console.log(queue.isEmpty()); // true +``` + +## 雙向佇列 (Double-ended Queue) + +## 優先佇列 (Priority Queue) + +## 雙向優先佇列 (Double Priority Queue) + +## 有界佇列 (Bounded Queue) + +### 圓形佇列 (Circular Queue) / 環形緩衝區 (Circular Buffer) + +### 尾部丟棄 (Tail Drop)