Skip to content

Instantly share code, notes, and snippets.

@MrMeison
Created May 13, 2018 16:22
Show Gist options
  • Save MrMeison/4c88330ca756fd2c736fb79fee91beb7 to your computer and use it in GitHub Desktop.
Save MrMeison/4c88330ca756fd2c736fb79fee91beb7 to your computer and use it in GitHub Desktop.
const sum = (arr, start, count) => arr.slice(start, start + count).reduce((res, val) => res + val, 0);
const solve = (n, a, b, c) => {
let i = 0;
let res = 0;
while(i < n) {
if (sum(a, i, 2) && b[i] + a[i + 1] > c[i]) {
res += c[i];
i += 3;
} else if (sum(a, i, 1) > b[i]) {
res += b[i];
i += 2;
} else {
res += a[i];
i++;
}
}
return res;
};
console.log(`${solve(
5,
[5, 2, 5, 20, 20],
[10, 10, 5, 20, 1],
[15, 15, 5, 1, 1]
)}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment