Minimum Cost to Set Cooking Time LeetCode solution in Java

1
class Solution {
2
public int minCostSetTime(int startAt, int moveCost, int pushCost, int tar) {
3

4
int min = tar / 60, sec = tar % 60, minCost = (moveCost + pushCost) * 4;
5

6
if (min > 99) {
7
min--;
8
sec += 60;
9
} // this is required to do because if tar>=6000 then min is 100 which is not possible as it
10
// atmost can be 99mins
11

12
while (min >= 0 && sec <= 99) { // this while loop will work for atmost 2 iterations
13
tar = min * 100 + sec;
14
char arr[] = ("" + tar).toCharArray();
15
int sameMove = 0;
16
for (int i = 0; i < arr.length - 1; i++) if (arr[i] == arr[i + 1]) sameMove++;
17
if (startAt == arr[0] - '0')
18
minCost = Math.min(minCost, pushCost * arr.length + moveCost * (arr.length - 1 - sameMove));
19
else minCost = Math.min(minCost, pushCost * arr.length + moveCost * (arr.length - sameMove));
20
min--;
21
sec += 60;
22
}
23
return minCost;
24
}
25
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0