Skip to content

Instantly share code, notes, and snippets.

def matrix_mul(m1, m2):
s1, s2 = m1
k1, k2 = m2
cost = s1 * s2 * k2
resulting_matrix = (s1, k2,)
return cost, resulting_matrix
def matrix_chain_mult(matrices):
if len(matrices) == 0:
return 0, None
CACHE = {}
def compute(w, h):
# Base case, nothing
if 0 in [w, h]:
return 1
try:
return CACHE[(w, h)]
CACHE = {}
def lcs2(frm, to):
len_frm = len(frm)
len_to = len(to)
# Create a table
# len_to is height, len_frm is width
table = [[0] * len_to
def solve(cost, els):
total = [
[(0, -1)] * (cost + 1)
for _ in xrange(len(els) + 1)
]
for idx, el in enumerate(els):
idx += 1
for current_cost in xrange(cost + 1):
if el > current_cost:
import bisect
def main():
with open('rope_large.in') as f:
n = int(f.readline())
for n in xrange(n):
amount = int(f.readline())
items_left = []
items_right = []
import string
from collections import defaultdict
from heapq import heappop, heappush
class UnionFind(object):
def __init__(self, size):
# not enough
self.size = defaultdict(int)
self.weights = range(size)
import re
RE = re.compile('(\w+)')
def insert(current_tree, current_path, n=0):
if len(current_path) == 0:
return n
first, last = current_path[0], current_path[1:]
try:
import string
from collections import defaultdict
from heapq import heappop, heappush
class UnionFind(object):
def __init__(self, size):
# not enough
self.size = defaultdict(int)
self.weights = range(size)
Case #4:
import string
class UnionFind(object):
def __init__(self, size):
# not enough
self.weights = range(size)
def union(self, a, b):
# Add top-left rule
smaller_letter, bigger_letter = sorted((