HealthFood
SRM 219 · 2004-11-20 · by erinn
Problem Statement
Your favorite local restaurant has caught onto the health food trend, and has begun publishing basic nutritional information for all of their most popular menu selections. This is excellent timing, since your doctor has recently recommended various diet plans for you and several of your friends.
You are given
You are also given a
- 'C' = high carbs
- 'c' = low carbs
- 'P' = high protein
- 'p' = low protein
- 'F' = high fat
- 'f' = low fat
- 'T' = high calorie
- 't' = low calorie
As an example, the diet plan "tf" means the doctor recommends a meal with the lowest possible calories, and if more than one is tied, the one with less fat should be selected. Whenever more than one meal is tied according to the diet plan, then the one with a lower index should be selected.
The restaurant sloppily neglected to list the total calorie count on the menu. Fortunately, you happen to remember from days gone by that one gram of fat contains 9 calories, and one gram of carbs or protein contains 5 calories.
You are to return a
Notes
- When no diet plan is specified, the default should be the menu selection with the lowest index (0).
Constraints
- carbs will contain between 1 and 50 elements, inclusive
- protein will contain between 1 and 50 elements, inclusive
- fat will contain between 1 and 50 elements, inclusive.
- carbs, protein, and fat will each contain the same number of elements.
- Each element of carbs, protein, and fat will be between 0 and 100, inclusive.
- dietPlans will contain between 1 and 50 elements, inclusive.
- Each element of dietPlans will be between 0 and 4 characters in length, inclusive, and will contain only the characters "CcPpFfTt".
{3, 4}
{2, 8}
{5, 2}
{"P", "p", "C", "c", "F", "f", "T", "t"}
Returns: { 1, 0, 1, 0, 0, 1, 1, 0 }
This is a simple menu, with only two selections. We see each of the simplest diet plans here.
{3, 4, 1, 5}
{2, 8, 5, 1}
{5, 2, 4, 4}
{"tFc", "tF", "Ftc"}
Returns: { 3, 2, 0 }
Note here that lowest total calories is tied between items 2 and 3. Note also that both of those items are tied for fat content. So, when we have lowest carbs as a tie-breaker, item 3 is selected. When there is no further tiebreaker, we select the one with lowest index. Note also that if highest fat is the first requirement, then the tiebreaker is irrelevant since item 0 has more fat than items 2 or 3.
{3, 4, 1, 5}
{2, 8, 5, 1}
{5, 2, 4, 4}
{"tf", "cF", "PC", "FTCP"}
Returns: { 2, 3, 3, 0 }
Here, we have the same menu items, being ordered by someone on the typical low-calorie, low-fat diet, someone on an Atkin's type diet (low carbs, high fat), someone who is training to run the marathon (high protein, high carbs), and a bear getting ready for hibernation (lots of fat, lots of calories!).
{18, 86, 76, 0, 34, 30, 95, 12, 21}
{26, 56, 3, 45, 88, 0, 10, 27, 53}
{93, 96, 13, 95, 98, 18, 59, 49, 86}
{"f", "Pt", "PT", "fT", "Cp", "C", "t", "",
"cCp", "ttp", "PCFt", "P", "pCt", "cP", "Pc"}
Returns: { 2, 6, 6, 2, 4, 4, 5, 0, 5, 5, 6, 6, 3, 5, 6 }
{21,76,96,7,6,76,59,52,90,93,27,12,38,88,53,38,81,27,61,59,10,85,65,38,16,57,6,62,26,10,17,39,92,52,9,39,83,22}
{90,51,20,19,97,61,23,41,68,64,87,56,27,96,86,0,99,92,96,98,44,67,53,1,33,27,22,70,23,99,5,56,83,88,63,35,22,65}
{67,42,66,69,81,47,32,43,57,50,58,91,64,99,28,61,40,35,44,53,68,70,14,49,77,68,66,78,70,81,83,90,73,82,73,68,15,41}
{"cF","Ccp","T","TPFP","CCTf","Fpp","Tf","tP","Pc","T","","pPf","PT"}
Returns: { 15, 29, 13, 13, 29, 13, 13, 23, 2, 13, 0, 26, 2 }
Submissions are judged against all 64 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class HealthFood with a public method vector<int> selectMeals(vector<int> protein, vector<int> carbs, vector<int> fat, vector<string> dietPlans) · 64 test cases · 2 s / 256 MB per case