A generic microwave supports cooking times for:
1 second.99 minutes and 99 seconds.To set the cooking time, you push at most four digits. The microwave normalizes what you push as four digits by prepending zeroes. It interprets the first two digits as the minutes and the last two digits as the seconds. It then adds them up as the cooking time. For example,
9 5 4 (three digits). It is normalized as 0954 and interpreted as 9 minutes and 54 seconds.0 0 0 8 (four digits). It is interpreted as 0 minutes and 8 seconds.8 0 9 0. It is interpreted as 80 minutes and 90 seconds.8 1 3 0. It is interpreted as 81 minutes and 30 seconds.You are given integers startAt, moveCost, pushCost, and targetSeconds. Initially, your finger is on the digit startAt. Moving the finger above any specific digit costs moveCost units of fatigue. Pushing the digit below the finger once costs pushCost units of fatigue.
There can be multiple ways to set the microwave to cook for targetSeconds seconds but you are interested in the way with the minimum cost.
Return the minimum cost to set targetSeconds seconds of cooking time.
Remember that one minute consists of 60 seconds.
Example 1:
Input: startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600 Output: 6 Explanation: The following are the possible ways to set the cooking time. - 1 0 0 0, interpreted as 10 minutes and 0 seconds. The finger is already on digit 1, pushes 1 (with cost 1), moves to 0 (with cost 2), pushes 0 (with cost 1), pushes 0 (with cost 1), and pushes 0 (with cost 1). The cost is: 1 + 2 + 1 + 1 + 1 = 6. This is the minimum cost. - 0 9 6 0, interpreted as 9 minutes and 60 seconds. That is also 600 seconds. The finger moves to 0 (with cost 2), pushes 0 (with cost 1), moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1). The cost is: 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 = 12. - 9 6 0, normalized as 0960 and interpreted as 9 minutes and 60 seconds. The finger moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1). The cost is: 2 + 1 + 2 + 1 + 2 + 1 = 9.
Example 2:
Input: startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76 Output: 6 Explanation: The optimal way is to push two digits: 7 6, interpreted as 76 seconds. The finger moves to 7 (with cost 1), pushes 7 (with cost 2), moves to 6 (with cost 1), and pushes 6 (with cost 2). The total cost is: 1 + 2 + 1 + 2 = 6 Note other possible ways are 0076, 076, 0116, and 116, but none of them produces the minimum cost.
Constraints:
0 <= startAt <= 91 <= moveCost, pushCost <= 1051 <= targetSeconds <= 6039When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy explores every possible combination of digits to see which one results in the minimum cost. We essentially try out all valid times to cook the food and then choose the cheapest option. It is an exhaustive search of all possibilities.
Here's how the algorithm would work step-by-step:
def min_cost_to_set_time(start_at, move_cost, push_cost, target_seconds):
minimum_cost = float('inf')
for minutes in range(100):
for seconds in range(100):
total_seconds = minutes * 60 + seconds
if total_seconds != target_seconds:
continue
# Build all possible button sequences
time_string = str(minutes).zfill(2) + str(seconds).zfill(2)
button_presses = []
leading_zero = True
for digit_char in time_string:
digit = int(digit_char)
if leading_zero and digit == 0:
continue
else:
leading_zero = False
button_presses.append(digit)
if not button_presses:
button_presses = [0] # handle zero case
current_cost = 0
current_position = start_at
for button in button_presses:
if button != current_position:
current_cost += move_cost
current_cost += push_cost
current_position = button
minimum_cost = min(minimum_cost, current_cost)
return minimum_costThe most efficient way to find the minimum cost is to carefully consider two possibilities: directly entering the time and converting the time into digit presses. We avoid exploring every single combination and instead focus on these two dominant strategies to determine the cheapest way.
Here's how the algorithm would work step-by-step:
def minimum_cost_to_set_cooking_time(start_at, move_cost, push_cost, target_seconds):
def calculate_cost(digits):
cost = 0
current_position = start_at
for digit in digits:
digit = int(digit)
if digit != current_position:
cost += move_cost
cost += push_cost
current_position = digit
return cost
# Direct entry: cost is based on the number of digits.
direct_entry_cost = len(str(target_seconds)) * push_cost
minimum_cost = direct_entry_cost
# Explore alternative time representations.
minutes = target_seconds // 60
seconds = target_seconds % 60
# Limit the valid time to avoid scenarios > 99.
if minutes > 99:
return minimum_cost
time_string = str(minutes * 100 + seconds)
# Only consider times with a valid number of digits.
if len(time_string) <= 4:
while len(time_string) < 4 and time_string[0] == '0' and len(time_string) > 1:
time_string = time_string[1:]
cost = calculate_cost(time_string)
minimum_cost = min(minimum_cost, cost)
# Check cost from seconds directly to minutes.
if target_seconds >= 60:
minutes = target_seconds // 60
seconds = target_seconds % 60
time_string = str(minutes).zfill(2) + str(seconds).zfill(2)
if int(str(minutes)) <= 99:
cost = calculate_cost(time_string)
minimum_cost = min(minimum_cost, cost)
return minimum_cost| Case | How to Handle |
|---|---|
| Target time is zero | Return the cost of pressing the button representing '0' directly, if allowed, or the cost to wait (if waiting is cheaper and allowed). |
| Button press costs are all zero | Find the shortest sequence regardless of the number of button presses, likely just pressing the digits of the target time. |
| Only one button is available | If it's '0', return cost * target time; otherwise, see if target time is divisible by button value, returning the corresponding cost or infinity. |
| Target time is extremely large | The solution should avoid integer overflow by checking against a reasonable maximum value for time, returning infinity or appropriate error if exceeded. |
| Target time requires more than 4 digits. | Return infinity or a similar sentinel value indicating that the time is unreachable, as it goes beyond the standard clock format. |
| Waiting is not allowed and some digits of the target time are unavailable as buttons | Return infinity indicating that the target time is unreachable because you cannot wait or form the required digits. |
| The '00' case, when the target time has leading zeros. | Calculate the cost of either waiting, using the direct digits or by using the special double zero feature if available; return the minimum cost |
| Integer overflow when calculating cost for a very long sequence of button presses | Use a data type large enough to hold the maximum possible cost (e.g., long long in C++, long in Java) to avoid overflow. |