Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 <= 6039Problem summary: 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
1 2 1 600
0 1 2 76
minimum-time-difference)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2162: Minimum Cost to Set Cooking Time
class Solution {
public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
int m = targetSeconds / 60;
int s = targetSeconds % 60;
return Math.min(
f(m, s, startAt, moveCost, pushCost), f(m - 1, s + 60, startAt, moveCost, pushCost));
}
private int f(int m, int s, int prev, int moveCost, int pushCost) {
if (m < 0 || m > 99 || s < 0 || s > 99) {
return Integer.MAX_VALUE;
}
int[] arr = new int[] {m / 10, m % 10, s / 10, s % 10};
int i = 0;
for (; i < 4 && arr[i] == 0; ++i)
;
int t = 0;
for (; i < 4; ++i) {
if (arr[i] != prev) {
t += moveCost;
}
t += pushCost;
prev = arr[i];
}
return t;
}
}
// Accepted solution for LeetCode #2162: Minimum Cost to Set Cooking Time
func minCostSetTime(startAt int, moveCost int, pushCost int, targetSeconds int) int {
m, s := targetSeconds/60, targetSeconds%60
f := func(m, s int) int {
if m < 0 || m > 99 || s < 0 || s > 99 {
return 0x3f3f3f3f
}
arr := []int{m / 10, m % 10, s / 10, s % 10}
i := 0
for ; i < 4 && arr[i] == 0; i++ {
}
t := 0
prev := startAt
for ; i < 4; i++ {
if arr[i] != prev {
t += moveCost
}
t += pushCost
prev = arr[i]
}
return t
}
return min(f(m, s), f(m-1, s+60))
}
# Accepted solution for LeetCode #2162: Minimum Cost to Set Cooking Time
class Solution:
def minCostSetTime(
self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int
) -> int:
def f(m, s):
if not 0 <= m < 100 or not 0 <= s < 100:
return inf
arr = [m // 10, m % 10, s // 10, s % 10]
i = 0
while i < 4 and arr[i] == 0:
i += 1
t = 0
prev = startAt
for v in arr[i:]:
if v != prev:
t += moveCost
t += pushCost
prev = v
return t
m, s = divmod(targetSeconds, 60)
ans = min(f(m, s), f(m - 1, s + 60))
return ans
// Accepted solution for LeetCode #2162: Minimum Cost to Set Cooking Time
/**
* [2162] Minimum Cost to Set Cooking Time
*
* 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.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/12/30/1.png" style="width: 506px; height: 210px;" />
* 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/12/30/2.png" style="width: 505px; height: 73px;" />
* 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 <= 9
* 1 <= moveCost, pushCost <= 10^5
* 1 <= targetSeconds <= 6039
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-cost-to-set-cooking-time/
// discuss: https://leetcode.com/problems/minimum-cost-to-set-cooking-time/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn min_cost_set_time(
start_at: i32,
move_cost: i32,
push_cost: i32,
target_seconds: i32,
) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2162_example_1() {
let start_at = 1;
let move_cost = 2;
let push_cost = 1;
let target_seconds = 600;
let result = 6;
assert_eq!(
Solution::min_cost_set_time(start_at, move_cost, push_cost, target_seconds),
result
);
}
#[test]
#[ignore]
fn test_2162_example_2() {
let start_at = 0;
let move_cost = 1;
let push_cost = 2;
let target_seconds = 76;
let result = 6;
assert_eq!(
Solution::min_cost_set_time(start_at, move_cost, push_cost, target_seconds),
result
);
}
}
// Accepted solution for LeetCode #2162: Minimum Cost to Set Cooking Time
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2162: Minimum Cost to Set Cooking Time
// class Solution {
// public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
// int m = targetSeconds / 60;
// int s = targetSeconds % 60;
// return Math.min(
// f(m, s, startAt, moveCost, pushCost), f(m - 1, s + 60, startAt, moveCost, pushCost));
// }
//
// private int f(int m, int s, int prev, int moveCost, int pushCost) {
// if (m < 0 || m > 99 || s < 0 || s > 99) {
// return Integer.MAX_VALUE;
// }
// int[] arr = new int[] {m / 10, m % 10, s / 10, s % 10};
// int i = 0;
// for (; i < 4 && arr[i] == 0; ++i)
// ;
// int t = 0;
// for (; i < 4; ++i) {
// if (arr[i] != prev) {
// t += moveCost;
// }
// t += pushCost;
// prev = arr[i];
// }
// return t;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.