From ee93cd1d0fe36f005de7313aa69c3e821b9bc4ac Mon Sep 17 00:00:00 2001 From: Slone <50995948+aa274325721@users.noreply.github.com> Date: Mon, 5 Dec 2022 13:36:51 +0800 Subject: [PATCH 01/15] Update binary_search.md --- docs/chapter_searching/binary_search.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index d0e4a6d..5000b66 100644 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -220,7 +220,24 @@ $$ === "Go" ```go title="binary_search.go" - + /* 二分查找(左闭右开) */ + func binarySearch1(nums []int, target int) int { + // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 + i, j := 0, len(nums) + // 循环,当搜索区间为空时跳出(当 i = j 时为空) + for i < j { + m := (i + j) / 2 // 计算中点索引 m + if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中 + i = m + 1 + } else if nums[m] > target { // 此情况说明 target 在区间 [i, m) 中 + j = m + } else { // 找到目标元素,返回其索引 + return m + } + } + // 未找到目标元素,返回 -1 + return -1 + } ``` === "JavaScript" From b580b29b666e2347fcac4eb4cf54eb16b63ef9a3 Mon Sep 17 00:00:00 2001 From: Slone <50995948+Slone123c@users.noreply.github.com> Date: Mon, 5 Dec 2022 19:08:24 +0800 Subject: [PATCH 02/15] Update binary_search.md Left closed right closed interval bsearch using go --- docs/chapter_searching/binary_search.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index 5000b66..04dbba8 100644 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -123,7 +123,24 @@ $$ === "Go" ```go title="binary_search.go" - + /* 二分查找(左闭右开) */ + func binarySearch1(nums []int, target int) int { + // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 + i, j := 0, len(nums) + // 循环,当搜索区间为空时跳出(当 i = j 时为空) + for i < j { + m := (i + j) / 2 // 计算中点索引 m + if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中 + i = m + 1 + } else if nums[m] > target { // 此情况说明 target 在区间 [i, m) 中 + j = m + } else { // 找到目标元素,返回其索引 + return m + } + } + // 未找到目标元素,返回 -1 + return -1 + } ``` === "JavaScript" From 75ffa5180eb8deda585fc73d55de985f1b37f661 Mon Sep 17 00:00:00 2001 From: Slone <50995948+Slone123c@users.noreply.github.com> Date: Mon, 5 Dec 2022 19:14:50 +0800 Subject: [PATCH 03/15] Update binary_search.md Edited number out of bound topic --- docs/chapter_searching/binary_search.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index 04dbba8..0a407ff 100644 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -328,7 +328,10 @@ $$ === "Go" ```go title="" - + // (i + j) 有可能超出 int 的取值范围 + m := (i + j) / 2 + // 更换为此写法则不会越界 + m := i + (j - i) / 2 ``` === "JavaScript" From 829e13494ebab43166e4f8e8fa199754522875b1 Mon Sep 17 00:00:00 2001 From: Slone <50995948+Slone123c@users.noreply.github.com> Date: Mon, 5 Dec 2022 20:39:42 +0800 Subject: [PATCH 04/15] binary_search.go and binary_search_test uploaded --- codes/go/chapter_searching/binary_search.go | 43 +++++++++++++++++++ .../chapter_searching/binary_search_test.go | 24 +++++++++++ 2 files changed, 67 insertions(+) create mode 100644 codes/go/chapter_searching/binary_search.go create mode 100644 codes/go/chapter_searching/binary_search_test.go diff --git a/codes/go/chapter_searching/binary_search.go b/codes/go/chapter_searching/binary_search.go new file mode 100644 index 0000000..437f79a --- /dev/null +++ b/codes/go/chapter_searching/binary_search.go @@ -0,0 +1,43 @@ +// File: binary_search.go +// Created Time: 2022-12-5 +// Author: Slone123c (274325721@qq.com) + +package chapter_searching + +/* 二分查找(双闭区间) */ +func binarySearch(nums []int, target int) int { + // 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 + i, j := 0, len(nums)-1 + // 循环,当搜索区间为空时跳出(当 i > j 时为空) + for i <= j { + m := (i + j) / 2 // 计算中点索引 m + if nums[m] < target { // 此情况说明 target 在区间 [m+1, j] 中 + i = m + 1 + } else if nums[m] > target { // 此情况说明 target 在区间 [i, m-1] 中 + j = m - 1 + } else { // 找到目标元素,返回其索引 + return m + } + } + // 未找到目标元素,返回 -1 + return -1 +} + +/* 二分查找(左闭右开) */ +func binarySearch1(nums []int, target int) int { + // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 + i, j := 0, len(nums) + // 循环,当搜索区间为空时跳出(当 i = j 时为空) + for i < j { + m := (i + j) / 2 // 计算中点索引 m + if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中 + i = m + 1 + } else if nums[m] > target { // 此情况说明 target 在区间 [i, m) 中 + j = m + } else { // 找到目标元素,返回其索引 + return m + } + } + // 未找到目标元素,返回 -1 + return -1 +} diff --git a/codes/go/chapter_searching/binary_search_test.go b/codes/go/chapter_searching/binary_search_test.go new file mode 100644 index 0000000..341b93e --- /dev/null +++ b/codes/go/chapter_searching/binary_search_test.go @@ -0,0 +1,24 @@ +// File: binary_search_test.go +// Created Time: 2022-11-25 +// Author: Slone123c (274325721@qq.com) + +package chapter_searching + +import ( + "fmt" + "testing" +) + +func TestBinarySearch(t *testing.T) { + var ( + target = 3 + nums = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + expected = 2 + ) + // 在数组中执行二分查找 + actual := binarySearch(nums, target) + fmt.Println("目标元素 3 的索引 =", actual) + if actual != expected { + t.Errorf("目标元素 3 的索引 = %d, 应该为 %d", actual, expected) + } +} From 27e993e6dfd18db551d28cb4c84cc924f9229a71 Mon Sep 17 00:00:00 2001 From: Slone <50995948+Slone123c@users.noreply.github.com> Date: Mon, 5 Dec 2022 20:45:46 +0800 Subject: [PATCH 05/15] binary_search_test_ edited time --- codes/go/chapter_searching/binary_search_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/go/chapter_searching/binary_search_test.go b/codes/go/chapter_searching/binary_search_test.go index 341b93e..aa0ff30 100644 --- a/codes/go/chapter_searching/binary_search_test.go +++ b/codes/go/chapter_searching/binary_search_test.go @@ -1,5 +1,5 @@ // File: binary_search_test.go -// Created Time: 2022-11-25 +// Created Time: 2022-12-5 // Author: Slone123c (274325721@qq.com) package chapter_searching From d0d53c5a84ca956a35ea706382ec185b30e45c03 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Mon, 5 Dec 2022 22:03:36 +0800 Subject: [PATCH 06/15] feat: add queue in js --- .../chapter_stack_and_queue/queue.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 codes/javascript/chapter_stack_and_queue/queue.js diff --git a/codes/javascript/chapter_stack_and_queue/queue.js b/codes/javascript/chapter_stack_and_queue/queue.js new file mode 100644 index 0000000..cb5b3e4 --- /dev/null +++ b/codes/javascript/chapter_stack_and_queue/queue.js @@ -0,0 +1,29 @@ +/** + * File: queue.js + * Created Time: 2022-12-04 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + +/* 初始化队列 */ +// Javascript 没有内置的队列,可以把 Array 当作队列来使用 +// 但是,要注意虽然Javascript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 +const queue = []; + +/* 元素入队 */ +queue.push(1); +queue.push(3); +queue.push(2); +queue.push(5); +queue.push(4); + +/* 访问队首元素 */ +const peek = queue[0]; + +/* 元素出队 */ +const poll = queue.shift(); + +/* 获取队列的长度 */ +const size = queue.length; + +/* 判断队列是否为空 */ +const empty = queue.length === 0; From d8bc3ba3e596dde7fe2c8b1066d0613281662423 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Mon, 5 Dec 2022 22:05:40 +0800 Subject: [PATCH 07/15] feat: add queue in ts --- .../chapter_stack_and_queue/queue.js | 2 +- .../chapter_stack_and_queue/queue.ts | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 codes/typescript/chapter_stack_and_queue/queue.ts diff --git a/codes/javascript/chapter_stack_and_queue/queue.js b/codes/javascript/chapter_stack_and_queue/queue.js index cb5b3e4..2a90008 100644 --- a/codes/javascript/chapter_stack_and_queue/queue.js +++ b/codes/javascript/chapter_stack_and_queue/queue.js @@ -6,7 +6,7 @@ /* 初始化队列 */ // Javascript 没有内置的队列,可以把 Array 当作队列来使用 -// 但是,要注意虽然Javascript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 +// 注意:虽然Javascript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 const queue = []; /* 元素入队 */ diff --git a/codes/typescript/chapter_stack_and_queue/queue.ts b/codes/typescript/chapter_stack_and_queue/queue.ts new file mode 100644 index 0000000..6b8fa4a --- /dev/null +++ b/codes/typescript/chapter_stack_and_queue/queue.ts @@ -0,0 +1,31 @@ +/** + * File: queue.js + * Created Time: 2022-12-04 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + +/* 初始化队列 */ +// Typescript 没有内置的队列,可以把 Array 当作队列来使用 +// 注意:虽然Typescript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 +const queue: number[] = []; + +/* 元素入队 */ +queue.push(1); +queue.push(3); +queue.push(2); +queue.push(5); +queue.push(4); + +/* 访问队首元素 */ +const peek = queue[0]; + +/* 元素出队 */ +const poll = queue.shift(); + +/* 获取队列的长度 */ +const size = queue.length; + +/* 判断队列是否为空 */ +const empty = queue.length === 0; + +export { }; \ No newline at end of file From 77611bc758f2000ec92ab5d1f310c9add314bd47 Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Mon, 5 Dec 2022 22:06:31 +0800 Subject: [PATCH 08/15] feat: complement doc --- docs/chapter_stack_and_queue/queue.md | 64 ++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index a663b97..87f0abd 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -20,13 +20,13 @@ comments: true
-| 方法 | 描述 | -| --------- | ------------------------ | -| offer() | 元素入队,即将元素添加至队尾 | -| poll() | 队首元素出队 | -| front() | 访问队首元素 | -| size() | 获取队列的长度 | -| isEmpty() | 判断队列是否为空 | +| 方法 | 描述 | +| --------- | ---------------------------- | +| offer() | 元素入队,即将元素添加至队尾 | +| poll() | 队首元素出队 | +| front() | 访问队首元素 | +| size() | 获取队列的长度 | +| isEmpty() | 判断队列是否为空 |
@@ -143,13 +143,63 @@ comments: true === "JavaScript" ```js title="queue.js" +/* 初始化队列 */ + // Javascript 没有内置的队列,可以把 Array 当作队列来使用 + // 注意:虽然Javascript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 + const queue = []; + /* 元素入队 */ + queue.push(1); + queue.push(3); + queue.push(2); + queue.push(5); + queue.push(4); + + /* 访问队首元素 */ + const peek = queue[0]; + + /* 元素出队 */ + const poll = queue.shift(); + + /* 获取队列的长度 */ + const size = queue.length; + + /* 判断队列是否为空 */ + const empty = queue.length === 0; ``` === "TypeScript" ```typescript title="queue.ts" + /** + * File: queue.js + * Created Time: 2022-12-04 + * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) + */ + /* 初始化队列 */ + // Typescript 没有内置的队列,可以把 Array 当作队列来使用 + // 注意:虽然Typescript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 + const queue: number[] = []; + + /* 元素入队 */ + queue.push(1); + queue.push(3); + queue.push(2); + queue.push(5); + queue.push(4); + + /* 访问队首元素 */ + const peek = queue[0]; + + /* 元素出队 */ + const poll = queue.shift(); + + /* 获取队列的长度 */ + const size = queue.length; + + /* 判断队列是否为空 */ + const empty = queue.length === 0; ``` === "C" From a81a0536726ca8e5fecacbcf13bf3ced37e63dbc Mon Sep 17 00:00:00 2001 From: S-N-O-R-L-A-X Date: Mon, 5 Dec 2022 22:07:41 +0800 Subject: [PATCH 09/15] fix: fix small problems --- codes/javascript/chapter_stack_and_queue/queue.js | 2 +- codes/typescript/chapter_stack_and_queue/queue.ts | 4 ++-- docs/chapter_stack_and_queue/queue.md | 8 +------- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/codes/javascript/chapter_stack_and_queue/queue.js b/codes/javascript/chapter_stack_and_queue/queue.js index 2a90008..e08d253 100644 --- a/codes/javascript/chapter_stack_and_queue/queue.js +++ b/codes/javascript/chapter_stack_and_queue/queue.js @@ -1,6 +1,6 @@ /** * File: queue.js - * Created Time: 2022-12-04 + * Created Time: 2022-12-05 * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) */ diff --git a/codes/typescript/chapter_stack_and_queue/queue.ts b/codes/typescript/chapter_stack_and_queue/queue.ts index 6b8fa4a..eb7b813 100644 --- a/codes/typescript/chapter_stack_and_queue/queue.ts +++ b/codes/typescript/chapter_stack_and_queue/queue.ts @@ -1,6 +1,6 @@ /** - * File: queue.js - * Created Time: 2022-12-04 + * File: queue.ts + * Created Time: 2022-12-05 * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) */ diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index 87f0abd..e9c9fcb 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -143,7 +143,7 @@ comments: true === "JavaScript" ```js title="queue.js" -/* 初始化队列 */ + /* 初始化队列 */ // Javascript 没有内置的队列,可以把 Array 当作队列来使用 // 注意:虽然Javascript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 const queue = []; @@ -171,12 +171,6 @@ comments: true === "TypeScript" ```typescript title="queue.ts" - /** - * File: queue.js - * Created Time: 2022-12-04 - * Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com) - */ - /* 初始化队列 */ // Typescript 没有内置的队列,可以把 Array 当作队列来使用 // 注意:虽然Typescript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 From bb00bb862045062834807dfa7f3475acae5633a2 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 5 Dec 2022 22:32:08 +0800 Subject: [PATCH 10/15] Update binary_search.go --- codes/go/chapter_searching/binary_search.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/go/chapter_searching/binary_search.go b/codes/go/chapter_searching/binary_search.go index 437f79a..cbaa851 100644 --- a/codes/go/chapter_searching/binary_search.go +++ b/codes/go/chapter_searching/binary_search.go @@ -1,5 +1,5 @@ // File: binary_search.go -// Created Time: 2022-12-5 +// Created Time: 2022-12-05 // Author: Slone123c (274325721@qq.com) package chapter_searching From 1f24a7c75d2855d6f943d952d191720510f05fff Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 5 Dec 2022 22:32:56 +0800 Subject: [PATCH 11/15] Update binary_search_test.go --- codes/go/chapter_searching/binary_search_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/go/chapter_searching/binary_search_test.go b/codes/go/chapter_searching/binary_search_test.go index aa0ff30..3dada95 100644 --- a/codes/go/chapter_searching/binary_search_test.go +++ b/codes/go/chapter_searching/binary_search_test.go @@ -1,5 +1,5 @@ // File: binary_search_test.go -// Created Time: 2022-12-5 +// Created Time: 2022-12-05 // Author: Slone123c (274325721@qq.com) package chapter_searching From 0c5e2c45c8e7a57723935cfdcb478f87ec109d10 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 5 Dec 2022 22:54:26 +0800 Subject: [PATCH 12/15] Update queue.js --- codes/javascript/chapter_stack_and_queue/queue.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/codes/javascript/chapter_stack_and_queue/queue.js b/codes/javascript/chapter_stack_and_queue/queue.js index e08d253..2ec3d69 100644 --- a/codes/javascript/chapter_stack_and_queue/queue.js +++ b/codes/javascript/chapter_stack_and_queue/queue.js @@ -6,7 +6,7 @@ /* 初始化队列 */ // Javascript 没有内置的队列,可以把 Array 当作队列来使用 -// 注意:虽然Javascript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 +// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n) const queue = []; /* 元素入队 */ @@ -20,6 +20,7 @@ queue.push(4); const peek = queue[0]; /* 元素出队 */ +// O(n) const poll = queue.shift(); /* 获取队列的长度 */ From 4c2ec0079f6188792f576f5d39ac137c7d91f147 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 5 Dec 2022 22:55:11 +0800 Subject: [PATCH 13/15] Update queue.ts --- codes/typescript/chapter_stack_and_queue/queue.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/codes/typescript/chapter_stack_and_queue/queue.ts b/codes/typescript/chapter_stack_and_queue/queue.ts index eb7b813..0701a9a 100644 --- a/codes/typescript/chapter_stack_and_queue/queue.ts +++ b/codes/typescript/chapter_stack_and_queue/queue.ts @@ -5,8 +5,8 @@ */ /* 初始化队列 */ -// Typescript 没有内置的队列,可以把 Array 当作队列来使用 -// 注意:虽然Typescript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 +// TypeScript 没有内置的队列,可以把 Array 当作队列来使用 +// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n) const queue: number[] = []; /* 元素入队 */ @@ -20,6 +20,7 @@ queue.push(4); const peek = queue[0]; /* 元素出队 */ +// O(n) const poll = queue.shift(); /* 获取队列的长度 */ @@ -28,4 +29,4 @@ const size = queue.length; /* 判断队列是否为空 */ const empty = queue.length === 0; -export { }; \ No newline at end of file +export { }; From 19401fbca99117f6f0995891531bb0f1325e1f34 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 5 Dec 2022 22:56:38 +0800 Subject: [PATCH 14/15] Update queue.md --- docs/chapter_stack_and_queue/queue.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index e9c9fcb..c270585 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -144,8 +144,8 @@ comments: true ```js title="queue.js" /* 初始化队列 */ - // Javascript 没有内置的队列,可以把 Array 当作队列来使用 - // 注意:虽然Javascript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 + // JavaScript 没有内置的队列,可以把 Array 当作队列来使用 + // 注意:由于是数组,所以 shift() 的时间复杂度是 O(n) const queue = []; /* 元素入队 */ @@ -159,6 +159,7 @@ comments: true const peek = queue[0]; /* 元素出队 */ + // O(n) const poll = queue.shift(); /* 获取队列的长度 */ @@ -172,8 +173,8 @@ comments: true ```typescript title="queue.ts" /* 初始化队列 */ - // Typescript 没有内置的队列,可以把 Array 当作队列来使用 - // 注意:虽然Typescript有shift()函数可以去除队首元素,但是时间复杂度是O(n)的。 + // TypeScript 没有内置的队列,可以把 Array 当作队列来使用 + // 注意:由于是数组,所以 shift() 的时间复杂度是 O(n) const queue: number[] = []; /* 元素入队 */ @@ -187,6 +188,7 @@ comments: true const peek = queue[0]; /* 元素出队 */ + // O(n) const poll = queue.shift(); /* 获取队列的长度 */ From f6ed7bcc95809b54d1ec08d32f2bfb5ed579e802 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 5 Dec 2022 22:57:24 +0800 Subject: [PATCH 15/15] Update queue.js --- codes/javascript/chapter_stack_and_queue/queue.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/javascript/chapter_stack_and_queue/queue.js b/codes/javascript/chapter_stack_and_queue/queue.js index 2ec3d69..5d26727 100644 --- a/codes/javascript/chapter_stack_and_queue/queue.js +++ b/codes/javascript/chapter_stack_and_queue/queue.js @@ -5,7 +5,7 @@ */ /* 初始化队列 */ -// Javascript 没有内置的队列,可以把 Array 当作队列来使用 +// JavaScript 没有内置的队列,可以把 Array 当作队列来使用 // 注意:由于是数组,所以 shift() 的时间复杂度是 O(n) const queue = [];