C++ solution || Beginner Friendly || Explanation with code || O(1) solution
\nThe approach for this question is to generate all representations for the time and find the minimum cost.\n\nFirstly, we convert the time as follows:\n\nsecon
\nThe approach for this question is to generate all representations for the time and find the minimum cost.\n\n*Firstly, we convert the time as follows*:\n```\nseconds = targetSeconds%60;\nminutes = targetSeconds/60;\n```\n\n*This method always gives us seconds < 60.*\n\n***Edge Case:***\n\n* ***When we have minutes = 100, i.e, targetSeconds >= 6000.***\nSince, we can only represent 99 as max value for minutes, Here, we are forced to use 100 as 99 and add 60 seconds to seconds part ( This is the reason why targetSeconds <= 6039 is a constraint as max time is 9999 ).\n\nWe can observe that since 1 minute can be subtracted and 60 seconds can be added to seconds part gives us at most two representations of targetSeconds possible.\n\n**The situation where two types of representations are possible:**\n\nHere, second representation is possible when we reduce minutes by 1 and add the same 60 seconds to the seconds part. Hence, \n\n*CONDITION:*                         \n```\nminutes >=1  AND seconds <= 39  AND  minutes<100\n``` \n\nHere, **minutes>=1** as minutes-1 cannot be negative and adding 60 seconds to seconds part should be <=99. Hence, **seconds <= 39** as seconds + 60 <= 99. **minutes=100** are the edge cases handled seperately.\n\n**NOTE:** \n\n*In this question we should **only consider combinations with no leading zeroes as input in the string** such as "960" will only be considered and "0960" will not be considered. The latter will require pushing an extra button which increses the cost, so it will give higher cost.*\n\n### CODE:\n```\nclass Solution {\npublic:\n    int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n        \n        int secs = targetSeconds%60;\n        int mins = targetSeconds/60;\n        \n        string type1="", type2="";\n        \n        // If mins is 0 then we won\'t take any string from mins \n        // as it only increases push cost\n        if(mins==0) type1 = to_string(secs);\n\n        // If mins < 100  we convert into normal time format\n        else if(mins<100){\n            type1 = to_string(mins);\n            if(secs<=9) type1 += "0";\n            type1 += to_string(secs);\n        }\n\n        // If min=100 we have to represent it as 99:xy format\n        else{\n            type1 = "99";\n            type1 += to_string(secs + 60);\n        }\n             \n        // Condition where two types can be present   \n        if(mins>=1 && mins<100 && secs<=39){\n            if(mins-1==0) type2 = to_string(secs+60); //limiting leading zeroes\n            else{\n                type2 = to_string(mins-1);\n                type2 += to_string(secs+60);\n            }\n        }\n        \n        \n        int i = 1;\n        vector<string>arr;\n        arr.push_back(type1);   \n        // If type2 is there push in array \n        if(type2.size() > 0){ \n            arr.push_back(type2);\n            i++;\n        }\n\n        int mini = INT_MAX;\n        int totalCost = 0;\n        \n        while(i--){\n            \n            int n = arr[i][0]-\'0\';\n            totalCost = pushCost;\n            if(startAt != n) totalCost += moveCost;\n            \n            int j = 1;\n            \n            while(j < arr[i].size() ){\n                if(arr[i][j] != arr[i][j-1]) totalCost += moveCost;\n                totalCost += pushCost;\n                j++;\n            }\n            \n            mini = min(totalCost, mini);\n        }\n        \n        return mini;\n    }\n};\n```\n\n*Hope you liked the explanation!!*\n\n**Thanks for reading!!**\n\n\n
[]