Created
July 30, 2019 03:24
-
-
Save wataruoguchi/28b758dd5ab1d03bdb1b899b6daadba6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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