✅O(1) | Complete explanation with examples | 6 total cases | C++
There are total 6 edge cases possible. \nLet me explain with examples for clear understanding\n\nI\'m making a string, which will represent time of microwave di
There are total 6 edge cases possible. \nLet me explain with examples for clear understanding\n\nI\'m making a string, which will represent time of microwave display.\n```\nseconds = targetSeconds%60;\nminutes = targetSeconds/60;\n```\n```\nstring = minutes + seconds\n```\n\nWhen we have targetSeconds = 611, then it is - 10 minutes and 11 seconds == "10:11".\nBut it can also be written as "9:71", as total number of seconds remain same.\n\nNote1 : If minutes <= 9, lest say minutes=8, then it can also be written as "08", which is a new pattern representing same time.\nNote2 : if seconds <= 39, then a pattern with (minute-1) can be formed as shown in above example, but if seconds are > 39, then that can\'t be represent in this way.\n\nTotal 6 cases are there, I represented them as string p1, p2, p3, p4,p5, p6\nBy seeing the code, its self explanatory that what different cases these strings are holding. Also, see the below examples.\n\nWhen targetSeconds = 76  == 1 min + 16 sec\n```\np1 = 116      ==   1:16\np4 = 0116     ==   01:16\np2 = 076\np3 = 0076\np5 = 76\np6 = \n```\n\nWhen targetSeconds = 6039  ==  100 min + 39 sec\n```\np1 = \np4 = \np2 = 9999\np3 = \np5 = \np6 = \n```\n\nWhen targetSeconds = 600  == 10 min\n```\np1 = 1000\np4 = \np2 = 960\np3 = 0960\np5 = \np6 = \n```\n\nWhen targetSeconds = 19  == 19 sec\n```\np1 = 019\np4 = 0019\np2 = \np3 = \np5 = \np6 = 19\n```\n\n# CODE \n\n```\nclass Solution {\npublic:\n    int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n        \n        int seconds = targetSeconds%60;\n        int minutes = targetSeconds/60;\n        \n        string p1="", p2="", p3="", p4="",p5="", p6="";\n        \n        p1 = to_string(minutes);\n        if(seconds <= 9) p1 += "0";\n        p1 += to_string(seconds);\n        \n        if(minutes == 100) p1="";\n        \n        if(minutes <= 9){\n            p4="0";\n            p4 += p1;\n        }\n        \n        if(seconds <= 39 and minutes >= 1){\n            p2 = to_string(minutes-1);\n            p2 += to_string(60 + seconds);\n        }\n        \n        if(p2.length() > 0){\n            if(minutes - 1 <= 9){\n                p3 = "0";\n                p3 += p2;\n            }\n            if(minutes-1 == 0){\n                p5 = to_string(60+seconds);\n            }\n        }\n        \n        if(minutes == 0) p6=to_string(seconds);\n        \n        \n        vector<string>arr;\n        if(p1.size() > 0) arr.push_back(p1);    \n        if(p2.size() > 0) arr.push_back(p2);\n        if(p3.size() > 0) arr.push_back(p3);\n        if(p4.size() > 0) arr.push_back(p4);\n        if(p5.size() > 0) arr.push_back(p5);\n        if(p6.size() > 0) arr.push_back(p6);\n\n        \n\t\t// now, the main calculation according to question\n        int mn = INT_MAX;\n        int cost;\n        for(string str : arr){\n            int x = str[0]-\'0\';\n            cost = 0;\n            if(startAt == x) cost += pushCost;\n            else cost += moveCost + pushCost;\n            \n            for(int i=1; i<str.length(); i++){\n                if(str[i] == str[i-1]) cost += pushCost;\n                else cost += moveCost + pushCost;\n            }\n            mn = min(cost, mn);\n        }\n        \n        return mn;\n    }\n};\n```\n\nI know that its a frustrating question. Most of my time was spent in it. \n\n**Please Upvote**
[]