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 954 (three digits). It is normalized as 0954 and interpreted as 9 minutes and 54 seconds.
You push 0008 (four digits). It is interpreted as 0 minutes and 8 seconds.
You push 8090. It is interpreted as 80 minutes and 90 seconds.
You push 8130. 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 theminimum cost to settargetSecondsseconds of cooking time.

Input: startAt =1, moveCost =2, pushCost =1, targetSeconds =600Output: 6Explanation: The following are the possible ways to set the cooking time.-1000, 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.-0960, 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.-960, 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.

Input: startAt =0, moveCost =1, pushCost =2, targetSeconds =76Output: 6Explanation: The optimal way is to push two digits:76, 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=6Note other possible ways are 0076,076,0116, and 116, but none of them produces the minimum cost.
To minimize the cost, we enumerate all possible ways to represent the target time in minutes and seconds (within the microwave’s constraints), then simulate the process of entering each representation and calculate the cost. We pick the representation with the lowest cost.
For each possible (minutes, seconds) such that 0 <= minutes <= 99, 0 <= seconds <= 99, and 1 <= total_seconds = minutes * 60 + seconds == targetSeconds:
Generate the 4-digit string representation.
Simulate entering the digits, tracking finger movement and pushes.
Calculate the total cost for this representation.
Return the minimum cost among all valid representations.
classSolution {
public:int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
int ans = INT_MAX;
for (int m =0; m <=99; ++m) {
int s = targetSeconds - m *60;
if (s <0|| s >99) continue;
string t = to_string(m *100+ s);
while (t.size() <4) t ="0"+ t;
int cur =0, pos = startAt;
for (char c : t) {
int d = c -'0';
if (pos != d) cur += moveCost;
cur += pushCost;
pos = d;
}
ans = min(ans, cur);
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
funcminCostSetTime(startAt, moveCost, pushCost, targetSecondsint) int {
ans:=1<<31-1form:=0; m<=99; m++ {
s:=targetSeconds-m*60ifs < 0||s > 99 { continue }
t:=fmt.Sprintf("%04d", m*100+s)
cur, pos:=0, startAtfori:=0; i < 4; i++ {
d:= int(t[i] -'0')
ifpos!=d { cur+=moveCost }
cur+=pushCostpos = d }
ifcur < ans { ans = cur }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
publicintminCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
int ans = Integer.MAX_VALUE;
for (int m = 0; m <= 99; ++m) {
int s = targetSeconds - m * 60;
if (s < 0 || s > 99) continue;
String t = String.format("%04d", m * 100 + s);
int cur = 0, pos = startAt;
for (char c : t.toCharArray()) {
int d = c -'0';
if (pos != d) cur += moveCost;
cur += pushCost;
pos = d;
}
ans = Math.min(ans, cur);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funminCostSetTime(startAt: Int, moveCost: Int, pushCost: Int, targetSeconds: Int): Int {
var ans = Int.MAX_VALUE
for (m in0..99) {
val s = targetSeconds - m * 60if (s < 0|| s > 99) continueval t = ("%04d").format(m * 100 + s)
var cur = 0; var pos = startAt
for (c in t) {
val d = c - '0'if (pos != d) cur += moveCost
cur += pushCost
pos = d
}
ans = minOf(ans, cur)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defminCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:
ans = float('inf')
for m in range(100):
s = targetSeconds - m *60if s <0or s >99:
continue t =f"{m*100+s:04d}" cur, pos =0, startAt
for c in t:
d = int(c)
if pos != d:
cur += moveCost
cur += pushCost
pos = d
ans = min(ans, cur)
return ans
impl Solution {
pubfnmin_cost_set_time(start_at: i32, move_cost: i32, push_cost: i32, target_seconds: i32) -> i32 {
letmut ans =i32::MAX;
for m in0..100 {
let s = target_seconds - m *60;
if s <0|| s >99 { continue; }
let t =format!("{:04}", m *100+ s);
letmut cur =0;
letmut pos = start_at;
for c in t.chars() {
let d = c.to_digit(10).unwrap() asi32;
if pos != d { cur += move_cost; }
cur += push_cost;
pos = d;
}
ans = ans.min(cur);
}
ans
}
}