Java Solution | Precompute everything | Short explanation
I am checking for every clockTime starting from 00:01 to 99:99 in the oven what is the total seconds and store it in a hashmap.\n\nSo my HashMap stores every po
I am checking for every clockTime starting from 00:01 to 99:99 in the oven what is the total seconds and store it in a hashmap.\n\nSo my HashMap stores every possible way to reach a targetSecond.\n\nexample : hasmap[76] = { 076 , 76 , 0076 , 0116 , 116 };\n\nNext as per the targetSecond in problemStatement i simply iterate over the values and return the answer :)\n\n```\nclass Solution {\n    public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n        int ans = Integer.MAX_VALUE;\n        int maxLen = 4;\n        HashMap<Integer , List<String>> map = new HashMap<>();\n        for (int i=1 ; i<=9999 ; i++) {\n            int seconds = getSeconds(i);\n            if (!map.containsKey(seconds)) {\n                map.put(seconds, new ArrayList<>());\n            }\n            map.get(seconds).add(String.valueOf(i));\n            int paddedZeroes = maxLen - String.valueOf(i).length();\n            String s = ""+i;\n            for (int k=0 ; k<paddedZeroes ; k++) {\n                s = "0"+s;\n                map.get(seconds).add(s);\n            }\n        }\n\n        for (String unit : map.get(targetSeconds)) {\n            char ch[] = unit.toCharArray();\n            int currentCost = 0;\n            int prevDigit = startAt;\n            for (int k = 0; k < ch.length; k++) {\n                if (ch[k] - \'0\' != prevDigit) {\n                    currentCost += moveCost;\n                }\n                currentCost += pushCost;\n                prevDigit = ch[k] - \'0\';\n            }\n            ans = Math.min(ans, currentCost);\n        }\n\n        return ans;\n    }\n\n    private int getSeconds(int i) {\n        String s = ""+i;\n        if (s.length() <= 2)\n            return i;\n        if (s.length() == 3) {\n            String min = s.substring(0, 1);\n            String sec = s.substring(1);\n            return Integer.parseInt(min) * 60 + Integer.parseInt(sec);\n        } else {\n            String min = s.substring(0, 2);\n            String sec = s.substring(2);\n            return Integer.parseInt(min) * 60 + Integer.parseInt(sec);\n        }\n    }\n}\n```
[]