mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 13:12:44 +00:00
docs(content): revamp sliding-window examples and purge empty appendix pages
This commit is contained in:
1 parent
937266b629
commit
0e9e06eb8d
38 files changed
+39
-419
No files matched your search
@@ -48,18 +48,14 @@ The window size never changes. The main task is updating the window state effici
|
||||
|
||||
|
||||
```python
|
||||
def fixed_window(nums, k):
|
||||
window_sum = 0
|
||||
best = float('-inf')
|
||||
|
||||
for right in range(len(nums)):
|
||||
window_sum += nums[right] # add the new element
|
||||
|
||||
if right >= k - 1: # window is full
|
||||
best = max(best, window_sum) # record answer
|
||||
window_sum -= nums[right - k + 1] # remove the oldest element
|
||||
|
||||
return best
|
||||
def sliding_window_fixed(input, window_size):
|
||||
ans = window = input[0:window_size]
|
||||
for right in range(window_size, len(input)):
|
||||
left = right - window_size
|
||||
remove input[left] from window
|
||||
append input[right] to window
|
||||
ans = optimal(ans, window)
|
||||
return ans
|
||||
```
|
||||
|
||||
<Steps>
|
||||
@@ -87,24 +83,43 @@ Here the window expands until invalid, then shrinks until valid again.
|
||||
|
||||
</Frame>
|
||||
|
||||
|
||||
<Tabs inline>
|
||||
<Tab title="Longest Window">
|
||||
|
||||
```python
|
||||
def flexible_window(nums, target):
|
||||
def sliding_window_flexible_longest(input):
|
||||
initialize window, ans
|
||||
left = 0
|
||||
window_sum = 0
|
||||
best = 0
|
||||
|
||||
for right in range(len(nums)):
|
||||
window_sum += nums[right] # grow the window
|
||||
|
||||
while window_sum > target: # window is invalid
|
||||
window_sum -= nums[left] # shrink from the left
|
||||
for right in range(len(input)):
|
||||
append input[right] to window
|
||||
while invalid(window): # update left until window is valid again
|
||||
remove input[left] from window
|
||||
left += 1
|
||||
ans = max(ans, window) # window is guaranteed to be valid here
|
||||
return ans
|
||||
|
||||
best = max(best, right - left + 1) # window is valid, record answer
|
||||
|
||||
return best
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab title="Shortest Window">
|
||||
|
||||
```python
|
||||
def sliding_window_flexible_shortest(input):
|
||||
initialize window, ans
|
||||
left = 0
|
||||
for right in range(len(input)):
|
||||
append input[right] to window
|
||||
while valid(window):
|
||||
ans = min(ans, window) # window is guaranteed to be valid here
|
||||
remove input[left] from window
|
||||
left += 1
|
||||
return ans
|
||||
```
|
||||
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
<Steps>
|
||||
<Step title="">
|
||||
Add `nums[right]` to the window.
|
||||
@@ -119,26 +134,6 @@ def flexible_window(nums, target):
|
||||
|
||||
---
|
||||
|
||||
For "shortest window" problems, swap the logic: use `while` for the *valid* case, record the answer inside the loop, then shrink.
|
||||
|
||||
```python
|
||||
def shortest_valid_window(nums, target):
|
||||
left = 0
|
||||
window_sum = 0
|
||||
best = float('inf')
|
||||
|
||||
for right in range(len(nums)):
|
||||
window_sum += nums[right]
|
||||
|
||||
while window_sum >= target: # window is valid
|
||||
best = min(best, right - left + 1) # record, then try to shrink
|
||||
window_sum -= nums[left]
|
||||
left += 1
|
||||
|
||||
return best if best != float('inf') else 0
|
||||
|
||||
```
|
||||
|
||||
## 3. Frequency-tracking windows
|
||||
|
||||
:::tip
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Array"
|
||||
list: "https://leetcode.com/problem-list/array/"
|
||||
icon: list-ordered
|
||||
sidebar:
|
||||
badge: '48'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Array.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Binary Tree"
|
||||
list: "https://leetcode.com/problem-list/binary-tree/"
|
||||
icon: git-branch
|
||||
sidebar:
|
||||
badge: '9'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Binary Tree.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Directed Acyclic Graph"
|
||||
list: "https://leetcode.com/problem-list/directed-acyclic-graph/"
|
||||
icon: route
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Directed Acyclic Graph.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Doubly-Linked List"
|
||||
list: "https://leetcode.com/problem-list/doubly-linked-list/"
|
||||
icon: link-2
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Doubly-Linked List.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Graph Theory"
|
||||
list: "https://leetcode.com/problem-list/graph/"
|
||||
icon: share-2
|
||||
sidebar:
|
||||
badge: '5'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Graph Theory.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "K-D Tree"
|
||||
list: "https://leetcode.com/problem-list/k-d-tree/"
|
||||
icon: axis-3d
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged K-D Tree.
|
||||
:::
|
||||
@@ -1,3 +0,0 @@
|
||||
import { defineMeta } from "blume";
|
||||
|
||||
export default defineMeta({ title: "Data Structures", order: 1 });
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Queue"
|
||||
list: "https://leetcode.com/problem-list/queue/"
|
||||
icon: list-end
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Queue.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "String"
|
||||
list: "https://leetcode.com/problem-list/string/"
|
||||
icon: quote
|
||||
sidebar:
|
||||
badge: '18'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged String.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Tree"
|
||||
list: "https://leetcode.com/problem-list/tree/"
|
||||
icon: tree-deciduous
|
||||
sidebar:
|
||||
badge: '9'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Tree.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Bidirectional Search"
|
||||
list: "https://leetcode.com/problem-list/bidirectional-search/"
|
||||
icon: move-horizontal
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Bidirectional Search.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Binary Lifting"
|
||||
list: "https://leetcode.com/problem-list/binary-lifting/"
|
||||
icon: chevrons-up
|
||||
sidebar:
|
||||
badge: '2'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Binary Lifting.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Bit Manipulation"
|
||||
list: "https://leetcode.com/problem-list/bit-manipulation/"
|
||||
icon: binary
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Bit Manipulation.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Bracket Sequences"
|
||||
list: "https://leetcode.com/problem-list/bracket-sequences/"
|
||||
icon: parentheses
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Bracket Sequences.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Bucket Sort"
|
||||
list: "https://leetcode.com/problem-list/bucket-sort/"
|
||||
icon: container
|
||||
sidebar:
|
||||
badge: '2'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Bucket Sort.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Complete Knapsack"
|
||||
list: "https://leetcode.com/problem-list/complete-knapsack/"
|
||||
icon: package-plus
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Complete Knapsack.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Divide and Conquer"
|
||||
list: "https://leetcode.com/problem-list/divide-and-conquer/"
|
||||
icon: split
|
||||
sidebar:
|
||||
badge: '5'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Divide and Conquer.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Floyd's Cycle Finding Algorithm"
|
||||
list: "https://leetcode.com/problem-list/floyds-cycle-finding-algorithm/"
|
||||
icon: repeat
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Floyd's Cycle Finding Algorithm.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Knapsack Problem"
|
||||
list: "https://leetcode.com/problem-list/knapsack-problem/"
|
||||
icon: backpack
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Knapsack Problem.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Longest Common Subsequence"
|
||||
list: "https://leetcode.com/problem-list/longest-common-subsequence/"
|
||||
icon: diff
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Longest Common Subsequence.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Lowest Common Ancestor"
|
||||
list: "https://leetcode.com/problem-list/lowest-common-ancestor/"
|
||||
icon: git-merge
|
||||
sidebar:
|
||||
badge: '2'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Lowest Common Ancestor.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Memoization"
|
||||
list: "https://leetcode.com/problem-list/memoization/"
|
||||
icon: database
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Memoization.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Merge Sort"
|
||||
list: "https://leetcode.com/problem-list/merge-sort/"
|
||||
icon: blend
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Merge Sort.
|
||||
:::
|
||||
@@ -1,3 +0,0 @@
|
||||
import { defineMeta } from "blume";
|
||||
|
||||
export default defineMeta({ title: "Algorithms", order: 2 });
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Quickselect"
|
||||
list: "https://leetcode.com/problem-list/quickselect/"
|
||||
icon: crosshair
|
||||
sidebar:
|
||||
badge: '3'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Quickselect.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Quicksort"
|
||||
icon: shuffle
|
||||
list: "https://leetcode.com/problem-list/quicksort/"
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Quicksort.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Recursion"
|
||||
list: "https://leetcode.com/problem-list/recursion/"
|
||||
icon: iteration-ccw
|
||||
sidebar:
|
||||
badge: '3'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Recursion.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Simulation"
|
||||
list: "https://leetcode.com/problem-list/simulation/"
|
||||
icon: monitor-play
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Simulation.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Sorting"
|
||||
list: "https://leetcode.com/problem-list/sorting/"
|
||||
icon: arrow-down-wide-narrow
|
||||
sidebar:
|
||||
badge: '12'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Sorting.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Tournament Sort"
|
||||
list: "https://leetcode.com/problem-list/tournament-sort/"
|
||||
icon: trophy
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Tournament Sort.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Combinatorics"
|
||||
list: "https://leetcode.com/problem-list/combinatorics/"
|
||||
icon: dices
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Combinatorics.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Counting"
|
||||
list: "https://leetcode.com/problem-list/counting/"
|
||||
icon: tally-5
|
||||
sidebar:
|
||||
badge: '3'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Counting.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Design"
|
||||
list: "https://leetcode.com/problem-list/design/"
|
||||
icon: pencil-ruler
|
||||
sidebar:
|
||||
badge: '5'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Design.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Geometry"
|
||||
list: "https://leetcode.com/problem-list/geometry/"
|
||||
icon: shapes
|
||||
sidebar:
|
||||
badge: '1'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Geometry.
|
||||
:::
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
title: "Math"
|
||||
list: "https://leetcode.com/problem-list/math/"
|
||||
icon: calculator
|
||||
sidebar:
|
||||
badge: '7'
|
||||
---
|
||||
|
||||
:::note
|
||||
This guide has not been written yet. The LeetCode list above has every problem tagged Math.
|
||||
:::
|
||||
@@ -1,3 +0,0 @@
|
||||
import { defineMeta } from "blume";
|
||||
|
||||
export default defineMeta({ title: "Techniques", order: 3 });
|
||||
@@ -1,3 +0,0 @@
|
||||
import { defineMeta } from "blume";
|
||||
|
||||
export default defineMeta({ title: "Appendix", icon: "library", order: 0, display: "page" });
|
||||
Reference in new issue
Block a user