Skip to content

Instantly share code, notes, and snippets.

@Gerst20051
Last active October 12, 2020 07:41
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/d5f2f262f2cc02685bc0b5c5292967f8 to your computer and use it in GitHub Desktop.
Save Gerst20051/d5f2f262f2cc02685bc0b5c5292967f8 to your computer and use it in GitHub Desktop.
Closing Time Coding Challenge
/*
* Complete the 'closingTime' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY queue
* 2. DOUBLE admission
* 3. DOUBLE runningCost
*/
function closingTime(queue, admission, runningCost) {
let runningProfit = 0;
let runningOverflow = 0;
let maximumProfit = {
amount: 0,
shift: 0,
};
for (let i = 0; i < queue.length; i++) {
runningProfit -= runningCost;
const shiftOverflow = queue[i] > 4 ? queue[i] - 4 : 0;
if (shiftOverflow) {
runningOverflow += shiftOverflow;
}
let shiftBoardings = Math.min(queue[i], 4);
if (shiftBoardings < 4 && runningOverflow > 0) {
const shiftOpenings = 4 - shiftBoardings;
if (runningOverflow >= shiftOpenings) {
shiftBoardings += shiftOpenings;
runningOverflow -= shiftOpenings;
} else {
shiftBoardings += runningOverflow;
runningOverflow = 0;
}
}
const shiftRevenue = shiftBoardings * admission;
runningProfit += shiftRevenue;
if (runningProfit > maximumProfit.amount) {
maximumProfit.amount = runningProfit;
maximumProfit.shift = i;
}
}
if (maximumProfit.amount <= 0) return -1;
return maximumProfit.shift;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment