LeetCode in Kotlin

2162. Minimum Cost to Set Cooking Time

Medium

A generic microwave supports cooking times for:

To set the cooking time, you push at most four digits. The microwave normalizes what you push as four digits by prepending zeroes. It interprets the first two digits as the minutes and the last two digits as the seconds. It then adds them up as the cooking time. For example,

You are given integers startAt, moveCost, pushCost, and targetSeconds. Initially, your finger is on the digit startAt. Moving the finger above any specific digit costs moveCost units of fatigue. Pushing the digit below the finger once costs pushCost units of fatigue.

There can be multiple ways to set the microwave to cook for targetSeconds seconds but you are interested in the way with the minimum cost.

Return the minimum cost to set targetSeconds seconds of cooking time.

Remember that one minute consists of 60 seconds.

Example 1:

Input: startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600

Output: 6

Explanation: The following are the possible ways to set the cooking time.

The finger is already on digit 1, pushes 1 (with cost 1), moves to 0 (with cost 2), pushes 0 (with cost 1), pushes 0 (with cost 1), and pushes 0 (with cost 1).

The cost is: 1 + 2 + 1 + 1 + 1 = 6. This is the minimum cost.

The finger moves to 0 (with cost 2), pushes 0 (with cost 1), moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).

The cost is: 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 = 12.

The finger moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).

The cost is: 2 + 1 + 2 + 1 + 2 + 1 = 9.

Example 2:

Input: startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76

Output: 6

Explanation: The optimal way is to push two digits: 7 6, interpreted as 76 seconds.

The finger moves to 7 (with cost 1), pushes 7 (with cost 2), moves to 6 (with cost 1), and pushes 6 (with cost 2).

The total cost is: 1 + 2 + 1 + 2 = 6

Note other possible ways are 0076, 076, 0116, and 116, but none of them produces the minimum cost.

Constraints:

Solution

class Solution {
    fun minCostSetTime(startAt: Int, moveCost: Int, pushCost: Int, targetSeconds: Int): Int {
        val mins = targetSeconds / 60
        val secs = targetSeconds % 60
        return Math.min(
            cost(mins, secs, startAt, moveCost, pushCost),
            cost(mins - 1, secs + 60, startAt, moveCost, pushCost)
        )
    }

    private fun cost(mins: Int, secs: Int, startAt: Int, moveCost: Int, pushCost: Int): Int {
        if (mins > 99 || secs > 99 || mins < 0 || secs < 0) {
            return Int.MAX_VALUE
        }
        val s = Integer.toString(mins * 100 + secs)
        var curr = (startAt + '0'.code).toChar()
        var res = 0
        for (i in 0 until s.length) {
            if (s[i] == curr) {
                res += pushCost
            } else {
                res += pushCost + moveCost
                curr = s[i]
            }
        }
        return res
    }
}