Skip to content

Instantly share code, notes, and snippets.

@wataruoguchi
Created July 30, 2019 03:24
Show Gist options
  • Save wataruoguchi/28b758dd5ab1d03bdb1b899b6daadba6 to your computer and use it in GitHub Desktop.
Save wataruoguchi/28b758dd5ab1d03bdb1b899b6daadba6 to your computer and use it in GitHub Desktop.
// https://practice.geeksforgeeks.org/problems/stickler-theif/0
function sticklerTheif(houses) {
function getAmount(condition) {
return (acc, house, idx) => {
if (condition(idx)) {
acc += house;
}
return acc;
}
}
const amount1 = houses.reduce(getAmount((idx) => idx % 2 === 0), 0);
const amount2 = houses.reduce(getAmount((idx) => idx % 2 !== 0), 0);
return amount1 > amount2 ? amount1 : amount2;
}
const houses1 = [5,5,10,100,10,5];
const result1 = sticklerTheif(houses1);
console.log(result1 === 110);
const houses2 = [1,2,3];
const result2 = sticklerTheif(houses2);
console.log(result2 === 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment