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
Minimum Cost to Set Cooking Time INPUT MICROWAVE 1 2 3 4 5 6 7 8 9 0 ^finger startAt = 1 moveCost = 2 pushCost = 1 targetSeconds = 600 Target: 10 minutes 0 seconds ALGORITHM STEPS 1 Generate Valid Times Find all MM:SS combos Option 1: 10:00 (1000) Option 2: 09:60 (0960) Both = 600 seconds 2 Calculate Cost: 1000 From digit 1, push: 1,0,0,0 Push 1: 0 move + 1 push = 1 Push 0,0,0: 2 move + 3 push = 5 Total: 6 3 Calculate Cost: 0960 From digit 1, push: 9,6,0 Push 9: 2 move + 1 push = 3 Push 6,0: 4 move + 2 push = 6 Total: 9 4 Compare and Return Min min(6, 9) = 6 FINAL RESULT 10:00 Optimal Sequence Start at: 1 Push 1: cost 1 Move to 0: cost 2 Push 0,0,0: cost 3 Output: 6 Minimum fatigue cost OK - Verified Key Insight: For any target time, there can be at most TWO valid representations: MM:SS and (MM-1):(SS+60). We calculate the cost for each by tracking finger position and counting moves vs pushes. Starting position matters: if already on target digit, no move cost needed - just push cost. TutorialsPoint - Minimum Cost to Set Cooking Time | Generate Valid Time Representations Approach
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