forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
27 lines (26 loc) · 836 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;
}
}