Gumball Machines
Updated
Problem statement
A diner has three gumball machines. Each machine holds gumballs of a single color, at most 1000 of them. All three machines start empty.
You are given colors, where colors[i] is the color of gumballs a truck brings on the morning of day i. Each morning the machines are restocked with these rules, checked in order:
- If a machine already holds this color, fill it back up to 1000. Nothing is thrown away.
- Otherwise, if a machine is empty, fill it with 1000 gumballs of the new color.
- Otherwise all three machines hold other colors. Throw away every gumball in the machine that holds the fewest, then fill that machine with 1000 gumballs of the new color.
After the restock, every machine that holds gumballs sells 10 of them during the day. Each gumball is worth 1 cent.
Return the total value, in cents, of all gumballs thrown away over all the days.
With these rules, in step 3 exactly one machine holds the fewest gumballs, and in step 2 it does not matter which empty machine you use.
Function
discardedCents(colors: String[]) → intExamples
Example 1
colors = ["red","blue","green","pink"]return = 970Days 0, 1 and 2 fill the three empty machines with red, blue and green. On the morning of day 3 they hold 970 red (sold on days 0, 1 and 2), 980 blue and 990 green. Pink matches none of them and no machine is empty, so the 970 red gumballs are thrown away and that machine gets 1000 pink. The answer is 970 cents.
Example 2
colors = ["mint","lime","plum","mint","cola","lime"]return = 1940Days 0 to 2 fill the machines with mint, lime and plum. On day 3 mint comes again: its machine goes back up to 1000 and nothing is thrown away. On the morning of day 4 the machines hold 990 mint, 970 lime and 980 plum, so cola replaces lime and 970 gumballs are thrown away. On the morning of day 5 they hold 980 mint, 990 cola and 970 plum. Lime is no longer in a machine, so plum is replaced and 970 more are thrown away. The answer is 970 + 970 = 1940 cents.
Example 3
colors = ["gold","gold","gold"]return = 0Gold fills one machine on day 0 and refills the same machine on days 1 and 2. Nothing is ever thrown away, so the answer is 0.
Constraints
1 <= colors.length <= 201 <= colors[i].length <= 10colors[i]consists only of lowercase English letters.
More Nutanix Online Assessment questions
- Good SubsequencesOA · Seen Jul 2026
- Min Cost to Connect All PointsOA · Seen Jun 2026
- Digit Changes for a Multiple of 9OA · Seen Aug 2025
- Gas Station Loop StartOA · Seen Aug 2025
- Number of Players (for MTS2)OA · Seen Oct 2024
- Work Schedules (for MTS2)OA · Seen Oct 2024
- Minimum Operations to Convert ArrayOA · Seen Apr 2024
- No Adjacent CharactersOA · Seen Apr 2024
- Build a BST From a ListOA · Seen Nov 2022
- Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022
Similar questions at other companies
- Count Server ReplacementsAkuna Capital · OAAkuna Capital interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
- Student and Course EnrollmentWells Fargo · OAWells Fargo interview questions
- Network Endpoint or Cycle BoundaryOpenAI · OA · PHONE SCREENOpenAI interview questions
- Branch with the Most FilesCapital One · OACapital One interview questions
- Assign Server Numbers by TypeOura · OA
- Filter Transaction Logs and Calculate Reliability MetricsStripe · OAStripe interview questions
- Calculate Net ProfitJP Morgan Chase · OAJP Morgan Chase interview questions
- Get Total RequestsAmazon · OA