From 8d2c6f881c8254ba549361c9180453830bef25cb Mon Sep 17 00:00:00 2001 From: Rameez Parwez Date: Fri, 20 Sep 2024 11:31:02 +0530 Subject: [PATCH 01/10] change sub-topic --- content/4_Gold/Sliding_Window.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/4_Gold/Sliding_Window.mdx b/content/4_Gold/Sliding_Window.mdx index db6140de38..521f27e94e 100644 --- a/content/4_Gold/Sliding_Window.mdx +++ b/content/4_Gold/Sliding_Window.mdx @@ -224,7 +224,7 @@ public class test { -## Sliding Window Minimum in $\mathcal{O}(N)$ +## Sliding Window Maximum in $\mathcal{O}(N)$ From 17204145fc42a9caff1a5757ab841eec4b7a514d Mon Sep 17 00:00:00 2001 From: Rameez Parwez Date: Fri, 20 Sep 2024 11:35:48 +0530 Subject: [PATCH 02/10] added prblm --- content/4_Gold/Sliding_Window.problems.json | 12 ++++++++++++ solutions/gold/cses-3221.mdx | 7 +++++++ 2 files changed, 19 insertions(+) create mode 100644 solutions/gold/cses-3221.mdx diff --git a/content/4_Gold/Sliding_Window.problems.json b/content/4_Gold/Sliding_Window.problems.json index 4584b4ec0b..25c35702c0 100644 --- a/content/4_Gold/Sliding_Window.problems.json +++ b/content/4_Gold/Sliding_Window.problems.json @@ -190,6 +190,18 @@ } ], "queue": [ + { + "uniqueId": "cses-3221", + "name": "Sliding Window Minimum", + "url": "https://cses.fi/boi24/task/3221/", + "source": "CSES", + "difficulty": "Easy", + "isStarred": true, + "tags": [], + "solutionMetadata": { + "kind": "internal" + } + }, { "uniqueId": "ys-QueueComposite", "name": "Queue Composite", diff --git a/solutions/gold/cses-3221.mdx b/solutions/gold/cses-3221.mdx new file mode 100644 index 0000000000..bf04ca8200 --- /dev/null +++ b/solutions/gold/cses-3221.mdx @@ -0,0 +1,7 @@ +--- +id: cses-3221 +source: CSES +title: Sliding Window Minimum +author: Rameez Parwez +--- + From d8276ff9989ae05e8679adde7a654de91eb76819 Mon Sep 17 00:00:00 2001 From: Rameez Parwez Date: Fri, 20 Sep 2024 11:47:26 +0530 Subject: [PATCH 03/10] c++ sol for swm --- solutions/gold/cses-3221.mdx | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/solutions/gold/cses-3221.mdx b/solutions/gold/cses-3221.mdx index bf04ca8200..670934d9e3 100644 --- a/solutions/gold/cses-3221.mdx +++ b/solutions/gold/cses-3221.mdx @@ -5,3 +5,52 @@ title: Sliding Window Minimum author: Rameez Parwez --- +[Official Analysis] (https://cp-algorithms.com/data_structures/stack_queue_modification.html#queue-modification-method-1) + +## Implementation + +**Time Complexity**: $\mathcal{O}(N)$ + + + + +```cpp +#include + +int main() { + int n, k; + std::cin >> n >> k; + std::vector arr(n); + for (auto &x : arr) { + std::cin >> x; + } + + std::deque d; + std::vector res; + for (int i = 0; i < k; i++) { + while (!d.empty() && arr[i] < arr[d.back()]) { d.pop_back(); } + d.push_back(i); + } + for (int i = k; i < n; i++) { + res.push_back(arr[d.front()]); + + if (!d.empty() && d.front() == i - k) { + d.pop_front(); + } + + while (!d.empty() and arr[i] <= arr[d.back()]) { + d.pop_back(); + } + d.push_back(i); + } + res.push_back(arr[d.front()]); + + for (int i = 0; i < n - k + 1; i++) { + std::cout << res[i] << " \n"[i == n - k]; + } +} +``` + + + + \ No newline at end of file From 2b7a8c9c65cf25437d18445acb24644cfbc19999 Mon Sep 17 00:00:00 2001 From: Rameez Parwez Date: Fri, 20 Sep 2024 11:54:06 +0530 Subject: [PATCH 04/10] py sol for swm --- solutions/gold/cses-3221.mdx | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/solutions/gold/cses-3221.mdx b/solutions/gold/cses-3221.mdx index 670934d9e3..944b86c71b 100644 --- a/solutions/gold/cses-3221.mdx +++ b/solutions/gold/cses-3221.mdx @@ -53,4 +53,37 @@ int main() { + + +```py +from collections import deque + +n, k = map(int, input().split()) +arr = list(map(int, input().split())) + +d = deque() +res = [] + +for i in range(k): + while d and arr[i] < arr[d[-1]]: + d.pop() + d.append(i) + +for i in range(k, n): + res.append(arr[d[0]]) + + if d and d[0] == i - k: + d.popleft() + + while d and arr[i] <= arr[d[-1]]: + d.pop() + d.append(i) + +res.append(arr[d[0]]) + +print(*res) +``` + + + \ No newline at end of file From 518e9c7b9e7fa2051bb2fa114327e1926da61179 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 20 Sep 2024 06:30:24 +0000 Subject: [PATCH 05/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/gold/cses-3221.mdx | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/solutions/gold/cses-3221.mdx b/solutions/gold/cses-3221.mdx index 944b86c71b..c7130321c1 100644 --- a/solutions/gold/cses-3221.mdx +++ b/solutions/gold/cses-3221.mdx @@ -21,9 +21,7 @@ int main() { int n, k; std::cin >> n >> k; std::vector arr(n); - for (auto &x : arr) { - std::cin >> x; - } + for (auto &x : arr) { std::cin >> x; } std::deque d; std::vector res; @@ -34,20 +32,14 @@ int main() { for (int i = k; i < n; i++) { res.push_back(arr[d.front()]); - if (!d.empty() && d.front() == i - k) { - d.pop_front(); - } + if (!d.empty() && d.front() == i - k) { d.pop_front(); } - while (!d.empty() and arr[i] <= arr[d.back()]) { - d.pop_back(); - } + while (!d.empty() and arr[i] <= arr[d.back()]) { d.pop_back(); } d.push_back(i); } res.push_back(arr[d.front()]); - for (int i = 0; i < n - k + 1; i++) { - std::cout << res[i] << " \n"[i == n - k]; - } + for (int i = 0; i < n - k + 1; i++) { std::cout << res[i] << " \n"[i == n - k]; } } ``` @@ -65,19 +57,19 @@ d = deque() res = [] for i in range(k): - while d and arr[i] < arr[d[-1]]: - d.pop() - d.append(i) + while d and arr[i] < arr[d[-1]]: + d.pop() + d.append(i) for i in range(k, n): - res.append(arr[d[0]]) + res.append(arr[d[0]]) - if d and d[0] == i - k: - d.popleft() + if d and d[0] == i - k: + d.popleft() - while d and arr[i] <= arr[d[-1]]: - d.pop() - d.append(i) + while d and arr[i] <= arr[d[-1]]: + d.pop() + d.append(i) res.append(arr[d[0]]) @@ -86,4 +78,4 @@ print(*res) - \ No newline at end of file + From c7bd939c3413a7b9b3ce26d818cbaa8f0f03df17 Mon Sep 17 00:00:00 2001 From: Rameez Parwez Date: Fri, 20 Sep 2024 12:24:51 +0530 Subject: [PATCH 06/10] imp2 --- solutions/gold/cses-3221.mdx | 41 +++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/solutions/gold/cses-3221.mdx b/solutions/gold/cses-3221.mdx index c7130321c1..4ebb6c7594 100644 --- a/solutions/gold/cses-3221.mdx +++ b/solutions/gold/cses-3221.mdx @@ -7,7 +7,7 @@ author: Rameez Parwez [Official Analysis] (https://cp-algorithms.com/data_structures/stack_queue_modification.html#queue-modification-method-1) -## Implementation +## Implementation 1 - Deque **Time Complexity**: $\mathcal{O}(N)$ @@ -79,3 +79,42 @@ print(*res) + + +## Implementation 2 - Multiset + +**Time Complexity**: $\mathcal{O}(N\log N)$ + + + + + +```cpp +#include + +int main() { + int n, k; + std::cin >> n >> k; + std::vector arr(n); + for (auto &x : arr) { + std::cin >> x; + } + + std::multiset mset; + std::vector res; + for (int i = 0; i < n; i++) { + mset.insert(arr[i]); + if (i >= k - 1) { + res.push_back(*mset.begin()); + mset.erase(mset.find(arr[i - k + 1])); + } + } + + for (int i = 0; i < n - k + 1; i++) { + std::cout << res[i] << " \n"[i == n - k]; + } +} +``` + + + \ No newline at end of file From e203fe2d7e014a117d92000a24126b6a20a94942 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 20 Sep 2024 06:57:56 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/gold/cses-3221.mdx | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/solutions/gold/cses-3221.mdx b/solutions/gold/cses-3221.mdx index 4ebb6c7594..90a695465a 100644 --- a/solutions/gold/cses-3221.mdx +++ b/solutions/gold/cses-3221.mdx @@ -91,15 +91,13 @@ print(*res) ```cpp #include - + int main() { int n, k; std::cin >> n >> k; std::vector arr(n); - for (auto &x : arr) { - std::cin >> x; - } - + for (auto &x : arr) { std::cin >> x; } + std::multiset mset; std::vector res; for (int i = 0; i < n; i++) { @@ -109,12 +107,10 @@ int main() { mset.erase(mset.find(arr[i - k + 1])); } } - - for (int i = 0; i < n - k + 1; i++) { - std::cout << res[i] << " \n"[i == n - k]; - } + + for (int i = 0; i < n - k + 1; i++) { std::cout << res[i] << " \n"[i == n - k]; } } ``` - \ No newline at end of file + From dc1100d9fc9dc49cb583920f8d6c0b5bab3d559e Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+freakin23@users.noreply.github.com> Date: Fri, 20 Sep 2024 21:13:24 +0530 Subject: [PATCH 08/10] Update cses-3221.mdx --- solutions/gold/cses-3221.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/gold/cses-3221.mdx b/solutions/gold/cses-3221.mdx index 90a695465a..1306dcb590 100644 --- a/solutions/gold/cses-3221.mdx +++ b/solutions/gold/cses-3221.mdx @@ -5,7 +5,7 @@ title: Sliding Window Minimum author: Rameez Parwez --- -[Official Analysis] (https://cp-algorithms.com/data_structures/stack_queue_modification.html#queue-modification-method-1) +[Unofficial Analysis] (https://cp-algorithms.com/data_structures/stack_queue_modification.html#queue-modification-method-1) ## Implementation 1 - Deque From 7ce55bc575c4b146f66951b099dfc9a8360fa7d8 Mon Sep 17 00:00:00 2001 From: Rameez Parwez Date: Sat, 21 Sep 2024 16:37:02 +0530 Subject: [PATCH 09/10] revert --- content/4_Gold/Sliding_Window.problems.json | 12 -- solutions/gold/cses-3221.mdx | 120 -------------------- 2 files changed, 132 deletions(-) delete mode 100644 solutions/gold/cses-3221.mdx diff --git a/content/4_Gold/Sliding_Window.problems.json b/content/4_Gold/Sliding_Window.problems.json index 25c35702c0..4584b4ec0b 100644 --- a/content/4_Gold/Sliding_Window.problems.json +++ b/content/4_Gold/Sliding_Window.problems.json @@ -190,18 +190,6 @@ } ], "queue": [ - { - "uniqueId": "cses-3221", - "name": "Sliding Window Minimum", - "url": "https://cses.fi/boi24/task/3221/", - "source": "CSES", - "difficulty": "Easy", - "isStarred": true, - "tags": [], - "solutionMetadata": { - "kind": "internal" - } - }, { "uniqueId": "ys-QueueComposite", "name": "Queue Composite", diff --git a/solutions/gold/cses-3221.mdx b/solutions/gold/cses-3221.mdx deleted file mode 100644 index 4ebb6c7594..0000000000 --- a/solutions/gold/cses-3221.mdx +++ /dev/null @@ -1,120 +0,0 @@ ---- -id: cses-3221 -source: CSES -title: Sliding Window Minimum -author: Rameez Parwez ---- - -[Official Analysis] (https://cp-algorithms.com/data_structures/stack_queue_modification.html#queue-modification-method-1) - -## Implementation 1 - Deque - -**Time Complexity**: $\mathcal{O}(N)$ - - - - -```cpp -#include - -int main() { - int n, k; - std::cin >> n >> k; - std::vector arr(n); - for (auto &x : arr) { std::cin >> x; } - - std::deque d; - std::vector res; - for (int i = 0; i < k; i++) { - while (!d.empty() && arr[i] < arr[d.back()]) { d.pop_back(); } - d.push_back(i); - } - for (int i = k; i < n; i++) { - res.push_back(arr[d.front()]); - - if (!d.empty() && d.front() == i - k) { d.pop_front(); } - - while (!d.empty() and arr[i] <= arr[d.back()]) { d.pop_back(); } - d.push_back(i); - } - res.push_back(arr[d.front()]); - - for (int i = 0; i < n - k + 1; i++) { std::cout << res[i] << " \n"[i == n - k]; } -} -``` - - - - - -```py -from collections import deque - -n, k = map(int, input().split()) -arr = list(map(int, input().split())) - -d = deque() -res = [] - -for i in range(k): - while d and arr[i] < arr[d[-1]]: - d.pop() - d.append(i) - -for i in range(k, n): - res.append(arr[d[0]]) - - if d and d[0] == i - k: - d.popleft() - - while d and arr[i] <= arr[d[-1]]: - d.pop() - d.append(i) - -res.append(arr[d[0]]) - -print(*res) -``` - - - - - - -## Implementation 2 - Multiset - -**Time Complexity**: $\mathcal{O}(N\log N)$ - - - - - -```cpp -#include - -int main() { - int n, k; - std::cin >> n >> k; - std::vector arr(n); - for (auto &x : arr) { - std::cin >> x; - } - - std::multiset mset; - std::vector res; - for (int i = 0; i < n; i++) { - mset.insert(arr[i]); - if (i >= k - 1) { - res.push_back(*mset.begin()); - mset.erase(mset.find(arr[i - k + 1])); - } - } - - for (int i = 0; i < n - k + 1; i++) { - std::cout << res[i] << " \n"[i == n - k]; - } -} -``` - - - \ No newline at end of file From f1646d9a7659e87ede38614e12a58149bdbc974a Mon Sep 17 00:00:00 2001 From: Rameez Parwez Date: Sat, 21 Sep 2024 16:49:49 +0530 Subject: [PATCH 10/10] removed cses-3221 --- solutions/gold/cses-3221.mdx | 116 ----------------------------------- 1 file changed, 116 deletions(-) delete mode 100644 solutions/gold/cses-3221.mdx diff --git a/solutions/gold/cses-3221.mdx b/solutions/gold/cses-3221.mdx deleted file mode 100644 index 1306dcb590..0000000000 --- a/solutions/gold/cses-3221.mdx +++ /dev/null @@ -1,116 +0,0 @@ ---- -id: cses-3221 -source: CSES -title: Sliding Window Minimum -author: Rameez Parwez ---- - -[Unofficial Analysis] (https://cp-algorithms.com/data_structures/stack_queue_modification.html#queue-modification-method-1) - -## Implementation 1 - Deque - -**Time Complexity**: $\mathcal{O}(N)$ - - - - -```cpp -#include - -int main() { - int n, k; - std::cin >> n >> k; - std::vector arr(n); - for (auto &x : arr) { std::cin >> x; } - - std::deque d; - std::vector res; - for (int i = 0; i < k; i++) { - while (!d.empty() && arr[i] < arr[d.back()]) { d.pop_back(); } - d.push_back(i); - } - for (int i = k; i < n; i++) { - res.push_back(arr[d.front()]); - - if (!d.empty() && d.front() == i - k) { d.pop_front(); } - - while (!d.empty() and arr[i] <= arr[d.back()]) { d.pop_back(); } - d.push_back(i); - } - res.push_back(arr[d.front()]); - - for (int i = 0; i < n - k + 1; i++) { std::cout << res[i] << " \n"[i == n - k]; } -} -``` - - - - - -```py -from collections import deque - -n, k = map(int, input().split()) -arr = list(map(int, input().split())) - -d = deque() -res = [] - -for i in range(k): - while d and arr[i] < arr[d[-1]]: - d.pop() - d.append(i) - -for i in range(k, n): - res.append(arr[d[0]]) - - if d and d[0] == i - k: - d.popleft() - - while d and arr[i] <= arr[d[-1]]: - d.pop() - d.append(i) - -res.append(arr[d[0]]) - -print(*res) -``` - - - - - - -## Implementation 2 - Multiset - -**Time Complexity**: $\mathcal{O}(N\log N)$ - - - - - -```cpp -#include - -int main() { - int n, k; - std::cin >> n >> k; - std::vector arr(n); - for (auto &x : arr) { std::cin >> x; } - - std::multiset mset; - std::vector res; - for (int i = 0; i < n; i++) { - mset.insert(arr[i]); - if (i >= k - 1) { - res.push_back(*mset.begin()); - mset.erase(mset.find(arr[i - k + 1])); - } - } - - for (int i = 0; i < n - k + 1; i++) { std::cout << res[i] << " \n"[i == n - k]; } -} -``` - - -