Simple | O(1) both Time and Space Complexity
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves minimizing the cost of setting a target time on a microwave using 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves minimizing the cost of setting a target time on a microwave using the least number of finger movements and presses. The key to the solution is that there can be multiple valid ways to express a given number of seconds as minutes and seconds (e.g., 120 seconds can be 2 minutes or 1 minute and 60 seconds). Our goal is to calculate the cost for each possible configuration and return the minimum.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Understand the Input:\n\n- We are given startAt, moveCost, pushCost, and targetSeconds.\n- The goal is to find two possible representations of targetSeconds:\na) min1 and sec1 as the minutes and seconds.\nb) min2 and sec2 which might be another configuration of the time using an alternative minute-second split.\n\n2. Handling Time Splits:\n\n- Calculate min1 = targetSeconds / 60 and sec1 = targetSeconds % 60. This gives the first configuration.\n- If sec1 + 60 is valid (i.e., <= 99), we check if we can adjust min1 down by 1 minute and adjust sec1 upwards by 60 seconds, giving us min2 and sec2. This ensures that we explore both time representations.\n3. Cost Calculation:\n\n- Use a helper function cost to calculate the cost of typing the time on the microwave. This is done by checking the four-digit normalized form (prepend zeroes when necessary). The cost includes both moving the finger and pressing the buttons.\n- For each digit in the time representation, if it\'s the same as the current position of the finger, only the push cost is added. Otherwise, the move cost is added before pressing the button.\n4. Result:\n\n- Calculate the cost for both possible time configurations (min1, sec1 and min2, sec2), and return the minimum of the two.\n\n# Complexity\n- Time complexity:  O(1), since all calculations are based on simple arithmetic operations and we only check two possible time configurations.\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> \n\n- Space complexity: The space complexity is O(1) as we are using a constant amount of extra space for storing variables (digits, times, etc.).\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int cost(int min,int sec,int startAt, int moveCost, int pushCost){\n        vector<int> digits(4);\n        if(min == 0){\n            digits[0] = -1;\n            digits[1] = -1;\n        }\n        else if(min>9) {\n            digits[0] = min/10;\n            digits[1] = min%10;\n        }\n        else {\n            digits[0] = -1;\n            digits[1] = min;\n        }\n        if(min == 0 && sec <= 9){\n            digits[2] = -1;\n            digits[3] = sec;\n        }\n        else if(sec>9){\n            digits[2] = sec/10;\n            digits[3] = sec%10;\n        }\n        else{\n            digits[2] = 0;\n            digits[3] = sec;\n        }\n\n        int total = 0;\n\n        int curr_position = startAt;\n        for(int i=0;i<4;i++){\n            if(digits[i] == -1) continue;\n            else if(digits[i] == curr_position) total += pushCost;\n            else{\n                total += moveCost;\n                curr_position = digits[i];\n                total += pushCost;\n            }\n        }\n\n        return total;\n    }\n\n    int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n        int min1 = targetSeconds/60;\n        int sec1 = targetSeconds%60;\n        int min2 = -1;\n        int sec2 = -1;\n        if(sec1+60 <= 99 && min1>=1){\n            min2 = min1-1;\n            sec2 = sec1+60; \n        }\n        int cost1;\n        if(min1 > 99) cost1 = INT_MAX;\n        else cost1 = cost(min1,sec1,startAt,moveCost,pushCost);\n        int cost2 = INT_MAX;;\n        if(min2 != -1) cost2 = cost(min2,sec2,startAt,moveCost,pushCost);\n\n        return min(cost1,cost2);\n\n    }\n};\n```
['Math', 'Enumeration', 'C++']