Java | Easy
We have two choices:\n Convert targetSeconds to minutes and seconds as is\n Convert targetSeconds to minutes and seconds and try to move one minute to seconds (
We have two choices:\n* Convert `targetSeconds` to minutes and seconds as is\n* Convert `targetSeconds` to minutes and seconds and try to move one minute to seconds (if we have a room)\n\n**Hidden test case**: `minCostSetTime(0,1,1,6039)` - 99 mins, 99 seconds\nThe result is 100 mins 39 seconds, if we convert to minutes and seconds as is => We need to check if we use 4 digits only\n\n```\npublic int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n\tint mins = targetSeconds / 60;\n\tint secs = targetSeconds % 60;\n\n\t// convert to minutes and seconds as is\n\tint res = getCost(mins * 100 + secs, startAt, moveCost, pushCost);\n\n\t// try to move one minute to seconds\n\tif (secs + 60 <= 99 && mins > 0) {\n\t\tres = Math.min(res, getCost((mins - 1) * 100 + (secs + 60), startAt, moveCost, pushCost));\n\t}\n\n\treturn res;\n}\n\nprivate int getCost(int targetSeconds, int startAt, int moveCost, int pushCost) {\n\t// hidden test case, we can only have 4 digits, minCostSetTime(0,1,1,6039)\n\tif (targetSeconds > 9999) {\n\t\treturn Integer.MAX_VALUE;\n\t}\n\tint res = 0;\n\tvar ch = String.valueOf(targetSeconds).toCharArray();\n\tvar pre = (char) (startAt + \'0\');\n\tfor (int i = 0; i < ch.length; i++) {\n\t\tif (ch[i] != pre) {\n\t\t\tres += moveCost;\n\t\t}\n\t\tres += pushCost;\n\t\tpre = ch[i];\n\t}\n\treturn res;\n}\n```\n
[]