|
| 1 | +### Sliding Window Approach |
| 2 | + |
| 3 | + |
| 4 | +``` |
| 5 | +1. Fixed window length k |
| 6 | +2. dynamic |
| 7 | +3. dynamic with hashmap |
| 8 | +4. max update during window increase, |
| 9 | + min update during window size decrease |
| 10 | +``` |
| 11 | + |
| 12 | +- **Longest Continuous Increasing Subsequence** | LeetCode 674. |
| 13 | + |
| 14 | +``` |
| 15 | +class Solution: |
| 16 | + def findLengthOfLCIS(self, nums: List[int]) -> int: |
| 17 | + res = left = 0 |
| 18 | + for right in range(len(nums)): |
| 19 | + if right and nums[right]<=nums[right-1]: |
| 20 | + left = right |
| 21 | + res = max(res, right - left + 1) |
| 22 | + return [res] |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | +- **Permutation in String** | LeetCode 567. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +``` |
| 31 | +# the nice thing about this pieces of code is that it represents \ |
| 32 | +# fix window_size sliding window technique. |
| 33 | +
|
| 34 | +class Solution: |
| 35 | + def checkInclusion(self, s1: str, s2: str) -> bool: |
| 36 | + l1 = len(s1) |
| 37 | + l2 = len(s2) |
| 38 | + if s1 == s2: |
| 39 | + return True |
| 40 | + if l1 > l2: |
| 41 | + return False |
| 42 | +
|
| 43 | + dict1 = dict(zip(list('abcdefghijklmnopqrstuvwxyz'), [0]*26)) |
| 44 | + dict2 = dict(zip(list('abcdefghijklmnopqrstuvwxyz'), [0]*26)) |
| 45 | +
|
| 46 | + for i in range(l1): |
| 47 | + dict1[s1[i]] += 1 |
| 48 | + dict2[s2[i]] += 1 |
| 49 | + |
| 50 | + if dict1 == dict2: |
| 51 | + return True |
| 52 | + |
| 53 | + for i in range(l2 - l1): |
| 54 | + dict2[s2[i]] -= 1 |
| 55 | + dict2[s2[i + l1]] += 1 |
| 56 | + if dict2 == dict1: |
| 57 | + return True |
| 58 | + return False |
| 59 | +``` |
| 60 | + |
| 61 | +- Maximum Average Subarray I | 643. |
| 62 | +``` |
| 63 | +# 定长的先处理0-k-1,在处理k-length-1,用 for range 处理 |
| 64 | +
|
| 65 | +class Solution: |
| 66 | + def findMaxAverage(self, nums: List[int], k: int) -> float: |
| 67 | + res = 0.0 |
| 68 | + for i in range(k): |
| 69 | + res += nums[i] |
| 70 | + max_ = res |
| 71 | + for i in range(k, len(nums)): |
| 72 | + res += nums[i]-nums[i-k] |
| 73 | + max_ = max(max_,res) |
| 74 | +
|
| 75 | + return max_/k |
| 76 | +``` |
| 77 | +- [https://medium.com/@zengruiwang/sliding-window-technique-360d840d5740](https://medium.com/@zengruiwang/sliding-window-technique-360d840d5740) |
| 78 | +- [https://www.youtube.com/watch?v=MK-NZ4hN7rs](https://www.youtube.com/watch?v=MK-NZ4hN7rs) |
| 79 | + |
| 80 | + |
| 81 | + |
1 | 82 | ## 219. 存在重复元素 II
|
2 | 83 |
|
3 | 84 | [原题链接](https://leetcode-cn.com/problems/contains-duplicate-ii/)
|
@@ -39,6 +120,8 @@ class Solution(object):
|
39 | 120 |
|
40 | 121 | 不断对比得出每个窗口的最大值。
|
41 | 122 |
|
| 123 | + |
| 124 | + |
42 | 125 | ```python
|
43 | 126 | class Solution:
|
44 | 127 | def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
|
@@ -212,4 +295,4 @@ class Solution:
|
212 | 295 | ```
|
213 | 296 |
|
214 | 297 | - 时间复杂度:$O(l1 + (l2 - l1))$($l1$ 为字符串 s1 长度,$l2$ 为字符串 s2 长度)
|
215 |
| -- 空间复杂度:$O(1)$ |
| 298 | +- 空间复杂度:$O(1)$ |
0 commit comments