Last active
October 12, 2020 07:41
-
-
Save Gerst20051/d5f2f262f2cc02685bc0b5c5292967f8 to your computer and use it in GitHub Desktop.
Closing Time Coding Challenge
This file contains 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
/* | |
* 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