Skip to content
LC-2162 Medium LeetCode

2162. Minimum Cost to Set Cooking Time

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 41% Topics: Math, Enumeration
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(1)
# Space: O(1)

# simulation
class Solution(object):
    def minCostSetTime(self, startAt, moveCost, pushCost, targetSeconds):
        """
        :type startAt: int
        :type moveCost: int
        :type pushCost: int
        :type targetSeconds: int
        :rtype: int
        """     
        def cost(m, s):
            if not (0 <= m <= 99 and s <= 99):
                return float("inf")
            result = 0
            curr = startAt
            for x in map(int, list(str(m*100 + s))):
                result += (moveCost if x != curr else 0)+pushCost
                curr = x
            return result

        m, s = divmod(targetSeconds, 60)
        return min(cost(m, s), cost(m-1, s+60))

Solution from kamyu104/LeetCode-Solutions · MIT