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/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/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/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/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
// video: https://youtu.be/1O5YH1jFVRs
////////////
// CONFIG //
////////////
const backgroundColor = "#000000"
const textColorByTag = {
"H1":"#7FFFD4",
"H2":"#7FFFD4",
"H3":"#7FFFD4",
"P":"#F5F5DC",
// 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
// 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){
// 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/animal-transport/problem
# video: https://youtu.be/35cgDwzPIxg
inp = input
ran = range
_int = int
ref = [
"Q3JlYXRlZCBieSBHbGl0Y2hlZCBGYWlsdXJl",
"aHR0cHM6Ly93d3cueW91dHViZS5jb20vY2hhbm5lbC9VQ0VyU05pRFpWNHJKQ05COE5yREdSRUE="
]
// 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