Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typo #4792

Merged
merged 11 commits into from
Sep 21, 2024
Merged

typo #4792

Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/4_Gold/Sliding_Window.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public class test {

<Problems problems="general" />

## Sliding Window Minimum in $\mathcal{O}(N)$
## Sliding Window Maximum in $\mathcal{O}(N)$

<FocusProblem problem="constantSam" />

Expand Down
12 changes: 12 additions & 0 deletions content/4_Gold/Sliding_Window.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
SansPapyrus683 marked this conversation as resolved.
Show resolved Hide resolved
{
"uniqueId": "ys-QueueComposite",
"name": "Queue Composite",
Expand Down
116 changes: 116 additions & 0 deletions solutions/gold/cses-3221.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
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)$

<LanguageSection>

<CPPSection>
```cpp
#include <bits/stdc++.h>

int main() {
int n, k;
std::cin >> n >> k;
std::vector<int> arr(n);
for (auto &x : arr) { std::cin >> x; }

std::deque<int> d;
std::vector<int> 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]; }
}
```

</CPPSection>

<PySection>

```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)
```

</PySection>

</LanguageSection>


## Implementation 2 - Multiset

**Time Complexity**: $\mathcal{O}(N\log N)$

<LanguageSection>

<CPPSection>

```cpp
#include <bits/stdc++.h>

int main() {
int n, k;
std::cin >> n >> k;
std::vector<int> arr(n);
for (auto &x : arr) { std::cin >> x; }

std::multiset<int> mset;
std::vector<int> 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]; }
}
```
</CPPSection>

</LanguageSection>
Loading