Minimum Dining Table Rotations
Problem statement
A round table has seats positions numbered clockwise from 0 to seats - 1. A customer sits at position customer. A rotating tray initially has one dish position aligned with each seat.
The string dishes describes the initial tray layout. At position i, dishes[i] is one of:
E: a starter.P: a main course.D: a dessert.N: no dish.
The customer may rotate the tray clockwise or counterclockwise, one seat position per rotation. The meal must contain one starter, then one main course, then one dessert, in that order. A course can be eaten when a dish of its type is in front of the customer. Eating does not add a rotation.
There is at least one dish of each required type. Return the minimum total number of one-seat rotations needed to finish the meal.
Function
calculateTurns(seats: int, customer: int, dishes: String) → intExamples
Example 1
seats = 4customer = 1dishes = "ENPD"return = 4Initially the customer faces the empty position 1. Bring the starter originally at position 0 to the customer with one rotation. Then bring the main course from position 2 with two rotations, and the dessert from position 3 with one rotation. The total is 1 + 2 + 1 = 4.
Example 2
seats = 3customer = 0dishes = "EPD"return = 2The starter is already in front of the customer. One rotation brings the main course, and one more brings the dessert, for a total of 2.
Example 3
seats = 7customer = 6dishes = "EEEPEDD"return = 5Choose the starter originally at position 4, then the main course at 3, then the dessert at 5. The circular distances are 2, 1, 2, totaling 5. Choosing the nearest starter at position 0 instead requires at least 6 rotations for the complete meal.
Constraints
3 <= seats < 20.0 <= customer < seats.dishes.length == seats.- Every character of
dishesisE,P,D, orN. - The tray contains at least one starter, one main course, and one dessert.