Algo Components Flashcards
if not array:
return 0 # or [], None, etc.
Empty input guard - Protects against empty array cases
if len(array) == 1:
return array[0]
Single element guard - Handles single element edge case
if start > end:
return -1
Invalid input guard - Protects against invalid range inputs
current_max = float(‘-inf’)
current_min = float(‘inf’)
Min/Max tracking - Initialize tracking variables for finding extremes
prev = array[0]
for current in array[1:]:
# compare with prev
Previous value tracking - Pattern for comparing consecutive elements
running_sum = 0
prefix_sums = [0] # Include 0 for easier calculations
Running sums - Track cumulative sums for efficient range calculations
window_sum = sum(array[:k])
for i in range(k, len(array)):
# slide window
Fixed size window - Initialize and slide a window of constant size
start = 0
current_sum = 0
for end in range(len(array)):
# expand window
while condition_broken:
# contract window
Dynamic window - Window that can grow and shrink based on conditions
left, right = 0, len(array) - 1
n = len(array)
From Ends Setup - Initialize pointers at both ends of array
slow = fast = start_point
seen = set()
Fast/Slow Setup - Initialize pointers for cycle detection or similar patterns
write = 0
read = 1
Same Direction Setup - Initialize pointers for in-place array modification
for i in range(len(array)):
# process until end
Bounded loop - Process array with known bounds
while left < right:
# process until condition met
Conditional loop - Process until pointers meet or condition satisfied
while slow and fast and fast.next:
# process with multiple conditions
Multi-pointer loop - Process with multiple pointer conditions
def modify_array(array):
write_idx = 0
for read_idx in range(len(array)):
# modify array directly
In-place modification - Modify array without extra space
def build_result(array):
result = []
for element in array:
# append to result
return result
New result construction - Build new result structure
if d and d[0] < i - k + 1:
d.popleft()
d.append(i)
Creates sliding window in a deque of k elements. This removes the leftmost element.