Skip to content

Instantly share code, notes, and snippets.

@eakray
Last active July 8, 2022 05:37
Show Gist options
  • Select an option

  • Save eakray/b5a89bb7c16c6493a25a6deb405b2231 to your computer and use it in GitHub Desktop.

Select an option

Save eakray/b5a89bb7c16c6493a25a6deb405b2231 to your computer and use it in GitHub Desktop.
hackerrank
//compare the triplets
function compareTriplets(a, b) {
const results = a.reduce((accum, current, index) => {
if (current > b[index]) {
accum.a = accum.a + 1;
}
if (current < b[index]) {
accum.b = accum.b + 1;
}
return accum;
},{a: 0, b: 0});
return [results.a, results.b];
}
//Mini-Max Sum
const a = [1,2, 3, 4, 5];
const minMax = arr => {
const totalSum = arr.reduce((accum, current) => {
if (current > accum.max) {
accum.max = current;
}
if (current < accum.min) {
accum.min = current;
}
accum.sum = accum.sum + current;
return accum;
}, {max: Number.NEGATIVE_INFINITY,
min: Number.POSITIVE_INFINITY,
sum: 0
});
const max = totalSum.sum - totalSum.min;
const min = totalSum.sum - totalSum.max;
return [min, max];
}
//Birthday Cake Candles
const a = [3,2,1,3];
const birthdayCakeCandles = arr => {
return arr.reduce((accum, current) => {
if (current > accum.max) {
accum.max = current;
accum.numMax = 1;
}else if (current === accum.max) {
accum.numMax = accum.numMax + 1;
}
return accum;
}, {max: Number.NEGATIVE_INFINITY,
numMax: 0
}).numMax;
}
//10 2 3
//3 1
//5 2 8
const getMoneySpent = (keyboards, drives, b) => {
let maxSpend = -1;
keyboards.forEach(board => {
drives.forEach(key => {
let cost = board + key;
if (cost <= b && cost > maxSpend) {
maxSpend = board + key;
}
})
})
return maxSpend;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment