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/almost-sorted/problem
// video: https://youtu.be/deEW0pxWwsA
function almostSorted(arr) {
const isSortedAroundIndex = index => {
/*
Return true if value at and around index are sorted.
Handles edge "bounces".
*/
const val = arr[index]
// source: https://www.hackerrank.com/challenges/larrys-array/problem
// video: https://youtu.be/XWXVRdpYE0I
function larrysArray(A) {
// Since each value in the given array is unique we can map backwards
// the index of each value (allows us to quickly "find" where a desired val is)
const value_index_map = {}
for(let i = 0; i < A.length; i++){ // O(n)
const val = A[i]
value_index_map[val] = i
// source: https://www.hackerrank.com/challenges/minimum-distances/problem
// video: https://youtu.be/gKp8Aq9mm9E
function minimumDistances(a) {
// stores each unique value as a key with a value of last index
const val_last_i = {}
let min_distance = Infinity;
// loop through array and populate val_last_i
for(let i = 0; i < a.length; i++){ // O(n)
# source: https://www.hackerrank.com/challenges/absolute-permutation/problem
# video: https://youtu.be/Z0bpJiVz0no
def absolutePermutation(n, k):
if k == 0: # no need to swap anything
return list(range(1, n + 1))
cache = set(range(1, n + 1)) # O(n)
output = []
for i in range(1, n + 1): # O(n)
lower = i - k
# 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
# 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/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/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/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/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)