Links RSS
Author ArgentumCation Posts Notes
Summary Updated

Two pointer


Sliding Window


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
visited = set()
queue = Queue()
queue.enqueue(start_node)
visited.add(start_node)
while queue is not empty:
	current = queue.dequeue()
	process(current) # e.g. print(current)
	for neighbor in graph[current]:
		if neighbor not in visited:
			visited.add(neighbor)
			queue.enqueue(neighbor)
return visited
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def BFS_path(graph, start, end):
    visited = {start}
    queue = deque([start])
    parent = {start: None}

    while queue is not empty:
        current = queue.dequeue()

        if current == end:
            return reconstruct_path(parent, end)

        for neighbor in graph[current]:
            if neighbor not in visited:
                visited.popleft(neighbor)
                parent[neighbor] = current
                queue.append(neighbor)

    return None   # no path exists

function reconstruct_path(parent, end):
    path = []
    node = end
    while node is not None:
        path.append(node)
        node = parent[node]
    reverse(path)
    return path
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def dfs(graph, start, visited=None):
    if visited is None:
        visited = set()

    visited.add(start)
    print(start)  # process the node

    for neighbor in graph[start]:
        if neighbor not in visited:
            dfs(graph, neighbor, visited)

    return visited
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from collections import deque

def dfs_all_paths(graph, start, end):
    all_paths = []
    stack = deque([(start, [start], {start})])

    while stack:
        current, path, visited = stack.pop()

        if current == end:
            all_paths.append(path)
            continue

        for neighbor in graph[current]:
            if neighbor not in visited:
                stack.append((neighbor, path + [neighbor], visited | {neighbor}))

    return all_paths
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def driver(nums):
    result = []
    path = []
    used = [False] * len(nums)

    backtrack(path, used, nums, result)
    return result

def backtrack(path, choices, result):
    if is_solution(path):
        result.append(path.copy())
        return result  # or `continue` exploring if partial solutions are also valid

    for choice in choices:
        if not is_valid(choice, path):
            continue

        # make the choice
        path.append(choice)

        # recurse
        backtrack(path, next_choices(choices, choice), result)

        # undo the choice
        path.pop()


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from collections import deque
dq = deque()
dq.pop() # pop right
dq.append() #push right
dq.popleft() #pop left
dq.appendleft() #push left
# stack is append/pop
# queue is append/popleft

# minheap
import heapq
lst = [1,2,4,5]
heapq.heapify(lst)

heapq.heappush(lst,6)
heapq.heappop(lst)
lst[0] #peek min