Minimum Cost to Set Cooking Time - Problem

A generic microwave supports cooking times for:

  • at least 1 second.
  • at most 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:

  • You push 9 5 4 (three digits). It is normalized as 0954 and interpreted as 9 minutes and 54 seconds.
  • You push 0 0 0 8 (four digits). It is interpreted as 0 minutes and 8 seconds.
  • You push 8 0 9 0. It is interpreted as 80 minutes and 90 seconds.
  • You push 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.

Input & Output

Example 1 — Basic Case
$ Input: startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600
Output: 6
💡 Note: 600 seconds = 10:00. Input digits [1,0,0,0]: move to 1→0 (cost 2), push 0 (cost 1), push 0 (cost 1), push 0 (cost 1). Total = 2+1+1+1 = 5. Wait, let me recalculate: we start at 1, move to 1 (cost 0), push 1 (cost 1), move to 0 (cost 2), push 0 (cost 1), push 0 (cost 1), push 0 (cost 1). Total = 0+1+2+1+1+1 = 6.
Example 2 — Multiple Options
$ Input: startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76
Output: 5
💡 Note: 76 seconds can be 01:16 or 00:76. For 01:16 → [0,1,1,6]: stay at 0 (cost 0), push 0 (cost 2), move to 1 (cost 1), push 1 (cost 2), stay at 1 (cost 0), push 1 (cost 2), move to 6 (cost 1), push 6 (cost 2). Total = 0+2+1+2+0+2+1+2 = 10. For 00:76 → [0,0,7,6]: we can use this instead, but 76 > 59 so invalid. Actually 01:16 is correct.
Example 3 — Same Digit Sequence
$ Input: startAt = 9, moveCost = 4, pushCost = 1, targetSeconds = 3600
Output: 7
💡 Note: 3600 seconds = 60:00. Input digits [6,0,0,0]: move from 9→6 (cost 4), push 6 (cost 1), move to 0 (cost 4), push 0 (cost 1), stay at 0, push 0 (cost 1), stay at 0, push 0 (cost 1). Total = 4+1+4+1+1+1 = 12. Wait let me recalculate more carefully.

Constraints

  • 0 ≤ startAt ≤ 9
  • 1 ≤ moveCost, pushCost ≤ 105
  • 1 ≤ targetSeconds ≤ 6039

Visualization

Tap to expand
Microwave Timer: Find Minimum Cost to Input 125 secondsTarget Time125 secondsValid Representations02:05 → [0,2,0,5]01:65 → [0,1,6,5]Minimum CostCheaper optionStart at digit: 1moveCost: 2, pushCost: 1Compare both optionsCalculate input cost for each digit sequenceCost = (moves × moveCost) + (pushes × pushCost)Output: Minimum cost to input target time
Understanding the Visualization
1
Input
Target: 125 seconds, Start at digit 1
2
Convert
125s → 02:05 or 01:65 formats
3
Calculate
Find cheapest digit sequence to input
Key Takeaway
🎯 Key Insight: Any cooking time has at most 2 valid input representations - choose the cheaper one
Asked in
Google 15 Amazon 12 Microsoft 8
18.5K Views
Medium Frequency
~25 min Avg. Time
456 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen