This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/4_Silver/Sliding.mdx

86 lines
3.8 KiB
Text
Raw Normal View History

2020-06-04 01:42:57 +00:00
---
2020-07-06 16:37:04 +00:00
id: sliding
title: "Sliding Window"
author: Darren Yao?, Benjamin Qi
2020-06-05 01:42:25 +00:00
prerequisites:
2020-06-23 17:27:41 +00:00
- Silver - Stacks & Queues
2020-07-06 21:21:40 +00:00
- Silver - Introduction to Ordered Maps & Sets
2020-06-23 17:27:41 +00:00
description: "?"
2020-06-26 18:00:32 +00:00
frequency: 2
2020-06-04 01:42:57 +00:00
---
2020-06-24 22:50:59 +00:00
import { Problem } from "../models";
export const metadata = {
problems: {
sample: [
new Problem("CSES", "Sum of Two Values", "1141", "Easy", false, []),
],
2020-07-06 16:37:04 +00:00
slide: [
new Problem("CSES", "Playlist", "1141", "Easy", false, [],"classic example of 2P"),
],
2020-06-24 22:50:59 +00:00
general: [
2020-07-06 21:21:40 +00:00
new Problem("CSES", "Subarray Sums I", "1660", "Easy", false, [], ""),
new Problem("CSES", "Sliding Median", "1076", "Easy", false, [], ""),
new Problem("CSES", "Sliding Cost", "1077", "Hard", false, [], ""),
new Problem("CSES", "Max Subarray Sum II", "1644", "Normal", false, [], ""),
2020-06-28 16:03:46 +00:00
new Problem("Silver", "Diamond Collector", "643", "Easy", false, ["2P", "Sorting"], ""),
2020-06-24 22:50:59 +00:00
new Problem("Silver", "Paired Up", "738", "Normal", false, ["2P", "Sorting"]),
new Problem("Gold", "Haybale Feast", "767", "Normal", false, ["Set", "Sliding Window"]),
new Problem("CF", "Books", "problemset/problem/279/B", "Normal", false, []),
new Problem("CF", "Cellular Network", "problemset/problem/702/C", "Normal", false, []),
new Problem("CF", "USB vs. PS/2", "problemset/problem/762/B", "Normal", false, []),
new Problem("CF", "K-Good Segment", "problemset/problem/616/D", "Normal", false, []),
new Problem("CF", "Garland", "problemset/problem/814/C", "Normal", false, []),
new Problem("CF", "Jury Meeting", "problemset/problem/853/B", "Normal", false, []),
2020-07-06 16:37:04 +00:00
new Problem("Plat", "Fort Moo", "600", "Very Hard", false, ["Sliding Window"]),
2020-06-24 22:50:59 +00:00
],
2020-06-25 04:00:28 +00:00
qs: [
2020-07-06 16:37:04 +00:00
new Problem("YS","Queue Composite","queue_operate_all_composite","Hard",true,[],""),
2020-06-25 04:00:28 +00:00
],
2020-06-24 22:50:59 +00:00
}
};
2020-06-23 17:27:41 +00:00
## Two Pointers
2020-06-08 08:49:17 +00:00
2020-07-06 16:37:04 +00:00
<problems-list problems={metadata.problems.sample} />
2020-06-23 17:27:41 +00:00
Two pointers refers to iterating two monotonic pointers across an array to search for a pair of indices satisfying some condition in linear time.
2020-06-08 08:49:17 +00:00
2020-06-28 16:03:46 +00:00
<resources>
2020-07-06 16:37:04 +00:00
<resource source="CPH" title="8.1 - Two Pointers"></resource>
<resource source="IUSACO" title="14.1 - Two Pointers"></resource>
2020-06-28 16:03:46 +00:00
</resources>
2020-06-23 17:27:41 +00:00
2020-07-06 16:37:04 +00:00
## Sliding Window
2020-06-23 17:27:41 +00:00
2020-07-06 16:37:04 +00:00
<problems-list problems={metadata.problems.slide} />
2020-06-23 17:27:41 +00:00
2020-07-06 16:37:04 +00:00
(considered the same or a generalization of two pointers?)
2020-06-23 17:27:41 +00:00
2020-07-06 16:37:04 +00:00
Let's envision a sliding window (or constant size subarray) of size $K$ moving left to right along an array, $a$. For each position of the window, we want to compute some information.
2020-06-23 17:27:41 +00:00
2020-07-06 16:37:04 +00:00
Let's store a `std::set` of integers representing the integers inside the window. If the window currently spans the range $i \dots j$, we observe that moving the range forward to $i+1 \dots j+1$ only removes $a_i$ and adds $a_{j+1}$ to the window. We can support these two operations and query for the minimum / maximum in the set in $O(\log N)$.
2020-06-08 18:07:48 +00:00
2020-07-06 16:37:04 +00:00
To compute the sum in the range, instead of using a set, we can store a variable $s$ representing the sum. As we move the window forward, we update $s$ by subtracting $a_i$ from $s$ and adding $a_{j+1}$ to $s$.
2020-06-23 03:28:43 +00:00
2020-07-06 16:37:04 +00:00
<resources>
<resource source="Medium" title="Introduction to Sliding Window Algorithms" url="https://levelup.gitconnected.com/an-introduction-to-sliding-window-algorithms-5533c4fe1cc7"> </resource>
<resource source="GFG" title="Window Sliding Technique" url="window-sliding-technique"> </resource>
</resources>
2020-06-23 03:28:43 +00:00
2020-07-06 16:37:04 +00:00
### Problems
2020-06-25 04:00:28 +00:00
2020-07-06 16:37:04 +00:00
<problems-list problems={metadata.problems.general} />
2020-06-23 17:27:41 +00:00
2020-07-06 16:37:04 +00:00
## Sliding Window Minimum in $O(N)$
2020-06-23 17:27:41 +00:00
2020-07-06 16:37:04 +00:00
<resources>
<resource source="cp-algo" title="Minimum stack / Minimum queue" url="data_structures/stack_queue_modification.html" starred>Mentions two ways to solve this (both are important)!</resource>
</resources>
2020-06-23 17:27:41 +00:00
2020-07-06 16:37:04 +00:00
In particular, the second method allows us to solve the following generalization in linear time as well:
2020-06-23 17:27:41 +00:00
2020-07-06 16:37:04 +00:00
<problems-list problems={metadata.problems.qs} />