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/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/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/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/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/manasa-and-stones/problem
// video: https://youtu.be/LbqoacVy7ok
function stones(n, a, b) {
// Use a set of numbers and iterate over all possibilities for n stones
let possValues = new Set([0])
let i = 0
while(i < n - 1){ // starting with 1 default "0" rock
// Messy time complexity; each iteration processes more elements
// 2^0 + 2^1 + 2^2...2^n
# source: https://www.hackerrank.com/challenges/animal-transport/problem
# video: https://youtu.be/35cgDwzPIxg
inp = input
ran = range
_int = int
ref = [
"Q3JlYXRlZCBieSBHbGl0Y2hlZCBGYWlsdXJl",
"aHR0cHM6Ly93d3cueW91dHViZS5jb20vY2hhbm5lbC9VQ0VyU05pRFpWNHJKQ05COE5yREdSRUE="
]
// souce: https://www.hackerrank.com/challenges/animal-transport/problem
// video: https://youtu.be/_X9LpQJGPmM
const ref = [
"Q3JlYXRlZCBieSBHbGl0Y2hlZCBGYWlsdXJl",
"aHR0cHM6Ly93d3cueW91dHViZS5jb20vY2hhbm5lbC9VQ0VyU05pRFpWNHJKQ05COE5yREdSRUE=",
]
function add_1_up_to_source(left, right, unique_updated_nodes, additions) {
while (left < right) {
// source: https://www.hackerrank.com/challenges/migratory-birds/problem
// video: https://youtu.be/L83yEmlW3qM
function migratoryBirds(arr) {
let max = 0;
let max_id = 6;
let store = new Array(6).fill(0);
for(const num of arr){
store[num]++;
if(store[num] > max){
// video: https://youtu.be/wRbyX9sRZ9g
////////////
// CONFIG //
////////////
const termsAndColors = [
["how", "yellow"],
["javaScript", "LightPink"],
["works", "CornflowerBlue"],
]
// More named colors here: https://www.w3schools.com/colors/colors_names.asp
// video: https://youtu.be/1O5YH1jFVRs
////////////
// CONFIG //
////////////
const backgroundColor = "#000000"
const textColorByTag = {
"H1":"#7FFFD4",
"H2":"#7FFFD4",
"H3":"#7FFFD4",
"P":"#F5F5DC",