Skip to content

Instantly share code, notes, and snippets.

View siavashk's full-sized avatar

Siavash Khallaghi siavashk

View GitHub Profile
# UnionFind is defined in https://gist.github.com/siavashk/fef678f9f2ef17fdc6cff917922bf4ba
# Augment the union function to return True for new connections
def kruskal(edges: list[tuple[int, int, int]]):
"""
Edges: list of edge in (weight, start, end]) format
"""
edges.sort()
uf = UnionFind()
for _, x, y in edges:
@siavashk
siavashk / binary_search.py
Last active October 1, 2025 16:27
Binary Search Templates
# https://leetcode.com/discuss/post/2371234/an-opinionated-guide-to-binary-search-co-1yfw/
def minimization(arr, condition):
"""
Find the first time a condition is True.
[F, F, F, (T), T, T, T]
(T) is the first time the conditions is True.
r is the True answer (always T)
l is always invalid (always F)
"""
l, r = -1, len(arr) - 1 # if len(arr) is a candidate