# 设置时间的最少代价
/**
* @author yagol
* @date 下午11:02
* @desc the description of this class
**/
class Solution5986 {
public static void main(String[] args) {
System.out.println(new Solution5986().minCostSetTime(
0, 1, 1, 6039
));
}
public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
int min = targetSeconds / 60;
int sec = targetSeconds % 60;
String check = min + "" + sec;
if (sec < 10) {
check = min + "0" + sec;
}
int method1 = Integer.MAX_VALUE;
if (min<=99){
method1 = calculateCost(check, startAt, moveCost, pushCost);
}
int method2 = Integer.MAX_VALUE;
if (sec + 60 <= 99) {
check = (min - 1) + "" + (sec + 60);
method2 = calculateCost(check, startAt, moveCost, pushCost);
}
return Math.min(method2, method1);
}
public int calculateCost(String check, int start, int move, int push) {
check = Integer.valueOf(check) + "";
System.out.println("check number is: " + check);
char[] data = check.toCharArray();
int moveCount = 0;
int pushCount = 0;
if (data[0] == String.valueOf(start).toCharArray()[0]) {
moveCount--;
}
char lastLocation = data[0];
for (int i = 0; i < data.length; i++) {
moveCount++;
pushCount++;
if (i >= 1) {
if (lastLocation == data[i]) {
moveCount--;
}
}
lastLocation = data[i];
}
int value = move * moveCount + push * pushCount;
System.out.println("value is :" + value);
return value;
}
}
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56