Last active
November 27, 2024 21:38
-
-
Save ericnormand/d1944f11d6e68f40606f052db6ed9384 to your computer and use it in GitHub Desktop.
Minimal meal prep times for two stoves
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
/** | |
// Using number of minutes for prep time | |
mealPrep([120]) | |
2 // one single long dish | |
mealPrep([30, 30, 30, 20]) | |
2 // multiple shorter dishes | |
^ | |
I don't understand, this should take 60 minutes. | |
mealPrep([30, 25, 45, 30, 60, 15]) | |
3 // many dishes with varying times | |
^ | |
I don't understand. It should only take 105 minutes. | |
**/ | |
function mealPrep(prepTimes) { | |
var total = prepTimes.reduce((a,b)=>a+b, 0); | |
var halfTime = total/2; | |
function recur(total, remaining) { | |
if(total<halfTime) return; | |
if(remaining.length === 0) return total; | |
var rest = remaining.slice(1) | |
var include = recur(total, rest); | |
var exclude = recur(total-remaining[0], rest); | |
if(!include) | |
return exclude; | |
if(!exclude) | |
return include; | |
if(include < exclude) | |
return include; | |
return exclude; | |
} | |
return Math.ceil(recur(total, prepTimes)/60); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment