Java | 0ms | with explanation
Implement a function that calculates the cost given mins & secs.\n\nclass Solution {\n    int moveCost = 0, pushCost = 0;\n    public int minCostSetTime(int sta
Implement a function that calculates the cost given mins & secs.\n```\nclass Solution {\n    int moveCost = 0, pushCost = 0;\n    public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n        this.moveCost = moveCost;\n        this.pushCost = pushCost;\n        int mins = targetSeconds / 60;\n        int secs = targetSeconds % 60;\n        // edge case: mins == 100. The only possible representation is 99:xx, xx = secs + 60\n        if (mins == 100) {\n            return cost(mins - 1, secs + 60, startAt);\n        } \n        \n        // Multiple representations case: when mins is not 0, and secs is less than 40. \n        // eg: 5:12 has two representations: 1. 5:12 (regular) 2. 4: 72 (mins - 1, secs + 60)        \n        if (mins != 0 && secs < 40) {\n            return Math.min(cost(mins, secs, startAt), cost(mins - 1, secs + 60, startAt));\n        } \n        // Note when mins == 0, only one representation exists (0, secs);\n        // Or when secs >= 40, only one representation exists b/c if we add 60 to secs, the \n        // results will exceeds 100, which is invalid.\n        return cost(mins, secs, startAt);\n    }\n    \n    private int cost(int mins, int secs, int startAt) {\n        int res = 0;\n        // When mins >= 10, first digit is valid\n        if (mins >= 10) {\n            int firstDigit = mins / 10;\n            if (firstDigit != startAt) {\n                startAt = firstDigit;\n                res += moveCost;\n            }\n            res += pushCost;\n        }\n        // When mins != 0, then the second digit is valid\n        if (mins != 0) {\n            int secondDigit = mins % 10;\n            if (startAt != secondDigit) {\n                startAt = secondDigit;\n                res += moveCost;\n            }\n            res += pushCost;\n        }\n        \n        // When mins is not 0 or secs >= 10, the third digit is valid\n        if (secs >= 10 || mins != 0) {\n            int thirdDigit = secs / 10;\n            if (startAt != thirdDigit) {\n                startAt = thirdDigit;\n                res += moveCost;\n            }\n            res += pushCost;\n        }\n        \n        // The last digit is always valid\n        if (startAt != (secs % 10)) res += moveCost;\n        res += pushCost;\n        return res;\n    } \n}\n```
['Java']