Skip to content

Instantly share code, notes, and snippets.

View Shaddyjr's full-sized avatar
📉
You must construct additional models

Mahdi Shadkam-Farrokhi Shaddyjr

📉
You must construct additional models
View GitHub Profile
# source: https://www.hackerrank.com/challenges/bear-and-steady-gene
# video: https://youtu.be/eIW7vBWzibE
def extras_available(all_counts, max_nucleotide_count):
'''Returns True if any nucleotide count is greater than max_nucleotide_count'''
# loop through all counts
for counts in all_counts.values():
# if any count is greater than max_nucleotide_count, return True
if counts > max_nucleotide_count:
return True
# if got past loop, no extras found => return False
# source: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/distinct-integers-in-range-66eca44b/description/
import re
### SETUP ###
max_int = 5000
MAX_VALUE = (1 << max_int) - 1
re_splitter = re.compile(" ")
def agg_func(val_1, val_2):
output = val_1 | val_2
# video: https://youtu.be/I7RFycpqbDk
from math import ceil, log2
class SegmentationTree():
def __init__(self, input_list):
self._input_list = input_list[:] # copy of input_list
self._init_tree()
self._is_propogated = True
def _init_tree(self):
# source: https://www.hackerrank.com/challenges/find-maximum-index-product/problem?isFullScreen=false
# video: https://youtu.be/z8iSOFUbQUM
# Stack convenience wrapper for list
class Stack():
def __init__(self):
self._stack = [] # store internal list
def is_empty(self): # true when stack is empty
return len(self._stack) == 0
# source: https://leetcode.com/problems/combination-sum
# video: https://youtu.be/0Jt9_qKimCU
def combinationSum(candidates, target):
dp = [[] for _ in range(target + 1)] # len(dp) = target + 1 b/c want to start with 0
# intitialize with target = 0, must be empty
dp[0].append([]) # start out as empty list, with sum total = 0
# since combinations mean order doesn't matter, we should ensure we don't pass over
# parts already calculated ("old" ground)
# source: https://www.hackerrank.com/challenges/3d-surface-area/problem
# video: https://youtu.be/tIG0GF2EqjE
def surfaceArea(A):
n_rows = len(A)
n_cols = len(A[0])
total = (n_rows * n_cols) * 2 # top and bottom
for row_i in range(n_rows): # O(n)
for col_i in range(n_cols): # O(m)
val = A[row_i][col_i]
# source: https://www.hackerrank.com/challenges/hackerland-radio-transmitters
def hackerlandRadioTransmitters(houses, transmitter):
houses.sort() # requires sorting => O(nlogn)
transmitter_count = 0
i = 0
while i < len(houses): # O(n)
transmitter_count += 1
max_transmitting_house = houses[i] + transmitter
# position after transmitting house
# source: https://www.hackerrank.com/challenges/board-cutting/problem
def boardCutting(cost_y, cost_x):
MOD_INT = (10**9) + 7
# sort each cost (asc)
cost_y.sort() # O(nlogn)
cost_x.sort() # O(mlogm)
horizontal_boards = 1
vertical_boards = 1
def roadsAndLibraries(n, c_lib, c_road, cities):
# if library cost is less than road cost, then cheaper to only build libraries
if c_lib <= c_road:
return n * c_lib
city_groups = {city_id:{city_id} for city_id in range(1,n + 1)}
# groups = {1: {1}, 2: {2}, 3: {3}, 4: {4}, 5: {5}}
for city_a, city_b in cities: # O(m); where m = number of all city roads
city_groups[city_a].add(city_b)
# source: https://www.hackerrank.com/challenges/taum-and-bday/problem
# video: https://youtu.be/9vajRE0bJ5w
def taumBday(b, w, bc, wc, z):
# if black gifts > white gifts + z conversion cost
if bc > wc + z:
return (w * wc) + (b * (wc + z))
# if white gifts > black gifts + z conversion cost
if wc > bc + z:
return (b * bc) + (w * (bc + z))