Practice/Google/Leetcode 2162. Minimum Cost to Set Cooking Time
Leetcode 2162. Minimum Cost to Set Cooking Time
CodingMust
Problem
You're programming a microwave with a numeric keypad (digits 0-9). Your finger starts at a given digit startAt. To set the cooking time, you need to input a time in MM:SS format (minutes and seconds), but you don't actually press a colon - you just enter up to 4 digits that represent MMSS.
Each action has an associated fatigue cost:
- Pressing a digit costs
pushCost units of fatigue
- Moving your finger from one digit to a different digit costs
moveCost units of fatigue (staying on the same digit between presses costs 0)
Given a target cooking duration in seconds (targetSeconds), determine the minimum total fatigue cost needed to input a valid time representation.
Valid time constraints:
- Minutes must be between 0 and 99 (inclusive)
- Seconds must be between 0 and 99 (inclusive)
- The total time (minutes × 60 + seconds) must equal
targetSeconds
- You can omit leading zeros (e.g., for 3:05 you can enter "305" instead of "0305")
Requirements
- Find all valid ways to represent
targetSeconds as MM:SS format
- For each valid representation, calculate the total fatigue cost
- Return the minimum cost among all valid representations
- Handle cases where multiple time representations are possible
Constraints
- 0 ≤
startAt ≤ 9
- 1 ≤
moveCost, pushCost ≤ 10⁵
- 0 ≤
targetSeconds ≤ 5999 (maximum is 99:59)
- Time complexity should be O(1) since we have at most 100 possible minute/second splits to check
Examples
Example 1:
`
Input: startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600
Output: 6
Explanation:
- 600 seconds = 10 minutes and 0 seconds
- We can enter "1000" (10:00)
- Starting at 1: press 1 (cost=1), move to 0 (cost=2), press 0 (cost=1), stay at 0 (cost=0), press 0 (cost=1), stay at 0 (cost=0), press 0 (cost=1)
- Total: 1 + 2 + 1 + 1 + 1 = 6
`
Example 2:
`
Input: startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76
Output: 6
Explanation:
- 76 seconds can be represented as:
- "116" (1:16): move 0→1 (cost=1) + push 1 (cost=2) + stay (cost=0) + push 1 (cost=2) + move 1→6 (cost=1) + push 6 (cost=2) = 8
- "0076" (0:76): push 0 (cost=2) + stay (cost=0) + push 0 (cost=2) + move 0→7 (cost=1) + push 7 (cost=2) + move 7→6 (cost=1) + push 6 (cost=2) = 10
- Wait, let me recalculate "116": We start at 0, move to 1 (1), push 1 (2), stay at 1 (0), push 1 (2), move to 6 (1), push 6 (2) = 8
- The minimum is actually from carefully checking both options
`
Example 3:
`
Input: startAt = 5, moveCost = 3, pushCost = 4, targetSeconds = 0
Output: 7
Explanation:
- 0 seconds can only be entered as "0" (single digit representing 0:00)
- Move from 5 to 0 (cost=3) + push 0 (cost=4) = 7
`