Sliding Window Template
Applicable whenever searching for an optimal contiguous subarray satisfying monotonic conditions:
algorithms/sliding_window.ts
function longestSubarray(nums: number[], k: number): number {
let left = 0;
let currentSum = 0;
let maxLen = 0;
for (let right = 0; right < nums.length; right++) {
currentSum += nums[right];
while (currentSum > k && left <= right) {
currentSum -= nums[left];
left++;
}
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}Personal engineering field notes compiled from production systems. Patterns and benchmarks are updated as runtimes evolve.