2162. Minimum Cost to Set Cooking Time LeetCode Solution
In this guide, you will get 2162. Minimum Cost to Set Cooking Time LeetCode Solution with the best time and space complexity. The solution to Minimum Cost to Set Cooking Time problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Cost to Set Cooking Time solution in C++
- Minimum Cost to Set Cooking Time solution in Java
- Minimum Cost to Set Cooking Time solution in Python
- Additional Resources

Problem Statement of 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:
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 <= 9
1 <= moveCost, pushCost <= 105
1 <= targetSeconds <= 6039
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
2162. Minimum Cost to Set Cooking Time LeetCode Solution in C++
class Solution {
public:
int minCostSetTime(int startAt, int moveCost, int pushCost,
int targetSeconds) {
int ans = INT_MAX;
int mins = targetSeconds > 5999 ? 99 : targetSeconds / 60;
int secs = targetSeconds - mins * 60;
auto getCost = [&](int mins, int secs) -> int {
int cost = 0;
char curr = '0' + startAt;
for (const char c : to_string(mins * 100 + secs))
if (c == curr) {
cost += pushCost;
} else {
cost += moveCost + pushCost;
curr = c;
}
return cost;
};
while (secs < 100) {
ans = min(ans, getCost(mins, secs));
--mins;
secs += 60;
}
return ans;
}
};
/* code provided by PROGIEZ */
2162. Minimum Cost to Set Cooking Time LeetCode Solution in Java
class Solution {
public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
int ans = Integer.MAX_VALUE;
int mins = targetSeconds > 5999 ? 99 : targetSeconds / 60;
int secs = targetSeconds - mins * 60;
while (secs < 100) {
ans = Math.min(ans, getCost(startAt, moveCost, pushCost, mins, secs));
--mins;
secs += 60;
}
return ans;
}
private int getCost(int startAt, int moveCost, int pushCost, int mins, int secs) {
int cost = 0;
char curr = (char) ('0' + startAt);
for (final char c : String.valueOf(mins * 100 + secs).toCharArray())
if (c == curr) {
cost += pushCost;
} else {
cost += moveCost + pushCost;
curr = c;
}
return cost;
};
}
// code provided by PROGIEZ
2162. Minimum Cost to Set Cooking Time LeetCode Solution in Python
class Solution:
def minCostSetTime(
self,
startAt: int,
moveCost: int,
pushCost: int,
targetSeconds: int,
) -> int:
ans = math.inf
mins = 99 if targetSeconds > 5999 else targetSeconds // 60
secs = targetSeconds - mins * 60
def getCost(mins: int, secs: int) -> int:
cost = 0
curr = str(startAt)
for c in str(mins * 100 + secs):
if c == curr:
cost += pushCost
else:
cost += moveCost + pushCost
curr = c
return cost
while secs < 100:
ans = min(ans, getCost(mins, secs))
mins -= 1
secs += 60
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.