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/xor-quadruples/problem
// video: https://youtu.be/LyOpIRgobsc
function beautifulQuadruples(a, b, c, d) {
// sorting values for consistency and order doesn't matter with XOR operation
const sortedInput = [a, b, c, d]
sortedInput.sort((val1, val2) => val1 - val2)
const [A, B, C, D] = sortedInput
// Given the maximum number D, the most significant bit sets the limit for
# source: https://www.hackerrank.com/challenges/xor-quadruples/problem
# video: https://youtu.be/LyOpIRgobsc
def beautifulQuadruples(a, b, c, d):
# sorting values for consistency and order doesn't matter with XOR operation
sorted_input = sorted([a, b, c, d])
A, B, C, D = sorted_input
# Given the maximum number D, the most significant bit sets the limit for
# how much space is needed for C_D_memo
# source: https://www.hackerrank.com/challenges/palindrome-index/problem
# video: https://youtu.be/IoiYMKmvuPQ
def is_palindrome(s):
left = 0
right = len(s) - 1
while left < right: # O(n)
if s[left] != s[right]:
return False
left += 1
// source: https://www.hackerrank.com/challenges/palindrome-index/problem
// video: https://youtu.be/IoiYMKmvuPQ
function isPalindrome(s){
let left = 0
let right = s.length - 1
while(left < right){ // O(n)
if(s[left]!==s[right]){
return false
}
left++
# Source: https://www.hackerrank.com/challenges/happy-ladybugs/problem
# Video: https://youtu.be/g9mzmUenrUU
def happyLadybugs(b):
hasSpace = False
store = {}
for label in b: # O(n), where n is the length of the string
if label == '_':
hasSpace = True
continue
// Source: https://www.hackerrank.com/challenges/happy-ladybugs/problem
// Video: https://youtu.be/g9mzmUenrUU
function happyLadybugs(b) {
let hasSpace = false
const store = {}
for(const label of b){ // O(n), where n is the length of the string
if (label === '_'){
hasSpace = true
continue
// source: https://www.hackerrank.com/challenges/big-sorting/problem
// video: https://youtu.be/0KJhHCIuoG0
function bigSorting(unsorted) {
// Chunk values into separate sizes
const cache = {}
for (const val of unsorted){ // O(n), max of 2*10^5
const length = val.length
if (!cache[length]){
cache[length] = []
# source: https://www.hackerrank.com/challenges/big-sorting/problem
# video: https://youtu.be/0KJhHCIuoG0
def bigSorting(unsorted):
# Chunk values into separate sizes
cache = {}
for val in unsorted: # O(n), max of 2*10^5
n = len(val)
if n not in cache:
cache[n] = []
cache[n].append(val)
# source: https://www.hackerrank.com/challenges/equal/problem
# video: https://youtu.be/Mbfs-4URYmg
def equal(arr):
rounds = float("inf")
min_val = min(arr)
# Go through each target value (min, min-1, min-2, ..., min-4)
for n in range(5): # O(5) = O(1)
target = min_val - n
operationCount = 0
// source: https://www.hackerrank.com/challenges/equal/problem
// video: https://youtu.be/Mbfs-4URYmg
function equal(arr) {
let rounds = Infinity
const min = Math.min(...arr)
// Go through each target value (min, min-1, min-2, ..., min-4)
for (let n = 0; n < 5; n++){ // O(5) = O(1)
const target = min - n
let operationCount = 0
// Go through each value in arr...