[C++, Python, Java] [Direct] O(1) space & time (0 ms, faster than 100%) , Modular concise short
C++: (0ms , faster than 100% in time)\n\n\nclass Solution {\npublic:\n    \n    int calc(int min, int sec, int s, int m, int p){\n        if(min>99 || sec>99 ||
**C++: (0ms , faster than 100% in time)**\n\n```\nclass Solution {\npublic:\n    \n    int calc(int min, int sec, int s, int m, int p){\n        if(min>99 || sec>99 || min<0 || sec<0) return INT_MAX;   \n        \n        vector<int> v = {min/10, min%10, sec/10, sec%10};\n        vector<int> vv;\n\t\t\n        int i=0;\n        while(i<4 && v[i]==0) i++;\n        \n        // if(i==4) return 0; // not req as 0000 not possible acc to test case\n        \n        for(; i<4; i++) vv.push_back(v[i]);\n\n        int cost=0;\n        for(int e: vv){\n            if(e!=s) cost+=m;\n            s=e;\n            cost+=p;\n        }\n        \n        return cost;\n    }\n    \n    int minCostSetTime(int s, int m, int p, int t) {\n        int mins=t/60;\n        int secs= t-60*mins;\n        return min(calc(mins, secs, s, m, p), calc(mins-1, secs+60, s, m, p));\n    }\n};\n```\n\n**Java: (faster than 100% in time)**\n```\nclass Solution {\n    \n    public int calc(int min, int sec, int s, int m, int p){\n        if(min>99 || sec>99 || min<0 || sec<0) return Integer.MAX_VALUE;   \n        \n        int[] v = new int[]{min/10, min%10, sec/10, sec%10};        \n        List<Integer> vv = new ArrayList<Integer>();\n        int i=0;\n        while(i<4 && v[i]==0) i++;\n        \n        for(; i<4; i++){\n            vv.add(v[i]);\n        }\n\n        int cost=0;\n        for(int e: vv){\n            if(e!=s) cost+=m;\n            s=e;\n            cost+=p;\n        }\n        \n        return cost;\n    }\n    \n    public int minCostSetTime(int s, int m, int p, int t) {\n        int mins=t/60;\n        int secs= t- 60*mins;\n        return Math.min(calc(mins, secs,s, m, p), calc(mins-1, secs+60,s,m,p));        \n    }\n}\n```\n\n**Python: (faster than 100% in time & space both)**\n```\nclass Solution(object):\n    \n    def calc(self, min, sec, s,m,p):\n        if(min>99 or sec>99 or min<0 or sec<0):\n            return sys.maxint\n    \n        l=[min/10, min%10, sec/10, sec%10]\n        ll=[]\n        i=0\n        while(i<4 and l[i]==0):\n            i+=1\n        \n        for j in range(i,4):\n            ll.append(l[j])\n            \n        cost=0\n        \n        for e in ll:\n            if(e!=s):\n                cost+=m\n            s=e\n            cost+=p\n        \n        return cost;        \n     \n    def minCostSetTime(self, s, m, p, t):\n        mins= t/60\n        secs= t- 60*mins\n        return min(self.calc(mins, secs, s, m, p), self.calc(mins-1, secs+60, s, m, p))\n```\n\n**Time Complexity Analysis:**\nTime: O(1) \nSpace: O(1) \n\n**Please upvote if you liked it.**
['C']