Skip to content

Commit f5f167f

Browse files
committed
update sliding window pattern
1 parent ecca603 commit f5f167f

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/data/patterns.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
},
1717
{
1818
"title": "Sliding Window",
19-
"description": "Two major forms of Sliding Window exist: (1) Fixed-size window (size = k), used for sums/averages and constant-length subarrays; and (2) Dynamic (variable-size) window, used when the window expands and shrinks based on a condition (e.g., character frequency, distinct characters, constraints).",
19+
"description": "Sliding Window is a technique to efficiently handle contiguous subarrays or substrings. Two major forms exist: (1) Fixed-size window (size = k), used for sums/averages and constant-length subarrays; (2) Dynamic (variable-size) window, used when the window expands and shrinks based on a condition (e.g., character frequency, distinct characters, constraints). For binary arrays or situations where only one counter is needed, an optimized dynamic window without a hashmap can be used.",
2020
"templates": {
21-
"python": "# ---------------------------\n# Version 1: Fixed-Size Window (size = k)\n# ---------------------------\nleft = 0\nwindow_sum = 0\nbest = 0\n\nfor right in range(len(arr)):\n window_sum += arr[right]\n\n # When window reaches size k\n if right - left + 1 == k:\n best = max(best, window_sum)\n window_sum -= arr[left]\n left += 1\n\n\n# ---------------------------\n# Version 2: Dynamic Window (variable size)\n# ---------------------------\nleft = 0\nmax_len = 0\ncount = {}\n\nfor right in range(len(s)):\n char = s[right]\n count[char] = count.get(char, 0) + 1\n\n # Shrink window while it violates a condition\n while window_invalid:\n count[s[left]] -= 1\n if count[s[left]] == 0:\n del count[s[left]]\n left += 1\n\n max_len = max(max_len, right - left + 1)",
22-
"javascript": "// ---------------------------\n// Version 1: Fixed-Size Window (size = k)\n// ---------------------------\nlet left = 0;\nlet windowSum = 0;\nlet best = 0;\n\nfor (let right = 0; right < arr.length; right++) {\n windowSum += arr[right];\n\n if (right - left + 1 === k) {\n best = Math.max(best, windowSum);\n windowSum -= arr[left];\n left++;\n }\n}\n\n\n// ---------------------------\n// Version 2: Dynamic Window (variable size)\n// ---------------------------\nlet left2 = 0;\nlet maxLen = 0;\nlet count = {};\n\nfor (let right = 0; right < s.length; right++) {\n const char = s[right];\n count[char] = (count[char] || 0) + 1;\n\n while (windowInvalid) {\n count[s[left2]]--;\n if (count[s[left2]] === 0) delete count[s[left2]];\n left2++;\n }\n\n maxLen = Math.max(maxLen, right - left2 + 1);\n}",
23-
"java": "// ---------------------------\n// Version 1: Fixed-Size Window (size = k)\n// ---------------------------\nint left = 0;\nint windowSum = 0;\nint best = 0;\n\nfor (int right = 0; right < arr.length; right++) {\n windowSum += arr[right];\n\n if (right - left + 1 == k) {\n best = Math.max(best, windowSum);\n windowSum -= arr[left];\n left++;\n }\n}\n\n\n// ---------------------------\n// Version 2: Dynamic Window (variable size)\n// ---------------------------\nint left2 = 0;\nint maxLen = 0;\nMap<Character, Integer> count = new HashMap<>();\n\nfor (int right = 0; right < s.length(); right++) {\n char c = s.charAt(right);\n count.put(c, count.getOrDefault(c, 0) + 1);\n\n while (windowInvalid) {\n char lc = s.charAt(left2);\n count.put(lc, count.get(lc) - 1);\n if (count.get(lc) == 0) count.remove(lc);\n left2++;\n }\n\n maxLen = Math.max(maxLen, right - left2 + 1);\n}",
24-
"go": "// ---------------------------\n// Version 1: Fixed-Size Window (size = k)\n// ---------------------------\nleft := 0\nwindowSum := 0\nbest := 0\n\nfor right := 0; right < len(arr); right++ {\n windowSum += arr[right]\n\n if right-left+1 == k {\n if windowSum > best {\n best = windowSum\n }\n windowSum -= arr[left]\n left++\n }\n}\n\n\n// ---------------------------\n// Version 2: Dynamic Window (variable size)\n// ---------------------------\nleft2 := 0\nmaxLen := 0\ncount := make(map[rune]int)\n\nfor right, char := range s {\n count[char]++\n\n for windowInvalid {\n leftChar := rune(s[left2])\n count[leftChar]--\n if count[leftChar] == 0 {\n delete(count, leftChar)\n }\n left2++\n }\n\n if right-left2+1 > maxLen {\n maxLen = right-left2+1\n }\n}"
21+
"python": "# ---------------------------\n# Version 1: Fixed-Size Window (size = k)\n# ---------------------------\nleft = 0\nwindow_sum = 0\nbest = 0\n\nfor right in range(len(arr)):\n window_sum += arr[right]\n\n # When window reaches size k\n if right - left + 1 == k:\n best = max(best, window_sum)\n window_sum -= arr[left]\n left += 1\n\n\n# ---------------------------\n# Version 2: Dynamic Window (variable size, with hashmap)\n# ---------------------------\nleft = 0\nmax_len = 0\ncount = {}\n\nfor right in range(len(s)):\n char = s[right]\n count[char] = count.get(char, 0) + 1\n\n # Shrink window while it violates a condition\n while window_invalid:\n count[s[left]] -= 1\n if count[s[left]] == 0:\n del count[s[left]]\n left += 1\n\n max_len = max(max_len, right - left + 1)\n\n\n# ---------------------------\n# Version 3: Dynamic Window (optimized counters, no hashmap)\n# ---------------------------\nleft = 0\nmax_len = 0\ncounter = 0 # counts \"bad\" elements in the window (e.g., zeros)\n\nfor right in range(len(nums)):\n if nums[right] == bad_element: # e.g., 0 in binary arrays\n counter += 1\n\n # Shrink window if it violates the allowed limit\n while counter > limit:\n if nums[left] == bad_element:\n counter -= 1\n left += 1\n\n max_len = max(max_len, right - left + 1) # adjust -1 if problem requires deletion"
22+
,
23+
"javascript": "// ---------------------------\n// Version 1: Fixed-Size Window (size = k)\n// ---------------------------\nlet left = 0;\nlet windowSum = 0;\nlet best = 0;\n\nfor (let right = 0; right < arr.length; right++) {\n windowSum += arr[right];\n\n if (right - left + 1 === k) {\n best = Math.max(best, windowSum);\n windowSum -= arr[left];\n left++;\n }\n}\n\n// ---------------------------\n// Version 2: Dynamic Window (variable size, with hashmap)\n// ---------------------------\nlet left2 = 0;\nlet maxLen = 0;\nlet count = {};\n\nfor (let right = 0; right < s.length; right++) {\n const char = s[right];\n count[char] = (count[char] || 0) + 1;\n\n while (windowInvalid) {\n count[s[left2]]--;\n if (count[s[left2]] === 0) delete count[s[left2]];\n left2++;\n }\n\n maxLen = Math.max(maxLen, right - left2 + 1);\n}\n\n// ---------------------------\n// Version 3: Dynamic Window (optimized counters, no hashmap)\n// ---------------------------\nlet left3 = 0;\nlet maxLen2 = 0;\nlet counter = 0; // counts bad elements\n\nfor (let right = 0; right < nums.length; right++) {\n if (nums[right] === badElement) counter++;\n\n while (counter > limit) {\n if (nums[left3] === badElement) counter--;\n left3++;\n }\n\n maxLen2 = Math.max(maxLen2, right - left3 + 1);\n}"
24+
,
25+
"java": "// ---------------------------\n// Version 1: Fixed-Size Window (size = k)\n// ---------------------------\nint left = 0;\nint windowSum = 0;\nint best = 0;\n\nfor (int right = 0; right < arr.length; right++) {\n windowSum += arr[right];\n\n if (right - left + 1 == k) {\n best = Math.max(best, windowSum);\n windowSum -= arr[left];\n left++;\n }\n}\n\n// ---------------------------\n// Version 2: Dynamic Window (variable size, with hashmap)\n// ---------------------------\nint left2 = 0;\nint maxLen = 0;\nMap<Character, Integer> count = new HashMap<>();\n\nfor (int right = 0; right < s.length(); right++) {\n char c = s.charAt(right);\n count.put(c, count.getOrDefault(c, 0) + 1);\n\n while (windowInvalid) {\n char lc = s.charAt(left2);\n count.put(lc, count.get(lc) - 1);\n if (count.get(lc) == 0) count.remove(lc);\n left2++;\n }\n\n maxLen = Math.max(maxLen, right - left2 + 1);\n}\n\n// ---------------------------\n// Version 3: Dynamic Window (optimized counters, no hashmap)\n// ---------------------------\nint left3 = 0;\nint maxLen2 = 0;\nint counter = 0;\n\nfor (int right = 0; right < nums.length; right++) {\n if (nums[right] == badElement) counter++;\n\n while (counter > limit) {\n if (nums[left3] == badElement) counter--;\n left3++;\n }\n\n maxLen2 = Math.max(maxLen2, right - left3 + 1);\n}"
26+
,
27+
"go": "// ---------------------------\n// Version 1: Fixed-Size Window (size = k)\n// ---------------------------\nleft := 0\nwindowSum := 0\nbest := 0\n\nfor right := 0; right < len(arr); right++ {\n windowSum += arr[right]\n\n if right-left+1 == k {\n if windowSum > best {\n best = windowSum\n }\n windowSum -= arr[left]\n left++\n }\n}\n\n// ---------------------------\n// Version 2: Dynamic Window (variable size, with hashmap)\n// ---------------------------\nleft2 := 0\nmaxLen := 0\ncount := make(map[rune]int)\n\nfor right, char := range s {\n count[char]++\n\n for windowInvalid {\n leftChar := rune(s[left2])\n count[leftChar]--\n if count[leftChar] == 0 {\n delete(count, leftChar)\n }\n left2++\n }\n\n if right-left2+1 > maxLen {\n maxLen = right-left2+1\n }\n}\n\n// ---------------------------\n// Version 3: Dynamic Window (optimized counters, no hashmap)\n// ---------------------------\nleft3 := 0\nmaxLen2 := 0\ncounter := 0\n\nfor right := 0; right < len(nums); right++ {\n if nums[right] == badElement {\n counter++\n }\n\n for counter > limit {\n if nums[left3] == badElement {\n counter--\n }\n left3++\n }\n\n if right-left3+1 > maxLen2 {\n maxLen2 = right-left3+1\n }\n}"
2528
},
2629
"problems": [
2730
"Maximum Sum Subarray of Size K",
@@ -30,7 +33,9 @@
3033
"Longest Substring with K Distinct Characters",
3134
"Minimum Window Substring",
3235
"Permutation in String",
33-
"Smallest Subarray With Sum ≥ Target"
36+
"Smallest Subarray With Sum ≥ Target",
37+
"Longest Subarray of 1's After Deleting One Element",
38+
"Max Consecutive Ones III"
3439
]
3540
},
3641
{
@@ -85,7 +90,6 @@
8590
},
8691
"problems": ["Subarray Sum Equals K", "Range Sum Query", "Continuous Subarray Sum"]
8792
},
88-
8993
{
9094
"title": "Backtracking",
9195
"description": "Used for generating combinations/permutations.",

0 commit comments

Comments
 (0)