These coding patterns will help you solve Leetcode problems effectively. These programs are written in Python.
Pattern | Format | Code Snippet |
---|---|---|
Sliding Window (Given Window size K) | Hint: Continuous & Subarray of size K Output: Operation(sum, max, av) of subarray of size K Approach: Slide along a window of size K SW Time: O(N) Brute Force Time: O(N*K) |
Max sum of subarray of K continuous elements start = 0 sum, result = 0, [] for end in range(0, len(arr)): sum = sum + arr[end] if end>= k-1: result.append(sum) sum = sum - arr[start] start += 1 return max(result) |