Skip to content

Instantly share code, notes, and snippets.

@Gerst20051
Last active December 9, 2020 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gerst20051/db4bab73c19d2cfd4bc9c2e98e6880c8 to your computer and use it in GitHub Desktop.
Save Gerst20051/db4bab73c19d2cfd4bc9c2e98e6880c8 to your computer and use it in GitHub Desktop.
Angry Bookstore Owner - Maximize Customer Satisfaction
// Today, the bookstore owner has a store open for customers.length minutes.
// Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute.
// On some minutes, the bookstore owner is grumpy.
// If the bookstore owner is grumpy on the i-th minute, grumpy[i] = 1, otherwise grumpy[i] = 0.
// When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.
// The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.
// Return the maximum number of customers that can be satisfied throughout the day.
function maximizeCustomerSatisfaction(customers, grumpy, x) {
const combinations = generateGrumpyCombinations(grumpy, x);
return combinations.reduce((maximumCustomersSatisfied, combination) => {
const customersSatisfied = combination.reduce((customersSatisfied, isGrumpy, index) => {
return isGrumpy ? customersSatisfied : customersSatisfied + customers[index];
}, 0);
return customersSatisfied > maximumCustomersSatisfied ? customersSatisfied : maximumCustomersSatisfied;
}, 0);
}
function generateGrumpyCombinations(grumpy, x) {
const combinations = [grumpy];
for (let i = 0; i < grumpy.length - x + 1; i++) {
combinations.push([...grumpy].fill(0, i, i + x));
}
return combinations;
}
console.log(maximizeCustomerSatisfaction([1,0,1,2,1,1,7,5], [0,1,0,1,0,1,0,1], 3) === 16);
console.log(maximizeCustomerSatisfaction([1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], 3) === 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment