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/acm-icpc-team/problem
# video: https://youtu.be/tfHd30CGAWs
def acmTeam(topic):
# keep track of teams (hashmap/dict)
teams = {}
length = len(topic)
for i in range(length - 1): # O(n)
curr_bin_string = topic[i] # first team member
# Take each bit set and use OR operation to create combined topic knowledge team
# source: https://www.hackerrank.com/challenges/equality-in-a-array/problem
# video: https://youtu.be/CLIVaqu39wU
def equalizeArray(arr):
# keep dict of counts
counts = {}
# keep highest count item
highest_count_item = arr[0]
# loop through list to take counts
for item in arr: # O(n)
# source: https://www.hackerrank.com/challenges/beautiful-triplets/problem
# video: https://youtu.be/fOUWTyxYXUQ
def beautifulTriplets(d, arr):
# Keep a running count dict
counts = {}
total = 0
# loop through list to get counts
for val in arr: # O(n)
if val not in counts:
# source: https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem
# video: https://youtu.be/WTwih7Ghvig
def isValid(s):
count_of_chars = dict()
for char in s:
if char not in count_of_chars:
count_of_chars[char] = 0
count_of_chars[char] += 1
# source: https://www.hackerrank.com/challenges/abbr/problem
# video: https://youtu.be/4WzCFTmjKu4
def abbreviation(a, b):
n = len(a)
m = len(b)
# init dp_list => O(n * m)
dp_list = [[False] * (m + 1) for _ in range(n + 1)] # + 1 b/c including empty string
dp_list[0][0] = True # abbreviation("", "") => True (base case)
// source: https://www.hackerrank.com/challenges/coin-change
// video: https://www.youtube.com/watch?v=b34GYbwwrJ8
function getWays(n, c) {
const store = new Array(n + 1).fill(0);
store[0] = 1;
for(const coin of c){
for(let total = coin; total < store.length; total++){
const remainder = total - coin;
store[total] += store[remainder];
# source: https://www.hackerrank.com/challenges/lisa-workbook/problem
# video: https://youtu.be/G2_o-FqdCTs
def workbook(n, k, arr):
count = 0
current_page = 1 # NOTE: we're indexing starting at 1
for question_count in arr: # O(n)
pages_added, remaining_questions = divmod(question_count, k)
for new_chapter_page in range(1, pages_added + 1): # O(pmodk) => O(100mod1) => O(100) => O(1)
# source: https://www.hackerrank.com/challenges/richie-rich/problem
# video: https://youtu.be/fcf4gy5V3L4
def count_mismatches(string_1, string_2):
"""
Returns count of mismatches between two given strings
"""
assert len(string_1) == len(string_2)
count = 0
for char_1, char_2 in zip(string_1, string_2):
count += char_1 != char_2
# source: https://www.hackerrank.com/challenges/sparse-arrays/problem
# Glitched Failure video: https://youtu.be/n9w7Nuy-FmA
function matchingStrings(strings, queries) {
const memory = {};
// O(n), where n = number of string
for(const string of strings) memory[string] = (memory[string] || 0) + 1;
// O(m), where m = number of queries (the lookup is constant time, O(1))
# source: https://www.hackerrank.com/challenges/kaprekar-numbers/problem
# video: https://youtu.be/UoHhr2dwP6g
def is_mod_kap(num):
'''Returns True if num is a modified kaprekar number'''
sq_num_str = str(num ** 2)
mid = len(sq_num_str) // 2 # floor division
left = sq_num_str[:mid] or 0
right = sq_num_str[mid:] or 0