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/chocolate-feast/problem
// video: https://youtu.be/TPjNYmUzOdg
function chocolateFeast(n, c, m) {
// initial buy
let bars = Math.floor(n/c)
let wrappers = bars
while (wrappers >= m){
// buy more bars
const additionalBars = Math.floor(wrappers/m)
bars += additionalBars
// source: https://www.hackerrank.com/challenges/service-lane/problem
// video: https://youtu.be/1nLHUwp_k80
function serviceLane(n, width, cases) {
const output = []
for(const [start, end] of cases){ // O(c); c = number of cases
let min = Infinity
for(let i = start; i <= end; i++){ // O(n); n = number of widths
min = Math.min(min, width[i])
// Source: https://www.hackerrank.com/challenges/flatland-space-stations/problem
// video: https://youtu.be/zCCz59gT9Rs
// Complete the flatlandSpaceStations function below.
function flatlandSpaceStations(n, c) {
// c = cities with space stations is NOT ordered
c.sort((a,b) => a - b) // O(clogc)
let maxDist = 0
let lastCity = 0
// consider very first city
# Source: https://www.hackerrank.com/challenges/flatland-space-stations/problem
# video: https://youtu.be/zCCz59gT9Rs
def flatlandSpaceStations(n, c):
# c = cities with space stations is NOT ordered
c.sort() # O(clogc)
# consider very first city
max_dist = c[0]
# going left to right, assume midpoint distance for max
// 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...
# 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/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/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/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/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