Minimum Cost to Set Cooking Time LeetCode solution in Python

1
class Solution:
2
def minCostSetTime(
3
self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int
4
) -> int:
5
poss = [
6
(targetSeconds // 60, targetSeconds % 60)
7
] # store possibilities as (minutes, seconds)
8

9
if poss[0][0] > 99: # for when targetSeconds >= 6000
10
poss = [(99, poss[0][1] + 60)]
11

12
if poss[0][0] >= 1 and (poss[0][1] + 60) <= 99:
13
# adding a second possibility e.g. (01, 16) -> (0, 76)
14
poss.append((poss[0][0] - 1, poss[0][1] + 60))
15

16
costs = list()
17

18
for i in poss:
19
curr_start = startAt
20
curr_cost = 0
21

22
minutes = str(i[0])
23
if i[0] != 0: # 0s are prepended, so no need to push 0s
24
for j in minutes:
25
if int(j) != curr_start:
26
curr_cost += moveCost
27
curr_start = int(j)
28
curr_cost += pushCost
29

30
seconds = str(i[1])
31
if (
32
len(seconds) == 1 and i[0] != 0
33
): # seconds is a single digit, prepend a "0" to it
34
seconds = "0" + seconds
35

36
for j in seconds:
37
if int(j) != curr_start:
38
curr_cost += moveCost
39
curr_start = int(j)
40
curr_cost += pushCost
41
costs.append(curr_cost)
42

43
return min(costs)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0