This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CACHE = {} | |
def compute(w, h): | |
# Base case, nothing | |
if 0 in [w, h]: | |
return 1 | |
try: | |
return CACHE[(w, h)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Case #4: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(( |