question_slug
stringlengths 3
77
| title
stringlengths 1
183
| slug
stringlengths 12
45
| summary
stringlengths 1
160
⌀ | author
stringlengths 2
30
| certification
stringclasses 2
values | created_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| updated_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| hit_count
int64 0
10.6M
| has_video
bool 2
classes | content
stringlengths 4
576k
| upvotes
int64 0
11.5k
| downvotes
int64 0
358
| tags
stringlengths 2
193
| comments
int64 0
2.56k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
minimum-cost-to-set-cooking-time
|
C++ || Brute Force Solution with Detailed Explanation
|
c-brute-force-solution-with-detailed-exp-shim
|
The main aim of the solution is to generate all possible ways to generate the required time, and then calculating the cost of each of them while returning the m
|
o_disdain_o
|
NORMAL
|
2022-02-05T16:45:04.670397+00:00
|
2022-03-06T11:32:03.555037+00:00
| 1,570 | false |
The main aim of the solution is to generate all possible ways to generate the required time, and then calculating the cost of each of them while returning the minimum one. \n\n***Note***: I am not actually generating all the possible outcomes, like for target as 600second, when taken as 9 mins 60 second, the possible buttons to be pushed can be *"0960"* and *"960"*. But I am not generating *"0960"* as it will require pushing an extra button which will obviously increase the cost, so no point in considering it. \n\n**Explaination of Code** \nSection 1:\nHere the target seconds are reduced till they are 2 digits or less, if they are of 3 digits or more, they can not be put in the timer as only 2 digits are allowed in microwave for seconds. \nEg: for 600 seconds will reduced to 9 mins, 60 second, (mins = 9, tar = 60)\n\nSection 2:\nHere I am generating different ways of pushing buttons in form of string by reducing seconds(*tar*) and increasing minutes(*mins*)\nEg: "960" is pushed as a possible string in vector *ways*, mins = 10, and tar = 0, which leads to exiting the loop\n\nSection 3:\nSection 2 ends when seconds are less than 60(*tar<60*), So now, if there is a possible way(mins < 100) that can be consi, I am storing it in the *ways* vector\nEg: "1000" is pushed as a possible string in vector *ways*\n\nSection 4:\nFor all possible ways(strings in *ways*) we calculate the cost, and then return the minimum one\n```\nclass Solution {\npublic:\n int minCostSetTime(int start, int move, int push, int tar) {\n vector<string> ways;\n int mins = 0; \n\t\t\n\t\t//Section 1\n while(tar >= 100)\n {\n mins++;\n tar -= 60;\n }\n string s = "";\n \n\t\t\n\t\t//Section 2\n while(tar >= 60)\n {\n s = "";\n if(mins != 0)\n s += to_string(mins);\n s += to_string(tar);\n ways.push_back(s);\n tar -= 60;\n mins++;\n }\n \n\t\t//Section 3\n s = "";\n if(mins >= 100)\n goto section4;\n if(mins != 0)\n s += to_string(mins);\n if(tar >= 10)\n s += to_string(tar);\n else if(tar>0 and tar < 10)\n {\n if(mins != 0)\n s += "0" + to_string(tar);\n else s += to_string(tar);\n }\n else if(tar == 0)\n s += "00";\n ways.push_back(s);\n \n\t\t\n\t\t//Section 4\n section4:\n int ans = INT_MAX; \n for(auto s : ways)\n {\n //cout<<s<<" ";\n int len = s.size(); \n int sub = 0; \n char cur = to_string(start)[0];\n for(int i =0; i<len; i++)\n {\n if(cur != s[i])\n {\n sub += move;\n cur = s[i];\n }\n sub += push;\n }\n //cout<<sub<<endl;\n ans = min(ans, sub);\n }\n \n return ans; \n \n }\n};\n```
| 17 | 3 |
['C']
| 3 |
minimum-cost-to-set-cooking-time
|
✅Java - Easy, Brute Force, Short and Understandable Code!!!
|
java-easy-brute-force-short-and-understa-kpzi
|
```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int tar) {\n \n int min=tar/60, sec=tar%60, minCost=(mo
|
pgthebigshot
|
NORMAL
|
2022-02-05T16:12:46.475540+00:00
|
2022-02-06T10:02:33.439242+00:00
| 1,365 | false |
```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int tar) {\n \n int min=tar/60, sec=tar%60, minCost=(moveCost+pushCost)*4;\n \n if(min>99) { min--; sec+=60; } // this is required to do because if tar>=6000 then min is 100 which is not possible as it atmost can be 99mins\n \n while(min>=0&&sec<=99) { // this while loop will work for atmost 2 iterations\n tar=min*100+sec;\n char arr[]=(""+tar).toCharArray();\n int sameMove=0;\n for(int i=0;i<arr.length-1;i++)\n if(arr[i]==arr[i+1])\n sameMove++;\n if(startAt==arr[0]-\'0\')\n minCost=Math.min(minCost,pushCost*arr.length+moveCost*(arr.length-1-sameMove));\n else\n minCost=Math.min(minCost,pushCost*arr.length+moveCost*(arr.length-sameMove));\n min--; sec+=60;\n }\n return minCost;\n }\n}\n````\nI really hope you like the solution!!!\nIf it was helpful then please **upvote** it:))
| 17 | 4 |
['Java']
| 5 |
minimum-cost-to-set-cooking-time
|
✅ C++ | All Combination | Brute Force | Easy | Readable
|
c-all-combination-brute-force-easy-reada-4k7f
|
\nclass Solution {\npublic:\n //finding answer of every possible minute that equal to targetSecond\n int solve(vector<int> &a,int sa, int mc, int pc){\n
|
Preacher001
|
NORMAL
|
2022-02-05T16:02:15.745398+00:00
|
2022-02-05T16:22:31.620139+00:00
| 1,409 | false |
```\nclass Solution {\npublic:\n //finding answer of every possible minute that equal to targetSecond\n int solve(vector<int> &a,int sa, int mc, int pc){\n int ans = 0;\n if(a[0] != sa){\n ans += mc;\n }\n ans += pc;\n for(int i=1;i<a.size();i++){\n if(a[i] != a[i-1]){\n ans += mc;\n }\n ans += pc;\n \n }\n return ans;\n }\n \n int minCostSetTime(int sa, int mc, int pc, int ts) {\n int cost = 1e8;\n //trying all permutation\n for(int i=0;i<=9;i++){\n for(int j=0;j<=9;j++){\n int min = i*10+j; // minutes\n for(int k=0;k<=9;k++){\n for(int l=0;l<=9;l++){\n\t\t\t\t\t int sec = k*10 + l;//seconds\n vector<int> ans;\n int m = min*60 + sec; \n if(m == ts){\n ans.emplace_back(i);\n ans.emplace_back(j);\n ans.emplace_back(k);\n ans.emplace_back(l);\n cost = min(cost,solve(ans,sa,mc,pc));\n while(ans[0] == 0){\n ans.erase(ans.begin());\n cost = min(cost,solve(ans,sa,mc,pc));\n }\n }\n }\n }\n }\n }\n return cost;\n }\n};\n```
| 13 | 5 |
['C']
| 11 |
minimum-cost-to-set-cooking-time
|
✅O(1) | Complete explanation with examples | 6 total cases | C++
|
o1-complete-explanation-with-examples-6-jtws0
|
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
|
arpitdhamija
|
NORMAL
|
2022-02-05T16:31:48.517396+00:00
|
2022-02-05T17:00:10.970961+00:00
| 685 | false |
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**
| 12 | 6 |
[]
| 1 |
minimum-cost-to-set-cooking-time
|
Simple Python Solution
|
simple-python-solution-by-ancoderr-zfjk
|
\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n def count_cost(minutes, seconds
|
ancoderr
|
NORMAL
|
2022-02-05T16:00:52.428847+00:00
|
2022-02-05T16:09:16.098604+00:00
| 1,014 | false |
```\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n def count_cost(minutes, seconds): # Calculates cost for certain configuration of minutes and seconds\n time = f\'{minutes // 10}{minutes % 10}{seconds // 10}{seconds % 10}\' # mm:ss\n time = time.lstrip(\'0\') # since 0\'s are prepended we remove the 0\'s to the left to minimize cost\n t = [int(i) for i in time]\n current = startAt\n cost = 0\n for i in t:\n if i != current:\n current = i\n cost += moveCost\n cost += pushCost\n return cost\n ans = float(\'inf\')\n for m in range(100): # Check which [mm:ss] configuration works out\n for s in range(100):\n if m * 60 + s == targetSeconds: \n ans = min(ans, count_cost(m, s))\n return ans\n```
| 11 | 1 |
['Python', 'Python3']
| 1 |
minimum-cost-to-set-cooking-time
|
Python 3 || 8 lines, strings || T/S: 97% / 99%
|
python-3-8-lines-strings-ts-97-99-by-spa-eeus
|
\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int,\n pushCost: int, targetSeconds: int) -> int:\n\n
|
Spaulding_
|
NORMAL
|
2023-03-30T16:09:03.187203+00:00
|
2024-06-21T23:01:12.001188+00:00
| 392 | false |
```\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int,\n pushCost: int, targetSeconds: int) -> int:\n\n def cost(m: int,s: int)-> int:\n\n if not(0 <= m <= 99 and 0 <= s <= 99): return inf\n\n display = str(startAt) + str(100*m+s)\n\n moves = sum(display[i] != display[i-1] for i in range(1,len(display)))\n pushes = len(display) - 1\n \n return moveCost*moves + pushCost*pushes\n \n\n mins, secs = divmod(targetSeconds,60)\n \n return min(cost(mins,secs),cost(mins-1,secs+60))\n```\n[https://leetcode.com/problems/minimum-cost-to-set-cooking-time/submissions/925094755/](https://leetcode.com/problems/minimum-cost-to-set-cooking-time/submissions/925094755/)\n\nI could be wrong, but I think that time complexity is *O*(1) and space complexity is *O*(1).\n
| 9 | 0 |
['Python3']
| 0 |
minimum-cost-to-set-cooking-time
|
JAVA Solution ||(Brute Force)|| All Edge Cases Explained !!!
|
java-solution-brute-force-all-edge-cases-vu78
|
Brute Force Solution\n0(1)Time And Space Complexity\n*\n\tclass Solution {\n\t\tpublic int minCostSetTime(int startAt, int moveCost, int pushCost, int ts) \n\t\
|
amish_06
|
NORMAL
|
2022-02-05T16:35:38.584371+00:00
|
2022-02-05T16:38:00.245991+00:00
| 688 | false |
Brute Force Solution\n0(1)Time And Space Complexity\n****\n\tclass Solution {\n\t\tpublic int minCostSetTime(int startAt, int moveCost, int pushCost, int ts) \n\t\t{\n\t\t\tchar st=(char)(startAt+\'0\');\n\t\t\tif(ts<60)\n\t\t\t\treturn cost(st,ts+"",moveCost,pushCost);\n\t\t\telse\n\t\t\t{\n\t\t\t\tint min=ts/60;\n\t\t\t\tint sec=ts%60;\n\t\t\t\tString s1="",s2="",or="";\n\t\t\t\t\n\t\t\t\tif(ts<=99)\n\t\t\t\t\tor=ts+""; // Orignal time in second;\n\n\t\t\t\tif(min==100) //If time >=100 min than convert in 00:99 second format\n\t\t\t\t{\n\t\t\t\t\tmin--;\n\t\t\t\t\tsec+=60;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif(sec<10)\n\t\t\t\t{\n\t\t\t\t\ts1=min+"0"+sec; //Time in 60 second format\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ts1=min+""+sec;\n\t\t\t\t}\n\n\t\t\t\tif(sec<=39)\n\t\t\t\t{\n\t\t\t\t\tmin--;\n\t\t\t\t\tsec+=60;\n\t\t\t\t\ts2=min+""+sec; //If time can be represened in 00:99 seconds Format\n\t\t\t\t}\n\n\t\t\tint ans=Math.min(cost(st,or,moveCost,pushCost),Math.min(cost(st,s1,moveCost,pushCost),cost(st,s2,moveCost,pushCost)));\n\t\t\t\treturn ans;\n\t\t\t} \n\t\t}\n\n\t\tpublic int cost(char st,String s,int moveCost, int pushCost)\n\t\t{\n\t\t\tif(s.length()==0)\n\t\t\t\treturn Integer.MAX_VALUE;\n\n\t\t\tint push=s.length();\n\t\t\tint move=1;\n\t\t\tfor(int i=0;i<push-1;i++)\n\t\t\t{\n\t\t\t\tif(s.charAt(i)!=s.charAt(i+1))\n\t\t\t\t{\n\t\t\t\t\tmove++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(st==s.charAt(0))\n\t\t\t\tmove--;\n\n\t\t\treturn (move*moveCost)+(push*pushCost); \n\t\t}\n\t}\n\t****
| 7 | 0 |
['Java']
| 4 |
minimum-cost-to-set-cooking-time
|
[Python] Be careful with some annoying edge cases.
|
python-be-careful-with-some-annoying-edg-zjqm
|
Convert targetseconds to mm:ss first.\nWe need to consider at most two cases:\n- If mm < 100, we should consider the cost of mm:ss.\n- If the current mm > 1 and
|
Bakerston
|
NORMAL
|
2022-02-05T16:20:32.658624+00:00
|
2022-02-05T17:02:23.930359+00:00
| 662 | false |
Convert targetseconds to **mm:ss** first.\nWe need to consider at most two cases:\n- If **mm < 100**, we should consider the cost of **mm:ss**.\n- If the current **mm > 1** and **ss < 40**, meaning we should consider the cost of **mm-1:ss+60**.\n\nWe should also be careful of the string of seconds:\n- If **mm > 0**, then ss **must** have a length of 2. For example, **7** should be written as **07**.\n- If **mm = 0**, then ss can have length of 1.\n\nThen we just iterate over the string of **mmss** and calculate the total costs.\n\n**Python**\n```\ndef minCostSetTime(self, start: int, mC: int, pC: int, TS: int) -> int:\n def helper(mins, secs):\n pre, cost, ss = str(start), 0, str(secs)\n if mins != 0:\n if len(ss) < 2:\n ss = "0" + ss\n ss = str(mins) + ss \n for ch in ss:\n if ch != pre:\n cost += mC\n cost += pC\n pre = ch\n return cost\n\n mins, secs = divmod(TS, 60)\n ans = math.inf\n \n if mins < 100:\n ans = min(ans, helper(mins, secs))\n if secs < 40:\n ans = min(ans, helper(mins - 1, secs + 60))\n return ans\n```
| 7 | 0 |
[]
| 1 |
minimum-cost-to-set-cooking-time
|
Easy to understand || Well Explained || Dfs || Clean Code || Complexity-Analysis
|
easy-to-understand-well-explained-dfs-cl-qu8t
|
Just do as question said :\nStart from 0 : \n1. And if you\'re currently at the button which has previously pressed or it\'s the begginging then you\'ll spend j
|
faltu_admi
|
NORMAL
|
2022-02-05T16:15:11.183124+00:00
|
2022-02-05T16:19:02.712530+00:00
| 469 | false |
Just do as question said :\nStart from 0 : \n1. And if you\'re currently at the button which has previously pressed or it\'s the begginging then you\'ll spend just only pressCost.\n2. Else, you need to move first you\'re desire button(moveCost) and than you need to press it(pressCost). I.e. moveCost+pressCost.\n3. Now, In recursion if you moved from a button to a new button than you must update the lastPressedButton also so that you wouldn\'t need to invest move cost again.\n4. Find the min from all the possibilites.\n\nNow you need handle those cases who are not incrementing in terms of number \ne.g. 000 you can add one more 0 i.e. 0000 but it is still 0. \nSo to handle this you can use how many times you have pushed the number and atmost you can push 4 times because \nmicrowave supports cooking times for:\n* at least 1 second.\n* at most 99 minutes and 99 seconds.\n```\nclass Solution {\npublic:\n bool isEqual(int source, int target) {\n \n // Convert source to sec\n int min = source/100;\n int sec = source%100;\n return (min*60+sec) == target;\n }\n\n\tlong solve(int curr, int moveCost, int pressCost, int target, int lastPressedButton, int times=0) {\n \n // Handle Overflow conditions\n if(curr>9999 || times>4) {\n return INT_MAX;\n }\n\n // If we get target than no need to do anything\n if(isEqual(curr,target)) {\n return 0;\n }\n\n long res=INT_MAX;\n \n // Now start from 0-th button and keep pressing one by one\n for(int i=0;i<10;i++) {\n long ans = (pressCost+(i!=lastPressedButton)*moveCost) + solve(curr*10+i, moveCost, pressCost, target, i, times+1);\n res = min(res,ans);\n }\n return res;\n }\n\n\tint minCostSetTime(int start, int moveCost, int pressCost, int target) {\n\t\treturn solve(0, moveCost, pressCost, target, start);\n\t}\n};\n```\n\n# Time Complexity : \nSo we\'re are pressing for each button for every move.\nSo atmost there can be 4 move i.e. <9999 or log(1e5)base10\nAnd for one move cost will be 10.\nSo for 4 move cost will be 10^4.\nSo more formally in the worst case (10^ log(1e5)base10).\n\n# Space Complexity :\nSo we are not using any extra space except recursion stack.\nSo at any point time what will be the maximum number of function which would have been pushed?\n1. Since, Each step we are incrementing by one digit or pushing one button.\n2. So at worst case I have been pushed all the button(atmost 4 button).\n\nSo overall stack size would be atmost of 4 i.e. constant.\n\n**Feel free to correct me if I was wrong any where. And please upvote if you find it usefull.\nHappy Coding!**
| 5 | 0 |
['C']
| 2 |
minimum-cost-to-set-cooking-time
|
Java Solution
|
java-solution-by-java_programmer_ketan-h709
|
\nclass Solution {\n //Make a list of digits to be pressed. If (minutes>=100 then list=null) || (seconds>=100 then list=null)\n public int minCostSetTime(i
|
Java_Programmer_Ketan
|
NORMAL
|
2022-02-05T16:01:19.955843+00:00
|
2022-02-05T16:01:19.955884+00:00
| 250 | false |
```\nclass Solution {\n //Make a list of digits to be pressed. If (minutes>=100 then list=null) || (seconds>=100 then list=null)\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int minutes = targetSeconds/60;\n int seconds = targetSeconds%60;\n List<Integer> time1 = calculateTimeArray(minutes,seconds);\n List<Integer> time2 = calculateTimeArray(minutes-1,seconds+60);\n return Math.min(calculateCost(startAt,moveCost,pushCost,time1),calculateCost(startAt,moveCost,pushCost,time2));\n }\n public int calculateCost(int startAt,int moveCost, int pushCost, List<Integer> time){\n if(time==null) return Integer.MAX_VALUE;\n int cost=0,index=0;\n while(time.get(index)==0) index++; //Always skip starting zeros even if you are at zero as they cost pushCost\n int currentPosition = startAt;\n while(index<=3){\n if(currentPosition==time.get(index)) cost+=pushCost;\n else{\n currentPosition=time.get(index);\n cost+=pushCost+moveCost;\n }\n index++;\n }\n return cost;\n }\n public List<Integer> calculateTimeArray(int minutes, int seconds){\n if(minutes>=100 || seconds>=100) return null;\n List<Integer> time = new ArrayList<>();\n time.add(minutes/10);\n time.add(minutes%10);\n time.add(seconds/10);\n time.add(seconds%10);\n return time;\n }\n}\n```
| 5 | 2 |
[]
| 1 |
minimum-cost-to-set-cooking-time
|
C++ | Accepted ✅
|
c-accepted-by-ash_gt7-as70
|
\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int target) {\n vector<string> v;\n if(target < 100){
|
ash_gt7
|
NORMAL
|
2022-02-05T16:01:06.835994+00:00
|
2022-02-05T16:19:49.964827+00:00
| 908 | false |
```\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int target) {\n vector<string> v;\n if(target < 100){\n v.push_back(to_string(target));\n int m = target / 60, sc = target % 60;\n string ssc = "";\n if(sc < 10){\n ssc += \'0\';\n ssc += to_string(sc);\n }else{\n ssc = to_string(sc);\n }\n v.push_back(to_string(m)+ssc);\n }else{\n int m = target / 60, sc = target % 60;\n if(m < 100){\n string ssc = "";\n if(sc < 10){\n ssc += \'0\';\n ssc += to_string(sc);\n }else{\n ssc = to_string(sc);\n }\n v.push_back(to_string(m)+ssc);\n }\n if(60 + sc < 100){\n v.push_back(to_string(m-1)+ to_string(sc+60));\n }\n }\n int res = INT_MAX;\n for(int i=0; i<v.size(); i++){\n int cost = 0, cur = startAt;\n for(int j=0; j<v[i].size(); j++){\n if(v[i][j]-\'0\' != cur){\n cost += moveCost;\n cur = v[i][j]-\'0\';\n }\n cost += pushCost;\n }\n res = min(res, cost);\n }\n return res;\n }\n};\n```
| 5 | 0 |
['C', 'C++']
| 1 |
minimum-cost-to-set-cooking-time
|
[C++, Python, Java] [Direct] O(1) space & time (0 ms, faster than 100%) , Modular concise short
|
c-python-java-direct-o1-space-time-0-ms-jo7x2
|
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 ||
|
ashucrma
|
NORMAL
|
2022-02-05T16:41:23.523492+00:00
|
2022-02-08T16:42:32.510112+00:00
| 170 | false |
**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.**
| 4 | 0 |
['C']
| 2 |
minimum-cost-to-set-cooking-time
|
C++ | O(1) time and space solution
|
c-o1-time-and-space-solution-by-mandysin-t4fh
|
Some points to remember\n We can have atmost 99 minutes and atleast 00 minutes in minutes dial\n We can have atmost 99 seconds and atleast 00 seconds in seconds
|
mandysingh150
|
NORMAL
|
2022-02-05T16:10:49.036790+00:00
|
2022-02-05T16:57:34.914509+00:00
| 293 | false |
**Some points to remember**\n* We can have atmost 99 minutes and atleast 00 minutes in minutes dial\n* We can have atmost 99 seconds and atleast 00 seconds in seconds dial \n\nLet **mins = targetSeconds/60** and **secs = targetSeconds%60**, then we just need to check for minutes={mins-1, min}. This can easily be understood with the following example,\n\n**Example** : *startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600*\nHere, mins = 6000/60 = 100 and secs = 6000%60 = 0\nmins = 100, which cannot be entered into the minutes dial, so we decrease value of *mins* and add 60 to *secs*.\nThen, min=99, secs=60, and after calculation, we get cost=10. Again, decreasing the value of *mins* and increasing *secs*, \nwe get min=98, secs=120. But, *secs=120* is not valid.\nThis way the previous values of minutes and seconds will not be valid inputs, so we stop here.\n\nI hope the approach is clear, if you have any doubts then feel free to ask in the comments.\n**Plz upvote if you liked it!!**\n\n```\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int mins = targetSeconds/60;\n int secs = targetSeconds%60;\n \n\t\twhile(mins>99) { // to ensure that \'minutes\' remain smaller than 100 and accordingly adding \'seconds\'\n mins--;\n secs += 60;\n }\n \n int ans = INT_MAX;\n while(mins>=0 and secs<100) { // as \'minutes\' are decreasing and \'seconds\' are increasing\n string s = (mins == 0 ? "" : to_string(mins));\n if(mins > 0) {\n if(secs == 0)\n s += "00";\n else if(secs < 10)\n s += "0" + to_string(secs);\n else\n s += to_string(secs);\n }\n else // when \'minutes\'=0, then we just need to enter the digits of \'seconds\' in microwave dial\n s += (secs==0 ? "" : to_string(secs));\n\n int t=0;\n\t\t\tchar previousCharacter = startAt+\'0\';\n for(auto i: s) {\n if(previousCharacter == i) // no need to move from current character, just press the key\n t += pushCost;\n else\n t += moveCost + pushCost;\n\t\t\t\tpreviousCharacter = i;\n }\n ans = min(ans, t);\n mins--;\n secs += 60;\n }\n return ans;\n }\n};\n```
| 4 | 0 |
[]
| 1 |
minimum-cost-to-set-cooking-time
|
C++ Simulation O(1) Time
|
c-simulation-o1-time-by-lzl124631x-au2v
|
See my latest update in repo LeetCode\n\n## Solution 1.\n\nOnly two cases, minute = 0, second = target and minute = m, second = target - m * 60. We need to make
|
lzl124631x
|
NORMAL
|
2022-02-05T16:05:39.452807+00:00
|
2022-02-05T17:17:51.026847+00:00
| 652 | false |
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1.\n\nOnly two cases, `minute = 0, second = target` and `minute = m, second = target - m * 60`. We need to make sure both `minute` and `second` are in range `[0, 100)`.\n\nFor a given pair of `minute, second`, we calculate the cost and use the minimum cost as the answer.\n\n```cpp\n// OJ: https://leetcode.com/problems/minimum-cost-to-set-cooking-time/\n// Author: github.com/lzl124631x\n// Time: O(1) since `target` is at most 6039\n// Space: O(1)\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int target) {\n int ans = INT_MAX, minute = 0;\n auto cost = [&](int m, int s) {\n auto d = to_string(m * 100 + s); // append seconds to minutes and get the digits\n int x = startAt, ans = 0;\n for (int i = 0; i < d.size(); ++i) { // simulate the time setting process\n if (x != d[i] - \'0\') ans += moveCost, x = d[i] - \'0\';\n ans += pushCost;\n }\n return ans;\n };\n while (target >= 0) {\n if (target < 100 && minute < 100) {\n ans = min(ans, cost(minute, target));\n }\n target -= 60;\n minute++;\n }\n return ans;\n }\n};\n```
| 4 | 1 |
[]
| 4 |
minimum-cost-to-set-cooking-time
|
C++ | Simple Solution | Brute Force
|
c-simple-solution-brute-force-by-dpw4112-wuhs
|
We know that maximum seconds we can make through last two digits is 99 so lets try all comibantions from making 0 to 99 and remaining seconds by using minute di
|
dpw4112001
|
NORMAL
|
2022-02-05T16:04:24.350015+00:00
|
2022-02-05T16:05:11.096677+00:00
| 132 | false |
We know that maximum seconds we can make through last two digits is 99 so lets try all comibantions from making 0 to 99 and remaining seconds by using minute digits.\n\nIf you need further clarification please comment it down.\n\n\n```\nclass Solution {\npublic:\n int getCost(string s,int start,int move,int push)\n {\n int c = 0;\n for(int i=0;i<s.length();i++)\n {\n if(i == 0)\n {\n\t\t\t // if not the start digit\n if((s[i] - \'0\') != start)\n {\n c += move;\n }\n }\n else\n {\n\t\t\t\t// if digit is not same as prev then we need to move\n if(s[i] != s[i-1])\n {\n c += move;\n }\n }\n c += push;\n }\n return c;\n }\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n int ans = 100000000;\n for(int i=0;i<100;i++)\n {\n if(targetSeconds < i)\n break;\n\n int x = targetSeconds - i;\n\n if(x % 60 == 0)\n {\n\n string s;\n if(i < 10)\n {\n s += "0";\n }\n s += to_string(i);\n\n\n int m = x / 60;\n\t\t\t\t// minute digits cannot exceed 2\n if(m >= 100)\n continue;\n if(m > 0){\n s = to_string(m) + s;\n ans = min(ans,getCost(s,startAt, moveCost, pushCost));\n }\n else\n {\n if(i < 10)\n {\n ans = min(ans,getCost(to_string(i),startAt, moveCost, pushCost));\n }\n else\n {\n ans = min(ans,getCost(s,startAt, moveCost, pushCost));\n }\n }\n \n \n }\n }\n\n return ans;\n\n\n }\n};
| 4 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
Java Solution || With example and comments
|
java-solution-with-example-and-comments-2mfgp
|
We have 2 option. \n1. minutes:seconds\n2. minutes-1:60+seconds\nFor Exmaple\n10:00 can be represented as \n10:00 or 9:60\nWe then convert to microve time by mu
|
priyadarshinee11
|
NORMAL
|
2022-09-17T11:39:28.164521+00:00
|
2022-09-17T11:40:28.777503+00:00
| 380 | false |
We have 2 option. \n1. minutes:seconds\n2. minutes-1:60+seconds\nFor Exmaple\n10:00 can be represented as \n10:00 or 9:60\nWe then convert to microve time by multiplying 100 to minutes\n```\n10:00-> 10*100+0 = 1000\n9:60--> 9*100+60 = 960\n```\n\n\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int minutes = targetSeconds/60;//Convert targetseconds to minutes and seconds\n int seconds = targetSeconds%60;\n //We have 2 option\n return Math.min(findMinCost(startAt,moveCost,pushCost,minutes,seconds),\n findMinCost(startAt,moveCost,pushCost,minutes-1,60+seconds));\n }\n public int findMinCost(int startAt, int moveCost, int pushCost, int minutes, int seconds){\n if(seconds< 0 || minutes<0 || minutes>99 || seconds>99){\n return Integer.MAX_VALUE;\n }\n int minCost = 0;\n int microwaveTime = (minutes*100)+seconds;//So avoid leading zeros 6:10 --->610\n char digits[] = String.valueOf(microwaveTime).toCharArray();\n for(char digitChar : digits){\n int digit = digitChar -\'0\';\n minCost = minCost+ pushCost + (startAt==digit?0:moveCost);//if the current digit is different then previous one we need to add a move cost since we move to a different position\n startAt = digit;\n }\n return minCost;\n }\n}\n```
| 3 | 0 |
['Java']
| 1 |
minimum-cost-to-set-cooking-time
|
✅ Easy to understand JS Solution with explanation
|
easy-to-understand-js-solution-with-expl-n39b
|
Intuition:\n\nThe idea is first we need to think that in how many ways we can get targetSeconds so that we can maintain the constraint of mm:ss i.e 99:99. And f
|
dollysingh
|
NORMAL
|
2022-02-06T09:21:29.130327+00:00
|
2022-02-06T10:30:17.951657+00:00
| 154 | false |
Intuition:\n\nThe idea is first we need to think that in how many ways we can get targetSeconds so that we can maintain the constraint of mm:ss i.e 99:99. And from those ways we need one possible combination such that we get less tired(moving + pushing those buttons).\n\n**Example 1:** if we have targetSeconds of 400 seconds:\n\n```\n1st way 400 sec ( _ _ : _ _) i.e ( 0 0: 400 )\n -60\n -----\n2nd way 340 sec ( _ _ : _ _) i.e ( 0 1: 340 ) <--- cannot be arranged here\n -60\n\t\t\t -----\n3rd way 280 sec ( _ _ : _ _) i.e ( 0 2: 280 )<-- neither here\n -60\n -----\n4th way 220 sec ( _ _: _ _) i.e ( 0 3: 220 ) <-- neither here\n -60\n -----\n5th way. 160 sec ( _ _: _ _) i.e ( 0 4: 160 ) <-- neither here\n -60\n -----\n6th 100 sec ( _ _: _ _) i.e ( 0 5: 100 ) <-- neither here\n -60\n -----\n 40 sec ( 0 6: 4 0) <-- here we can achieve our constraint ( <=99: <=99 )\n```\n\nHence we can see that 1 to 5 can not be part of minimum cost as they do not follow our constraint of ( <=99: <=99 ). For these we simply continue and for 6 we generate our cost, which will be the minimum.\n\n\n**Example 2:** targetSeconds of 99 seconds:\n```\n1st way 99 sec ( _ _ : _ _) i.e ( 0 0: 99 ) <-- can be arranged in mm:ss\n -60\n\t\t -----\n2n way 39 sec ( _ 1 : _ _) i.e ( 0 1: 39 ) <-- can be arranged in mm:ss\n```\n\nHence we see 1 and 2 both fulfill our criteria of ( <=99: <=99 ) and both of them will contribute to our minimum cost logic\n\n**CODE**\n\n```\n/**\n * @param {number} startAt\n * @param {number} moveCost\n * @param {number} pushCost\n * @param {number} targetSeconds\n * @return {number}\n */\nvar minCostSetTime = function(startAt, moveCost, pushCost, targetSeconds) {\n let cost = Infinity;\n \n let maxMinutes = Math.floor(targetSeconds / 60);\n \n for(let min = 0; min <= maxMinutes; min++) {\n let secs = targetSeconds - min * 60;\n \n if (secs > 99 || min > 99) continue;\n \n let buttons = String(100 * min + secs);\n let prev = Number(buttons[0]);\n \n let sum = 0;\n \n //start index will vary according to startAt pointer\n let start = 0;\n \n //If startAt is equal to first button, we need to add pushCost fatigue\n if(prev === startAt) {\n sum += pushCost;\n start = 1;\n } else {\n //Else we need to add moveCost fatigue\n sum += moveCost;\n }\n \n for(let i = start; i < buttons.length; i++) {\n let button = Number(buttons[i]);\n \n if(button !== prev) {\n sum += moveCost + pushCost;\n } else {\n sum += pushCost;\n }\n \n prev = button;\n }\n \n cost = Math.min(cost, sum)\n }\n \n return cost;\n};\n```\n\nNote: This is JS version of https://leetcode.com/problems/minimum-cost-to-set-cooking-time/discuss/1746988/Python3-Java-C%2B%2B-Combinations-of-Minutes-and-Seconds-O(1) Solution\n\nHope it helps!
| 3 | 0 |
['JavaScript']
| 0 |
minimum-cost-to-set-cooking-time
|
Short (Explained solution): Loop Through each possible combination O(1) time/space
|
short-explained-solution-loop-through-ea-7nv7
|
Since there were many possible ways to write the number and that would have been a tedious task to find all so simply check all the possible combinations you ca
|
Priyanshu_Chaudhary_
|
NORMAL
|
2022-02-05T16:06:39.294443+00:00
|
2022-02-05T16:32:20.316264+00:00
| 170 | false |
Since there were many possible ways to write the number and that would have been a tedious task to find all so simply check all the possible combinations you can put on microwave \n\n* The crux was to check all the possible ways from 0001 to 9999 \n* If you find the number of seconds equal to target seconds.\n* Count how many energy you need to make this possible combination.\n\n```\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) \n {\n int ans=INT_MAX;\n for(int i=1;i<=9999;i++)\n {\n int seconds=((i/100)*60)+(i%100);// count number of seconds\n \n if(seconds!=targetSeconds)// check whether no. of seconds are equal to target seconds or not\n continue;\n \n int start=startAt,count=0,j=0;// start time \n string s=to_string(i); // i converted the combination to string so that i can loop through it easily \n while(j<s.size()) // part where i calculated the number of points\n {\n if(s[j]!=start+\'0\') // if start position is not thedesired position\n count+=moveCost+pushCost;\n else\n count+=pushCost; \n start=s[j++]-\'0\'; // change the start position\n }\n ans=min(ans,count); \n }\n \n return ans;\n }\n};\n```
| 3 | 1 |
['Greedy', 'Simulation']
| 2 |
minimum-cost-to-set-cooking-time
|
Python simple solution with comments and beats 80%
|
python-simple-solution-with-comments-and-u4lz
|
```\n\'\'\'\nThis is relatively simple\n\n1. Convert target to minutes and seconds and get the string representation\n2. Since seconds can exceed 60, it may be
|
cutkey
|
NORMAL
|
2022-06-10T00:32:10.698676+00:00
|
2022-06-10T00:36:19.225984+00:00
| 201 | false |
```\n\'\'\'\nThis is relatively simple\n\n1. Convert target to minutes and seconds and get the string representation\n2. Since seconds can exceed 60, it may be possible to get a shorter string representation by increasing seconds by 60 and decreasing the \n\tminutes by 1. \n3. For each string representation calculate cost and return the minimum of the two\n4. Edge case: If the number of minutes is greater than 100 the cost can be set to inf\n\n\'\'\'\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n mins = targetSeconds//60\n secs = targetSeconds%60\n \n def getCost(s, startAt, moveCost, pushCost):\n if len(s) > 4:\n return float("inf")\n cost = len(s)*pushCost\n for ch in s:\n if ch != startAt:\n cost += moveCost\n startAt = ch\n return cost\n \n def getStringRepresentation(mins, secs):\n s = ""\n if mins > 0:\n s += str(mins)\n if secs < 10 and mins > 0:\n s += "0"+str(secs) # Note this step. Obvious but important\n else:\n s += str(secs)\n \n return s\n \n s1 = getStringRepresentation(mins, secs)\n c1 = getCost(s1, str(startAt), moveCost, pushCost)\n \n if secs + 60 < 100 and mins > 0:\n secs += 60\n mins -= 1\n \n s2 = getStringRepresentation(mins, secs)\n if s1 != s2:\n c2 = getCost(s2, str(startAt), moveCost, pushCost)\n return min(c1, c2)\n return c1
| 2 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
All cases Explained in depth. The best explanation you will come across.
|
all-cases-explained-in-depth-the-best-ex-9wse
|
Let us break it down into steps:\n1. step 1 convert the given input targetSeconds into minutes and seconds:\n\n2. step 2 generate all the permuatations for the
|
jatin_sachdeva
|
NORMAL
|
2022-02-17T05:17:52.719778+00:00
|
2022-02-17T05:17:52.719824+00:00
| 163 | false |
Let us break it down into steps:\n1. step 1 convert the given input *targetSeconds* into minutes and seconds:\n\n2. step 2 generate all the permuatations for the input:\n* \tThere are some things we need to take care of\n* \tmin<=99 && sec<=99 as mentioned in the ques.\n* \tleading zeroes in minutes are of no use as they will just increase our cost as we would need an extra push and move (ex: 9min 60 sec ---> "9:60" and "09:60", the first is definetely better so no need for checking second one).\n* \tfor an input at most 2 unique permuations will be produced, as we can reduce 1min and add 60 to seconds. cant do it twice as sec<=99.\n3. Cases, when we produce string equivalent of a valid input\n* if(mins==0) we need no leading zeroes so we just put seconds into string\n* if(mins>0){\n\t\tif(secs==0) we need an extra zero to be added at last(9min 0 sec ---> "9:00")\n\t\tif(secs<10) we need extra zero before the second value(9min 6sec--->"9:06")\n\t\t}\n\n\n4. step 4. for each produced permuatation computing the cost.\n\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int target) {\n // step 1 convert targetSeconds to minutes\n int min = target/60;\n int sec = target%60;\n \n // step 2. go through the permutations\n ArrayList<String> list = new ArrayList<String>();\n // a input can have atmost 2 permutations\n for(int i=0;i<2;i++)\n {\n // a input is valid only if min<=99 && sec<=99\n if(min<100&&sec<100){\n String mins = String.valueOf(min);\n String secs = String.valueOf(sec);\n if(min==0) list.add(new String(secs)); // we need no leading zeroes\n else\n {\n if(sec==0) // need a extra zero at end\n list.add(new String(mins+secs+"0"));\n else if(sec<10) // need a zero berfore second\n list.add(new String(mins+"0"+secs));\n else // everything is fine\n list.add(new String(mins+secs));\n }\n }\n // second permuation is taking a minute out and adding it to seconds\n min--;\n sec+=60;\n }\n // System.out.println(list);\n int res = Integer.MAX_VALUE;\n // computing result\n for(String str: list)\n {\n int cost = 0;\n char prev = \'$\';\n for(int i=0;i<str.length();i++)\n {\n char ch = str.charAt(i);\n int chi = ch-\'0\';\n // if first integer is same as startAt only pushCost is added\n if(i==0&&chi==startAt)\n {\n prev = ch;\n cost+=pushCost;\n continue;\n }\n // if curr is same as prev only pushcost is added\n if(prev==ch)\n {\n prev = ch;\n cost+=pushCost;\n }\n // if prev is not same moveCost and pushCost both are added\n else\n {\n prev = ch;\n cost+=pushCost+moveCost;\n }\n }\n res = Math.min(res,cost);\n // System.out.println(cost);\n }\n return res;\n }\n}\n```\n
| 2 | 0 |
['Java']
| 1 |
minimum-cost-to-set-cooking-time
|
Java. Only two options. Clean code. 0ms
|
java-only-two-options-clean-code-0ms-by-crq1a
|
What a weird question.\nOnly two options:\n\n\t1. (minute, second)\n\t2. (minute - 1, second + 60)\n\nRemember to check boundary. Also, only press 0 when there
|
Student2091
|
NORMAL
|
2022-02-17T04:54:55.265089+00:00
|
2022-02-17T04:54:55.265121+00:00
| 190 | false |
What a weird question.\nOnly two options:\n\n\t1. (minute, second)\n\t2. (minute - 1, second + 60)\n\nRemember to check boundary. Also, only press 0 when there is **any** previous number that is not 0.\n\t\n```Java\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int m = targetSeconds / 60;\n int s = targetSeconds % 60;\n int one = find(startAt, moveCost, pushCost, m, s);\n int two = find(startAt, moveCost, pushCost, m - 1, s + 60);\n if (s >= 40 || m == 0) return one;\n if (m > 99) return two;\n return Math.min(one, two);\n }\n\n private int find(int i, int c, int p, int m, int s){\n int ans = 0;\n int[] data = new int[]{0, i}; //prev key sum, prev key\n ans += each(c, p, m / 10, data);\n ans += each(c, p, m % 10, data);\n ans += each(c, p, s / 10, data);\n ans += each(c, p, s % 10, data);\n return ans;\n }\n\n private int each(int c, int p, int key, int[] data){\n data[0] += key;\n if (data[0] == 0) return 0;\n int ans = (data[1] == key? p : p + c);\n data[1] = key;\n return ans;\n }\n}\t\n```
| 2 | 0 |
['Java']
| 1 |
minimum-cost-to-set-cooking-time
|
C++ || Simple Approach || Brute Force
|
c-simple-approach-brute-force-by-sanheen-527x
|
IDEA: Generating all possible ways by which we can get the target seconds, as question suggests there is only 4 digit number so its not too difficult to try all
|
sanheen-sethi
|
NORMAL
|
2022-02-05T17:42:58.120675+00:00
|
2022-02-05T17:42:58.120704+00:00
| 51 | false |
IDEA: Generating all possible ways by which we can get the target seconds, as question suggests there is only 4 digit number so its not too difficult to try all possible ways.\n\nAfter generating all possible ways, just splitting them into digits and calculate the cost.\n\nCode (Easy Understanding):\n- set is because 0543 and 543 should be considered once.\n\n```\nclass Solution {\n #define debug(x) cout<<#x<<":"<<x<<endl;\n set<int> generateNumbers(int n){\n set<int> ss;\n for(int i=0;i<=9;i++){\n for(int j=0;j<=9;j++){\n for(int k = 0;k<=9;k++){\n for(int l=0;l<=9;l++){\n int minutes = i*10 + j;\n int seconds = k*10 + l;\n if((minutes*60 + seconds) == n){\n ss.insert(minutes*100 + seconds);\n }\n }\n }\n }\n }\n return ss;\n }\n \n vector<vector<int>> convert(set<int>& ss){\n vector<vector<int>> allPossible;\n for(auto val : ss){\n vector<int> vec;\n while(val){\n vec.push_back(val%10);\n val = val/10;\n }\n reverse(vec.begin(),vec.end());\n allPossible.push_back(vec);\n }\n return allPossible;\n }\n \npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n auto allPossible = generateNumbers(targetSeconds);\n auto possible = convert(allPossible);\n vector<int> costs;\n for(auto& vec:possible){\n int cost = 0;\n int st = startAt;\n for(auto&val:vec){\n if(st == val){\n cost += pushCost;\n }else{\n cost += moveCost;\n cost += pushCost;\n st = val;\n }\n }\n costs.emplace_back(cost);\n }\n return *min_element(costs.begin(),costs.end());\n }\n};\n```
| 2 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
C++ || 100% fast || Looks Lengthy but easy to Understand ||
|
c-100-fast-looks-lengthy-but-easy-to-und-c9ct
|
\tclass Solution {\n\tpublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int min1=targetSeconds/60;\n
|
kundankumar4348
|
NORMAL
|
2022-02-05T16:52:06.085308+00:00
|
2022-02-05T16:52:06.085435+00:00
| 54 | false |
\tclass Solution {\n\tpublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int min1=targetSeconds/60;\n int sec1=targetSeconds%60;\n int min2=min1-1;\n int sec2=targetSeconds-(min2*60);\n //cout<<min1<<sec1<<" "<<min2<<sec2;\n string str1="0000";\n int i=1;\n if(sec1<=99 && min1<=99){\n while(min1){\n int val=min1%10;\n str1[i--]=val+\'0\';\n min1/=10;\n }\n i=3;\n while(sec1){\n int val=sec1%10;\n str1[i--]=val+\'0\';\n sec1/=10;\n }\n }\n \n \n string str2="0000";\n if(sec2 <= 99 && min2<=99){\n int i=1;\n while(min2){\n int val=min2%10;\n str2[i--]=val+\'0\';\n min2/=10;\n }\n i=3;\n while(sec2){\n int val=sec2%10;\n str2[i--]=val+\'0\';\n sec2/=10;\n }\n }\n //cout<<str1<<" "<<str2<<endl;\n \n \n //cost for str1\n int flag=0;\n i=0;\n char sa=startAt+\'0\';\n long cost1=0;\n while(i<str1.size()){\n if(flag==0 && str1[i]==\'0\'){\n i++;continue;\n }else{\n flag=1;\n if(sa!=str1[i]){\n cost1+=(moveCost+pushCost);\n sa=str1[i];\n }else\n cost1+=pushCost;\n \n i++;\n }\n \n }\n \n //cost for str2\n flag=0;\n i=0;\n sa=startAt+\'0\';\n long cost2=0;\n while(i<str2.size()){\n if(flag==0 && str2[i]==\'0\'){\n i++;continue;\n }else{\n flag=1;\n if(sa!=str2[i]){\n cost2+=(moveCost+pushCost);\n sa=str2[i];\n }else\n cost2+=pushCost;\n \n i++;\n }\n \n }\n //cout<<cost1<<" "<<cost2<<endl;\n if(cost1==0)return cost2;\n if(cost2==0)return cost1;\n return min(cost1,cost2);\n }\n\t};
| 2 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
Python3 | Beats 100% time
|
python3-beats-100-time-by-himanshushah80-gc5f
|
\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n poss = [(targetSeconds // 60, t
|
himanshushah808
|
NORMAL
|
2022-02-05T16:27:54.065259+00:00
|
2022-02-05T16:27:54.065290+00:00
| 258 | false |
```\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n poss = [(targetSeconds // 60, targetSeconds % 60)] # store possibilities as (minutes, seconds)\n \n if poss[0][0] > 99: # for when targetSeconds >= 6000\n poss = [(99, poss[0][1]+60)]\n \n if poss[0][0] >= 1 and (poss[0][1]+60) <= 99:\n\t\t\t# adding a second possibility e.g. (01, 16) -> (0, 76)\n poss.append((poss[0][0]-1, poss[0][1]+60))\n \n costs = list()\n \n for i in poss:\n curr_start = startAt\n curr_cost = 0\n \n minutes = str(i[0])\n if i[0] != 0: # 0s are prepended, so no need to push 0s\n for j in minutes:\n if int(j) != curr_start:\n curr_cost += moveCost\n curr_start = int(j)\n curr_cost += pushCost\n \n seconds = str(i[1])\n if len(seconds) == 1 and i[0] != 0: # seconds is a single digit, prepend a "0" to it\n seconds = "0" + seconds\n \n for j in seconds:\n if int(j) != curr_start:\n curr_cost += moveCost\n curr_start = int(j)\n curr_cost += pushCost\n costs.append(curr_cost)\n \n return min(costs)\n```
| 2 | 0 |
['Python', 'Python3']
| 0 |
minimum-cost-to-set-cooking-time
|
Tips | for those who got lot of WA || Python
|
tips-for-those-who-got-lot-of-wa-python-sakoj
|
I know this problem is very poor and deceptive. But a lot of us have wasted a lot of time in the contest on this. So solution for those legends. Keep grinding\n
|
AdiDu
|
NORMAL
|
2022-02-05T16:19:29.865399+00:00
|
2022-02-05T16:19:29.865424+00:00
| 83 | false |
I know this problem is very poor and deceptive. But a lot of us have wasted a lot of time in the contest on this. So solution for those legends. Keep grinding\n\n```\ndef minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n \n secs = targetSeconds%60\n mins = targetSeconds//60\n cost = float(\'inf\')\n booli = False\n \n # print(mins,secs)\n if secs==0:\n time1 = str(mins) + \'00\'\n time2 = str(mins - 1) + \'60\'\n if mins >= 99:\n time1 = time2 = str(mins - 1) + str(secs + 60)\n else:\n print(mins)\n if mins<=1:\n # print(\'here\')\n time = str(targetSeconds)\n if int(time) <= 99:\n booli = True\n if 1<=secs <=9:\n time1 = str(mins) +\'0\' + str(secs)\n time2 = str(mins - 1) + str(secs + 60)\n if mins >= 99:\n time1 = time2 = str(mins - 1) + str(secs + 60)\n elif 10<= secs <= 39:\n time1 = str(mins) + str(secs)\n time2 = str(mins - 1) + str(secs + 60)\n if mins >= 99:\n time1 = time2 = str(mins - 1) + str(secs + 60)\n else:\n if mins <=99:\n time1 = str(mins) + str(secs)\n time2 = time1\n else:\n time1=time2 = str(mins-1) + str(secs + 60)\n \n arr = [time1,time2]\n if booli:\n arr.append(time)\n # print(arr)\n for ele in arr:\n if str(startAt)==ele[0]:\n curr = pushCost\n else:\n curr = moveCost+pushCost\n for i in range(1,len(ele)):\n if ele[i] == ele[i-1]:\n curr += pushCost\n else:\n curr += moveCost + pushCost\n print(curr,ele)\n cost = min(cost,curr)\n \n return cost\n```\nSorry for contest quality code. Took me a lot of debugging.
| 2 | 0 |
[]
| 1 |
minimum-cost-to-set-cooking-time
|
[Python 3] Solution and Explanation 😤Edge Cases Sucks 😤
|
python-3-solution-and-explanation-edge-c-sp6b
|
\n# [Python 3] Solution and Explanation\uD83D\uDE24Edge Cases Sucks \uD83D\uDE24\n\n## MAIN IDEA\nWe make targetSeconds into a list to store which buttons we ha
|
gcobs0834
|
NORMAL
|
2022-02-05T16:16:01.874079+00:00
|
2022-02-05T16:20:08.307126+00:00
| 91 | false |
\n# [Python 3] Solution and Explanation\uD83D\uDE24Edge Cases Sucks \uD83D\uDE24\n\n## MAIN IDEA\nWe make targetSeconds into a list to store which buttons we have to press on, and then **calculateCost** to calculate actual cost finally return min cost.\n\n## Cases\n> 1\uFE0F\u20E3 If **targetSeconds < 100** we can directly append a buttonList bt sperate digit in targetSeconds\n> 2\uFE0F\u20E3 We calculate seconds into minutes + remain seconds, but be careful minutes will **greater than 100** \uD83D\uDE24 we have to round it down to seconds.\n> 3\uFE0F\u20E3 In the example we could make 10:00 to 9:60 when second part is **smaller than 40** (dont have to carry in) and **minutes greater than 1**\n\n## Complexity Analysis\n* Time : O(1)\n* Space: O(1)\n\n\n## Code\n```\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n buttons = []\n if targetSeconds < 100:\n buttons.append([int(c) for c in str(targetSeconds)])\n \n minTargets = targetSeconds // 60 * 100 + targetSeconds % 60\n if minTargets // 100 < 100:\n buttons.append([int(c) for c in str(minTargets)])\n \n if minTargets % 100 < 40 and minTargets > 200:\n newTarget = minTargets - 100 + 60\n buttons.append([int(c) for c in str(newTarget)])\n \n minCost = float(\'inf\')\n \n for buttonList in buttons:\n currentCost = self.calculateCost(startAt, moveCost, pushCost, buttonList)\n minCost = min(minCost, currentCost)\n return minCost\n \n \n def calculateCost(self, startAt, moveCost, pushCost, targetButtons):\n cost = 0\n for button in targetButtons:\n if button != startAt:\n cost += moveCost\n startAt = button\n cost += pushCost\n return cost\n```\n* See more 2022 Daily Challenge Solution : [GitHub](https://github.com/gcobs0834/2022-Daily-LeetCoding-Challenge-python3-)
| 2 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
Java | Not easy to understand
|
java-not-easy-to-understand-by-aaveshk-7xyy
|
\nclass Solution\n{\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds)\n {\n int min1 = 0, min2 = 0;\n
|
aaveshk
|
NORMAL
|
2022-02-05T16:13:14.250775+00:00
|
2022-02-09T07:50:39.867994+00:00
| 103 | false |
```\nclass Solution\n{\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds)\n {\n int min1 = 0, min2 = 0;\n int[] num1 = new int[4]; // Storing each digits\n num1[0] = (targetSeconds/60)/10;\n num1[1] = (targetSeconds/60)%10;\n num1[2] = (targetSeconds%60)/10;\n num1[3] = (targetSeconds%60)%10;\n if(targetSeconds >= 6000) // Upper limit is 99 minutes\n {\n num1[0] = 9;\n num1[1] = 9;\n num1[2] += 6;\n }\n int cur = 3; \n int cur_dig = startAt; // Stores last pressed number\n boolean zero = true; // To avoid leading zeroes\n while(cur >= 0)\n {\n if(num1[3-cur] != 0)\n zero = false;\n if(zero && num1[3-cur] == 0) // Leading zeroes\n {\n cur--;\n continue;\n }\n min1 += minCost(cur_dig, moveCost, pushCost, num1[3-cur]);\n cur_dig = num1[3-cur];\n cur--;\n }\n cur = 3;\n if(targetSeconds < 60 || targetSeconds >= 5980 || num1[2]>=4) // Cases where only one way to reach the duration\n return min1;\n if(num1[1] == 0) // If minutes are 10,20,30 etc, next would be 9,19,29 and so on\n {\n num1[1] = 9;\n num1[0]--;\n }\n else // If not 10,20,30, then simply one lesser, e.g. 19 -> 18\n num1[1]--;\n num1[2]+=6;\n zero = true;\n cur_dig = startAt;\n while(cur >= 0)\n { \n if(num1[3-cur] != 0)\n zero = false;\n if(zero && num1[3-cur] == 0)\n {\n cur--;\n continue; \n }\n min2 += minCost(cur_dig, moveCost, pushCost, num1[3-cur]);\n cur_dig = num1[3-cur];\n cur--;\n }\n return Math.min(min1,min2);\n }\n private static int minCost(int cur,int move, int push, int target)\n {\n if(target != cur)\n return move+push;\n else\n return push;\n }\n}\n```
| 2 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
C++ | Beats 100% Time | Only 2 Cases for TargetScore | Easy to Understand
|
c-beats-100-time-only-2-cases-for-target-nzt8
|
\n\nclass Solution {\npublic:\n \n \n long long compute(int start, int move, int push, string target)\n {\n int len = target.size();\n
|
user6192i
|
NORMAL
|
2022-02-05T16:03:00.009710+00:00
|
2022-02-05T16:17:01.335973+00:00
| 314 | false |
\n```\nclass Solution {\npublic:\n \n \n long long compute(int start, int move, int push, string target)\n {\n int len = target.size();\n long long cost = 0;\n int curr = start; //keeps track of start point\n \n for(int i=0;i<len;i++)\n {\n int v1= target[i]-\'0\';\n \n if(curr==v1)\n {\n cost+= push; //if the combination requires a digit at the current point, simply push it\n }else{\n cost+= move+push; //otherwise, we move to the digit and push\n }\n curr=v1;\n }\n return cost;\n }\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n\t\t//There will be two-cases for Target Score ~ (Minutes:Seconds) OR ((Minutes-1):Seconds)\n int a = targetSeconds/60;\n int b = targetSeconds%60;\n \n vector<string> pos;\n \n string fin = "";\n \n if(a>99 && b<=39) //If this is the case, then we only have one possibility that is to have both in minutes\n {\n a--;\n b+=60;\n fin= to_string(a) + to_string(b);\n pos.push_back(fin);\n }else{\n\t\t\n\t\t\t//Case-1\n if(a!=0){\n fin = to_string(a);\n }\n string c = to_string(b);\n \n if(a!=0){ //debug1\n while(c.size()!=2) //Prepending Zeros for seconds if minutes are not 0\n {\n c = "0" + c; \n }\n }\n fin += c;\n pos.push_back(fin);\n \n int save= (targetSeconds- ((a-1)*60)); //this is to check if save is less than 99 as seconds cannot be greater than 99\n \n if((a-1)>=0 && save>=0 && save<= 99) //debug2: if save is within range, we have another possibility\n {\n fin = "";\n if(a-1 !=0){\n fin = to_string(a-1);\n }\n b+= 60;\n fin+= to_string(b);\n pos.push_back(fin);\n }\n }\n \n long long ans= INT_MAX;\n \n\t\t//Now, for every possibility, simply check the cost and find minimum\n for(int i=0;i<pos.size();i++)\n {\n long long cost= compute(startAt, moveCost, pushCost, pos[i]);\n if(cost<ans)\n {\n ans=cost;\n }\n }\n return ans;\n \n }\n};\n```
| 2 | 0 |
['C', 'C++']
| 0 |
minimum-cost-to-set-cooking-time
|
✅ C++ | All Combination | Simple | Easy | Readable
|
c-all-combination-simple-easy-readable-b-ovkt
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
anmoldau_50
|
NORMAL
|
2023-04-05T13:33:16.770057+00:00
|
2023-04-05T13:33:16.770099+00:00
| 245 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n int mm1=targetSeconds;\n int ss1=0;\n mm1=mm1/60;\n ss1=targetSeconds%60;\n \n int mm2=-1,ss2=-1;\n \n if(ss1<=39){\n mm2=mm1-1;\n ss2=ss1+60;\n }\n\n//\t\tcout<<mm2<<" "<<ss2<<endl;\n\n\t\t\n \n int sum1=INT_MAX;\n int sum2=INT_MAX;\n\n if(mm1<=99){\n string s1="";\n if(ss1<10){\n s1=to_string(mm1)+\'0\'+to_string(ss1);\n }\n else{\n s1=to_string(mm1)+to_string(ss1);\n }\n\n string t1="";\n int flag=1;\n for(int i=0;i<s1.length();i++){\n if(flag==0){\n t1=t1+s1[i];\n }\n if(flag==1 && s1[i]!=\'0\'){\n flag=0;\n t1=t1+s1[i];\n }\n }\n \n \n //\t\tcout<<t1<<endl;\n \n\n sum1=0;\n if(int(t1[0]-48)==startAt){\n sum1=sum1+pushCost;\n }\n else{\n sum1=sum1+pushCost+moveCost;\n }\n for(int i=1;i<t1.length();i++){\n if(t1[i]==t1[i-1]){\n sum1=sum1+pushCost;\n }\n else{\n sum1=sum1+pushCost+moveCost;\n }\n }\n }\n\n\n if(mm2!=-1){\n sum2=0;\n string s2=to_string(mm2)+to_string(ss2);\n\t\t\tstring t2="";\n\t\t\tint flag=1;\n\t\t\tfor(int i=0;i<s2.length();i++){\n\t\t\t\tif(flag==0){\n\t\t\t\t\tt2=t2+s2[i];\n\t\t\t\t}\n\t\t\t\tif(flag==1 && s2[i]!=\'0\'){\n\t\t\t\t\tflag=0;\n\t\t\t\t\tt2=t2+s2[i];\n\t\t\t\t}\n\t\t\t}\n//\t\t\tcout<<t2<<endl;\n if(int(t2[0]-48)==startAt){\n sum2=sum2+pushCost;\n }\n\t\t\telse{\n\t\t\t\tsum2=sum2+pushCost+moveCost;\n\t\t\t}\n for(int i=1;i<t2.length();i++){\n if(t2[i]==t2[i-1]){\n sum2=sum2+pushCost;\n }\n else{\n sum2=sum2+pushCost+moveCost;\n }\n }\n \n }\n\n cout<<sum1<<" "<<sum2;\n\n return min(sum1,sum2);\n \n }\n};\n```
| 1 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Python || Sharing my Simple Solution
|
python-sharing-my-simple-solution-by-s-d-ukyq
|
\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n def cost(s):\n ans =
|
s-dl
|
NORMAL
|
2022-08-29T12:33:39.811617+00:00
|
2022-09-08T06:41:17.579825+00:00
| 225 | false |
```\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n def cost(s):\n ans = 0\n prev_pos = startAt\n for i in range(len(s)):\n if prev_pos != int(s[i]):\n prev_pos = int(s[i])\n ans += moveCost\n ans += pushCost\n return ans\n secs = targetSeconds % 60\n mins = (targetSeconds - secs) // 60\n c = sys.maxsize\n if mins < 100:\n c = min(c, cost(str(mins * 100 + secs)))\n if mins > 0 and secs < 40:\n c = min(c, cost(str((mins - 1) * 100 + (secs + 60))))\n return c\n```
| 1 | 0 |
['Python']
| 1 |
minimum-cost-to-set-cooking-time
|
Java | 0ms | with explanation
|
java-0ms-with-explanation-by-yeelimtse-8cv8
|
Implement a function that calculates the cost given mins & secs.\n\nclass Solution {\n int moveCost = 0, pushCost = 0;\n public int minCostSetTime(int sta
|
yeelimtse
|
NORMAL
|
2022-08-18T05:10:44.866901+00:00
|
2022-08-18T05:10:44.866945+00:00
| 315 | false |
Implement a function that calculates the cost given mins & secs.\n```\nclass Solution {\n int moveCost = 0, pushCost = 0;\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n this.moveCost = moveCost;\n this.pushCost = pushCost;\n int mins = targetSeconds / 60;\n int secs = targetSeconds % 60;\n // edge case: mins == 100. The only possible representation is 99:xx, xx = secs + 60\n if (mins == 100) {\n return cost(mins - 1, secs + 60, startAt);\n } \n \n // Multiple representations case: when mins is not 0, and secs is less than 40. \n // eg: 5:12 has two representations: 1. 5:12 (regular) 2. 4: 72 (mins - 1, secs + 60) \n if (mins != 0 && secs < 40) {\n return Math.min(cost(mins, secs, startAt), cost(mins - 1, secs + 60, startAt));\n } \n // Note when mins == 0, only one representation exists (0, secs);\n // Or when secs >= 40, only one representation exists b/c if we add 60 to secs, the \n // results will exceeds 100, which is invalid.\n return cost(mins, secs, startAt);\n }\n \n private int cost(int mins, int secs, int startAt) {\n int res = 0;\n // When mins >= 10, first digit is valid\n if (mins >= 10) {\n int firstDigit = mins / 10;\n if (firstDigit != startAt) {\n startAt = firstDigit;\n res += moveCost;\n }\n res += pushCost;\n }\n // When mins != 0, then the second digit is valid\n if (mins != 0) {\n int secondDigit = mins % 10;\n if (startAt != secondDigit) {\n startAt = secondDigit;\n res += moveCost;\n }\n res += pushCost;\n }\n \n // When mins is not 0 or secs >= 10, the third digit is valid\n if (secs >= 10 || mins != 0) {\n int thirdDigit = secs / 10;\n if (startAt != thirdDigit) {\n startAt = thirdDigit;\n res += moveCost;\n }\n res += pushCost;\n }\n \n // The last digit is always valid\n if (startAt != (secs % 10)) res += moveCost;\n res += pushCost;\n return res;\n } \n}\n```
| 1 | 0 |
['Java']
| 1 |
minimum-cost-to-set-cooking-time
|
Python3 Easy Solution
|
python3-easy-solution-by-pparimita19-lppe
|
```\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n def calculatecost(digits, st
|
pparimita19
|
NORMAL
|
2022-07-22T05:17:52.037736+00:00
|
2022-07-22T05:17:52.037784+00:00
| 76 | false |
```\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n def calculatecost(digits, start, mc,pc):\n a= len(digits)\n if(digits[0]==str(start)):\n cost=pc\n else:\n cost= pc+mc\n for i in range(1,a):\n if(digits[i-1]==digits[i]):\n cost+=pc\n else:\n cost += pc+mc \n return cost\n minutes= targetSeconds//60\n seconds=targetSeconds%60\n cost=2**31-1\n if(minutes<=99):\n digits1= str(minutes*100+seconds)\n cost=calculatecost(digits1, startAt, moveCost,pushCost)\n if seconds<=39:\n digits2=str((minutes-1)*100+seconds+60)\n cost= min(cost,calculatecost(digits2, startAt, moveCost,pushCost))\n return cost\n \n \n \n \n
| 1 | 0 |
[]
| 1 |
minimum-cost-to-set-cooking-time
|
Java Easy to Understand with Explanation O(1) time
|
java-easy-to-understand-with-explanation-7zvi
|
Intuition - \nWe have only two choices:\n\nPunch minutes and seconds as is,\nor punch minutes - 1 and seconds + 60.\nWe just need to check that the number of mi
|
DaenyTargaryen
|
NORMAL
|
2022-07-22T03:30:20.427988+00:00
|
2022-07-22T03:30:20.428027+00:00
| 196 | false |
Intuition - \nWe have only two choices:\n\nPunch minutes and seconds as is,\nor punch minutes - 1 and seconds + 60.\nWe just need to check that the number of minutes and seconds is valid (positive and less than 99).\n\nFor example, for 6039 seconds, we can only use the second option (99:99), as the first option is invalid (100:39).\n\nso we create a method that takes minutes, seconds or minutes-1, seconds+60.\nto convert minutes and seconds to microwave time - microwaveTime = (minutes * 100) + seconds\ndigit - \'0\' to convert the char to integer\nif the current digit is same as our previous finger position, we don\'t need to move our finger again so move cost is 0. \n\ntime - O(1), space O(1)\n\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n int minutes = targetSeconds/60;\n int seconds = targetSeconds%60;\n \n return Math.min(getCost(startAt, minutes, seconds, moveCost, pushCost), getCost(startAt, minutes-1, seconds+60, moveCost, pushCost));\n }\n \n public int getCost(int startAt, int minutes, int seconds, int moveCost, int pushCost) {\n \n if(Math.min(minutes, seconds) < 0 || Math.max(minutes, seconds) > 99) return Integer.MAX_VALUE;\n \n int minCost = 0;\n \n int microwaveTime = (minutes * 100) + seconds;\n char[] digits = String.valueOf(microwaveTime).toCharArray();\n \n for(char digit : digits) {\n \n // digit - \'0\' to convert the char to integer\n int digitInteger = digit - \'0\';\n \n // if the current digit is same as our previous finger position, we don\'t need to move our finger again so move cost is 0. \n minCost = minCost + pushCost + (startAt == digitInteger ? 0 : moveCost);\n \n // assigning the startAt position as the previous position.\n startAt = digitInteger;\n }\n return minCost;\n }\n}
| 1 | 0 |
['Java']
| 2 |
minimum-cost-to-set-cooking-time
|
[C++] 0ms Faster than 100.00%, Clean
|
c-0ms-faster-than-10000-clean-by-yousuf_-h33b
|
\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n string min1 = to_string(targetSeconds
|
Yousuf_ejaz
|
NORMAL
|
2022-03-17T14:10:19.231857+00:00
|
2022-03-17T14:10:19.231909+00:00
| 194 | false |
```\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n string min1 = to_string(targetSeconds/60);\n string sec1 = to_string(targetSeconds%60);\n if(min1=="0")\n min1 = "";\n \n if(min1!="" && sec1.size()==1)\n sec1 = \'0\' + sec1;\n cout<<min1<<sec1<<endl;\n \n string min2 = "";\n string sec2 = "";\n\n if(min1 != "") {\n min2 = to_string(stoi(min1) - 1);\n sec2 = to_string(stoi(sec1) + 60); \n }\n \n if(sec2 != "" && stoi(sec2)>99) {\n min2 = "";\n sec2 = "";\n }\n if(min2=="0")\n min2 = "";\n \n if(min2!="" && sec2.size()==1)\n sec2 = \'0\' + sec2;\n \n cout<<min2<<sec2<<endl;\n\n \n int cost1 = 0;\n int cost2 = 0;\n int st = startAt;\n \n for(auto &i: min1) {\n if(st != (i-\'0\'))\n cost1+=moveCost;\n st = i-\'0\';\n cost1 += pushCost;\n }\n for(auto &i: sec1) {\n if(st != (i-\'0\'))\n cost1+=moveCost;\n st = i-\'0\';\n cost1 += pushCost;\n }\n \n st = startAt;\n \n for(auto &i: min2) {\n if(st != (i-\'0\'))\n cost2+=moveCost;\n st = i-\'0\';\n cost2 += pushCost;\n }\n for(auto &i: sec2) {\n if(st != (i-\'0\'))\n cost2+=moveCost;\n st = i-\'0\';\n cost2 += pushCost;\n }\n if(min1.size() + sec1.size() >4) {\n min1 = "";\n sec1 = "";\n }\n if(min2.size() + sec2.size() >4) {\n min2 = "";\n sec2 = "";\n }\n \n if(min2=="" && sec2=="")\n cost2 = INT_MAX;\n if(min1=="" && sec1=="")\n cost1 = INT_MAX;\n int cost = min(cost1, cost2);\n \n return cost;\n \n }\n};\n```
| 1 | 0 |
['C', 'C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Java, Easy to Understand, COMMENTS! 1 ms, faster than 91.15% & 41 MB, less than 60.77%
|
java-easy-to-understand-comments-1-ms-fa-o9u2
|
```\npublic int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int min = targetSeconds / 60;\n int sec = targetSec
|
tientang
|
NORMAL
|
2022-02-27T07:43:14.501447+00:00
|
2022-02-27T07:43:14.501493+00:00
| 148 | false |
```\npublic int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int min = targetSeconds / 60;\n int sec = targetSeconds % 60;\n \n // note: 1 second <= targetSeconds <= 99min and 99sec\n \n if (min == 100) { // if minutes is 100, only one way to display this\n return getMinCostSetTime(startAt, moveCost, pushCost, targetSeconds, min - 1, sec + 60);\n }\n \n if (sec + 60 <= 99 && min > 0) { // if targetSeconds can be displayed 2 way, return the min cost between them\n return Math.min(getMinCostSetTime(startAt, moveCost, pushCost, targetSeconds, min, sec),\n getMinCostSetTime(startAt, moveCost, pushCost, targetSeconds, min - 1, sec + 60));\n }\n // targetSeconds can only be displayed 1 way, return the cost of fatigue for this way\n return getMinCostSetTime(startAt, moveCost, pushCost, targetSeconds, min, sec); \n }\n \n // parses through minutes digits and seconds digits and sums the cost of fatigue for each action taken\n public int getMinCostSetTime(int lastPressed, int moveCost, int pushCost, int targetSeconds, int min, int sec) {\n int cost = 0;\n if (min > 0) { // if minutes are 0, then we do not need to press any thing for those digits\n if (min / 10 > 0) { // check 1st digit of minutes\n if (lastPressed != min / 10) { // checking if our finger is on the last/startAt number\n cost += moveCost;\n lastPressed = min / 10; // track prev pressed number\n }\n cost += pushCost;\n }\n if (lastPressed != min % 10) { // checks 2nd digit of minutes\n cost += moveCost;\n lastPressed = min % 10;\n }\n cost += pushCost;\n }\n \n if (min != 0 || sec / 10 != 0) { // if minutes = 0 and the 1st digit of seconds is 0, we can skip, otherwise:\n if (lastPressed != sec / 10) {\n cost += moveCost;\n lastPressed = sec / 10;\n }\n cost += pushCost;\n }\n // no checks here because we will always print this because targetSeconds >= 1\n if (lastPressed != sec % 10) cost += moveCost;\n cost += pushCost;\n \n return cost;\n }
| 1 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
c++ solution
|
c-solution-by-navneetkchy-zi2t
|
\nclass Solution {\npublic:\n int getSeconds(int time) {\n int a = time % 10;\n time /= 10;\n int b = time % 10;\n time /= 10;\n
|
navneetkchy
|
NORMAL
|
2022-02-19T06:16:03.363236+00:00
|
2022-02-19T06:16:03.363275+00:00
| 88 | false |
```\nclass Solution {\npublic:\n int getSeconds(int time) {\n int a = time % 10;\n time /= 10;\n int b = time % 10;\n time /= 10;\n int c = time % 10;\n time /= 10;\n int d = time % 10;\n time /= 10;\n int seconds = b * 10 + a;\n int minutes = d * 10 + c;\n return minutes * 60 + seconds;\n }\n int ans = 1e9 + 7;\n void dfs(int current, int time, int moveCost, int pushCost, int targetSeconds, int pos, int cost) {\n if(pos == 4) {\n if(targetSeconds == getSeconds(time)) {\n ans = min(ans, cost);\n }\n return;\n }\n if(targetSeconds == getSeconds(time)) {\n ans = min(ans, cost);\n }\n for(int i = 0; i < 10; i++) {\n if(current == i) {\n dfs(current, time * 10 + i, moveCost, pushCost, targetSeconds, pos + 1, cost + pushCost);\n }\n else {\n dfs(i, time * 10 + i, moveCost, pushCost, targetSeconds, pos + 1, cost + moveCost + pushCost);\n }\n }\n \n }\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n dfs(startAt, 0, moveCost, pushCost, targetSeconds, 0, 0);\n return ans;\n }\n};\n```
| 1 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
JAVA || EASY SOLUTION || O(1) TC
|
java-easy-solution-o1-tc-by-aniket7419-dugv
|
\nclass Solution {\n void helper(int data[],int min,int sec){\n data[1]=min%10;\n data[0]=(min/10)%10;\n data[3]=sec%10;\n data[2
|
aniket7419
|
NORMAL
|
2022-02-08T16:55:22.818378+00:00
|
2022-02-08T16:55:22.818416+00:00
| 78 | false |
```\nclass Solution {\n void helper(int data[],int min,int sec){\n data[1]=min%10;\n data[0]=(min/10)%10;\n data[3]=sec%10;\n data[2]=(sec/10)%10;\n }\n \n int getCost(int current,int mcost,int pcost,int data[]){\n int p=0,cost=0;\n while(p<4&&data[p]==0)p++;\n while(p<4){\n if(data[p]==current){\n cost+=pcost;\n }\n else{\n cost+=(pcost+mcost);\n current=data[p];\n }\n p++;\n \n }\n return cost;\n }\n \n public int minCostSetTime(int startAt, int moveCost, int pushCost, int seconds) {\n int min=seconds/60;\n int sec=seconds%60;\n int res=Integer.MAX_VALUE;\n int data[]=new int[4];\n if(min>=1&&sec<=39){\n \nhelper(data,min-1,sec+60);\n res=getCost(startAt,moveCost,pushCost,data);\n }\n helper(data,min,sec);\n if(min<=99)\n res=Math.min(res,getCost(startAt,moveCost,pushCost,data));\n return res; \n }\n}\n```
| 1 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
Java Simple Solution [Faster Than 100%]
|
java-simple-solution-faster-than-100-by-kbydl
|
\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int ts) {\n int min = ts / 60; \n int sec = ts % 60;\n
|
SheelBouddh
|
NORMAL
|
2022-02-07T03:57:48.388910+00:00
|
2022-02-07T03:57:48.388955+00:00
| 48 | false |
```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int ts) {\n int min = ts / 60; \n int sec = ts % 60;\n \n if(min > 99){\n min--;\n sec += 60;\n }\n\t\t\n int ans = Integer.MAX_VALUE;\n while(min >=0 && sec <= 99){\n ans = Math.min(ans, cost(startAt, moveCost, pushCost, (min * 100) + sec));\n min--;\n sec+=60;\n }\n return ans;\n }\n \n int cost(int start, int move, int push, int time){\n List<Integer> list = new ArrayList<>();\n while(time != 0){\n list.add(time%10);\n time = time/10;\n }\n int res = 0;\n for(int i = list.size()-1; i>=0; i--){\n if(list.get(i) == start){\n res += push;\n continue;\n }\n \n res += move + push;\n start = list.get(i);\n }\n return res;\n }\n}\n```
| 1 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
[C++ Solution] | BruteForce | Easy to Read
|
c-solution-bruteforce-easy-to-read-by-vr-5p55
|
There are 2 choices either take the exact time that is 669 seconds can be written as 11 minutes and 09 seconds or we can write it as 10 minutes and 69 seconds.
|
Vrundan28
|
NORMAL
|
2022-02-06T20:09:57.986097+00:00
|
2022-02-06T20:09:57.986127+00:00
| 46 | false |
There are 2 choices either take the exact time that is 669 seconds can be written as 11 minutes and 09 seconds or we can write it as 10 minutes and 69 seconds. \nAnd there would be some edge cases that such as:\n If minutes are greater than 99 then we cannot take 100 minutes and 39 seconds we need to take 99 minutes and 99 seconds.....\n\n```\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int ans=INT_MAX; \n \n string x = to_string(targetSeconds/60),y = to_string(targetSeconds%60);\n int tmp=0,tmp1=startAt;\n if(x.length() == 1 && x[0] != \'0\')\n {\n if(tmp1 != (x[0]-\'0\'))\n tmp += moveCost,tmp1 = x[0]-\'0\';\n tmp += pushCost;\n }\n else if(x.length() == 2)\n {\n if(tmp1 != (x[0]-\'0\'))\n tmp += moveCost,tmp1 = x[0]-\'0\';\n tmp += pushCost;\n \n if(tmp1 != (x[1]-\'0\'))\n tmp += moveCost,tmp1 = x[1]-\'0\';\n tmp += pushCost; \n }\n \n if(y.length() == 1)\n {\n if(x[0] != \'0\')\n {\n if(tmp1 != 0)\n tmp += moveCost,tmp1 = 0;\n tmp += pushCost;\n }\n if(tmp1 != (y[0]-\'0\'))\n tmp += moveCost,tmp1 = y[0]-\'0\';\n tmp += pushCost;\n }\n else if(y.length() == 2)\n {\n if(tmp1 != (y[0]-\'0\'))\n tmp += moveCost,tmp1 = y[0]-\'0\';\n tmp += pushCost;\n \n if(tmp1 != (y[1]-\'0\'))\n tmp += moveCost,tmp1 = y[1]-\'0\';\n tmp += pushCost; \n }\n if(x.length() < 3)\n ans = min(ans,tmp);\n \n tmp=0;\n if(((targetSeconds%60) + 60) > 99 || targetSeconds/60 == 0)\n return ans;\n\n x = to_string(max(0,(targetSeconds/60) - 1)),y = to_string((targetSeconds%60) + 60);\n tmp1=startAt;\n\n if(x.length() == 1 && x[0] != \'0\')\n {\n if(tmp1 != (x[0]-\'0\'))\n tmp += moveCost,tmp1 = x[0]-\'0\';\n tmp += pushCost;\n }\n else if(x.length() == 2)\n {\n if(tmp1 != (x[0]-\'0\'))\n tmp += moveCost,tmp1 = x[0]-\'0\';\n tmp += pushCost;\n \n if(tmp1 != (x[1]-\'0\'))\n tmp += moveCost,tmp1 = x[1]-\'0\';\n tmp += pushCost; \n }\n if(y.length() == 1)\n {\n if(x[0] != \'0\')\n {\n if(tmp1 != 0)\n tmp += moveCost,tmp1 = 0;\n tmp += pushCost;\n }\n if(tmp1 != (y[0]-\'0\'))\n tmp += moveCost,tmp1 = y[0]-\'0\';\n tmp += pushCost;\n }\n else if(y.length() == 2)\n {\n if(tmp1 != (y[0]-\'0\'))\n tmp += moveCost,tmp1 = y[0]-\'0\';\n tmp += pushCost;\n \n if(tmp1 != (y[1]-\'0\'))\n tmp += moveCost,tmp1 = y[1]-\'0\';\n tmp += pushCost; \n }\n ans = min(ans,tmp);\n \n return ans; \n }\n};\n```
| 1 | 0 |
['C']
| 0 |
minimum-cost-to-set-cooking-time
|
✅ C++ || Easy || Readable || Short Code
|
c-easy-readable-short-code-by-xmenvikas0-95hu
|
2 possible time settings, compare cost of both\n1. Minutes = time/60 , seconds = time%60\n2. Minutes = time/60 - 1 , seconds = time%60 + 60\n\nNote : Also chec
|
xmenvikas07
|
NORMAL
|
2022-02-06T08:54:37.349091+00:00
|
2022-02-06T09:24:20.906905+00:00
| 67 | false |
### 2 possible time settings, compare cost of both\n1. Minutes = time/60 , seconds = time%60\n2. Minutes = time/60 - 1 , seconds = time%60 + 60\n\nNote : Also check if minutes and seconds are in the display range i.e. [1,99]\n\n```\nclass Solution {\n int s,m,p,t;\n \n int computeCost(int &Min,int &sec){\n string a1 = (Min>0) ? to_string(Min) : "";\n if(sec <= 9 && Min != 0) a1 += "0" + to_string(sec);\n else a1 += to_string(sec);\n \n int cost = ((a1[0]-\'0\') != s) ? m : 0;\n int i = 0;\n while(i < a1.size()){\n cost += p;\n if(i>0 && a1[i-1] != a1[i]) cost += m;\n i++;\n }\n return cost;\n }\n \npublic:\n int minCostSetTime(int start, int move, int push, int time) {\n s = start , m = move, p = push , t = time;\n int M = t/60 , S = t%60 , cost = INT_MAX;\n for(int i = -1 ; i<=0 ; i++){\n int Min = M+i , sec = S -60*i;\n if(Min<0 || Min>99 || sec>99 || sec<0) continue;\n cost = min(cost , computeCost(Min,sec));\n }\n return cost;\n }\n};\n```
| 1 | 0 |
['String', 'C']
| 0 |
minimum-cost-to-set-cooking-time
|
Java | Easy To understand with Explanation | 100%
|
java-easy-to-understand-with-explanation-zo1i
|
First Find minutes and seconds from targetSeconds\nlike -> minutes = targetSeconds / 60 and seconds = targetSeconds % 60\n\n\nConsider the condition that minut
|
bhanugupta09
|
NORMAL
|
2022-02-05T19:59:49.541711+00:00
|
2022-02-05T19:59:49.541741+00:00
| 113 | false |
First Find minutes and seconds from targetSeconds\nlike -> minutes = targetSeconds / 60 and seconds = targetSeconds % 60\n\n\nConsider the condition that minutes can\'t exceed 99 and similar with seconds\nif minutes > 99 then subtract -1 and add 60 to seconds. \nCreate time number by using the minutes and seconds for better understating check this:\n\n\n\n\nAfter creating the number find its cost by comparing each value with startAt(s)\n* Check Leading Zeros\n* If array value is equal to startAt(s) then add pushCost(p) to the ans \n* Else add moveCost and pushCost to the ans and **don\'t forget to change the startAt(s)**\n\nNow reduce the minutes by 1 and add increment seconds by 60 and do the same proces \n* only If minutes >= 0 and seconds <= 99\n\nafter that compare values and return minimum value.\n\n\n\n\n\n**Check My Code Below**\n```\nclass Solution {\n public int minCostSetTime(int s, int m, int p, int target){\n int ans = Integer.MAX_VALUE;\n int min = target/60;\n int sec = target%60;\n if(min > 99){\n min -= 1;\n sec += 60;\n }\n while(min >= 0 && sec <= 99){\n int res = 0;\n int check = s;\n int[] arr = new int[4];\n arr[0] = min/10;\n arr[1] = min%10;\n arr[2] = sec/10;\n arr[3] = sec%10;\n boolean isZero = false;\n for(int i=0; i<4; i++){\n if(arr[i] == 0 && !isZero){\n continue;\n }\n isZero = true;\n if(arr[i] == check){\n res += p;\n }\n else{\n res += m + p;\n check = arr[i];\n }\n }\n min -= 1;\n sec += 60;\n ans = Math.min(res, ans);\n }\n return ans;\n }\n}\n\n```\n\nHope It helps. If you like this Explaination please UpVote ! Happy Coding
| 1 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Easy Solution
|
easy-solution-by-srivaayush-crs7
|
```\nclass Solution {\npublic:\n int minCostSetTime(int sa, int mc, int pc, int ts) {\n int mnMin=ts/60,mnSec=ts%60;\n int mnMin1=ts/60,mnSec1=
|
srivaayush
|
NORMAL
|
2022-02-05T19:31:08.042441+00:00
|
2022-02-05T19:31:08.042482+00:00
| 58 | false |
```\nclass Solution {\npublic:\n int minCostSetTime(int sa, int mc, int pc, int ts) {\n int mnMin=ts/60,mnSec=ts%60;\n int mnMin1=ts/60,mnSec1=ts%60;\n if(mnSec1<40 && mnMin1!=0){\n mnSec1=mnSec+60;\n mnMin1--;\n }\n string s1="",s2="";\n s1=to_string(mnMin*100+mnSec);\n s2=to_string(mnMin1*100+mnSec1);\n int a1=0,a2=0,t=sa,c0=0;\n for(auto &x:s1){\n if(sa==x-\'0\'){\n a1+=pc;\n }\n else{\n a1+=pc+mc;\n sa=x-\'0\';\n }\n }\n sa=t;t=sa;\n c0=0;\n for(auto &x:s2){ \n if(sa==(x-\'0\')){\n a2+=pc;\n }\n else{\n a2+=pc+mc;\n sa=x-\'0\';\n }\n }\n if(mnMin==100)\n return a2;\n return min(a1,a2);\n \n }\n};
| 1 | 0 |
['C', 'C++']
| 0 |
minimum-cost-to-set-cooking-time
|
C++ || explanation || 100% || 0ms
|
c-explanation-100-0ms-by-bss47-a93o
|
\n Approach:\n Handle two cases separately :\n case:1 --> targetSeconds<100\n case:2 --> targetSeconds>=100\n\n find all possible time expressions\n then chec
|
bss47
|
NORMAL
|
2022-02-05T19:15:41.615539+00:00
|
2022-02-05T19:16:24.150211+00:00
| 49 | false |
\n Approach:\n Handle two cases separately :\n case:1 --> targetSeconds<100\n case:2 --> targetSeconds>=100\n\n find all possible time expressions\n then check move and push conditions for each expressions\n and output minimum of them.\n\n```\nclass Solution {\npublic:\n int minCostSetTime(int start, int mC, int pC, int tS) {\n \n int ans=INT_MAX;\n if(tS<100)\n {\n vector<string> vt; // all possible time expressions\n \n string s= to_string(tS);\n while(s.size()<=4)\n {\n vt.push_back(s);\n s= "0"+s;\n }\n \n int x= tS/60;\n int rem= tS%60;\n if(x!=0)\n {\n string p1= to_string(x);\n string p2= to_string(rem);\n while(p2.size()!=2)\n {\n p2= "0"+p2;\n }\n vt.push_back(p1+p2);\n if(p1.size()==1) vt.push_back("0"+p1+p2);\n }\n \n int tmp=0;\n for(int i=0; i<vt.size(); i++)\n {\n tmp= start;\n int cost=0;\n for(auto ch: vt[i])\n {\n if(tmp!=(ch-\'0\') ) // we have to move + push\n {\n cost+=(mC+pC);\n tmp= (ch-\'0\');\n }\n else cost+= pC; // we have to only push\n }\n ans= min(ans,cost);\n }\n \n }\n else \n {\n vector<string> vt; // all possible time expressions\n \n int x= tS/60;\n int rem= tS%60;\n if(x==100) // IMP case take tS=6039---> x=100, rem=39\n {\n x--;\n rem+=60;\n }\n \n string p1= to_string(x);\n string p2= to_string(rem);\n while(p2.size()!=2)\n {\n p2= "0"+p2;\n }\n vt.push_back(p1+p2);\n if(p1.size()==1) vt.push_back("0"+p1+p2);\n \n if(rem+60 <=99) // IMP step \n {\n x--;\n rem+= 60;\n if(x==0)\n {\n string tt= to_string(rem);\n while(tt.size()<=4)\n {\n vt.push_back(tt);\n tt= "0"+ tt;\n }\n }\n else\n {\n string g1= to_string(x);\n string g2= to_string(rem);\n \n vt.push_back(g1+g2);\n if(g1.size()==1) vt.push_back("0"+g1+g2);\n }\n }\n \n \n int tmp=0;\n for(int i=0; i<vt.size(); i++)\n {\n tmp= start;\n int cost=0;\n for(auto ch: vt[i])\n {\n if(tmp!=(ch-\'0\') )\n {\n cost+=(mC+pC);\n tmp= (ch-\'0\');\n }\n else cost+= pC;\n }\n ans= min(ans,cost);\n }\n }\n return ans;\n }\n};\n```
| 1 | 0 |
['Greedy']
| 1 |
minimum-cost-to-set-cooking-time
|
O(1) SC & TC || Simple & Easy C++ Solution
|
o1-sc-tc-simple-easy-c-solution-by-mridu-vm8p
|
\nFor any given targetSeconds given we will have ony two possible times \n\n\t1) targetSecoond/60 : targetSecoond%60\n\t2) targetSecoond/60 - 1 : t
|
mridul20
|
NORMAL
|
2022-02-05T19:10:03.235563+00:00
|
2022-02-05T19:15:06.977410+00:00
| 46 | false |
\nFor any given targetSeconds given we will have ony two possible times \n\n\t1) targetSecoond/60 : targetSecoond%60\n\t2) targetSecoond/60 - 1 : targetSecoond%60 + 60\nSo we just need to calculate these two time and check whether they are valid or not i.e lies between 00:00 and 99:99.\n\nAfter that we can cacluate the cost for all the valid times (atmost two) by ignoring all the leading zeroes.\n\n\tTime Complexity : O(1)\n\tSpace Complecity : O(1)\n\n\n```\nvoid numtovec(int n, vector<int> &v)\n{\n v.push_back(n / 10);\n v.push_back(n % 10);\n}\n\nint ans(int x, vector<int> &time, int moveCost, int pushCost)\n{\n int cst = 0, j, i;\n for (j = 0; j < time.size(); j++)\n if (time[j] != 0)\n break;\n for (i = j; i < time.size(); i++)\n {\n if (time[i] != x)\n cst += moveCost;\n cst += pushCost;\n x = time[i];\n }\n return cst;\n}\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n vector<int> time1, time2;\n\n // Time 1\n int minute1 = targetSeconds / 60, second1 = targetSeconds % 60;\n numtovec(minute1, time1);\n numtovec(second1, time1);\n int cost1 = ans(startAt, time1, moveCost, pushCost);\n\n //Time 2\n int minute2 = minute1 - 1, second2 = second1 + 60;\n numtovec(minute2, time2);\n numtovec(second2, time2);\n int cost2 = ans(startAt, time2, moveCost, pushCost);\n\n\n if (minute1 < 0 || minute1 > 99 || second1 < 0 || second1 > 99)\n cost1 = INT_MAX;\n if (minute2 < 0 || minute2 > 99 || second2 < 0 || second2 > 99)\n cost2 = INT_MAX;\n return min(cost1, cost2);\n }\n};\n```\n
| 1 | 0 |
['C']
| 1 |
minimum-cost-to-set-cooking-time
|
C++ solution || Beginner Friendly || Explanation with code || O(1) solution
|
c-solution-beginner-friendly-explanation-4dzx
|
\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
|
Manav888
|
NORMAL
|
2022-02-05T18:58:11.868479+00:00
|
2022-02-05T18:58:11.868507+00:00
| 37 | false |
\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
| 1 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
Easy java solution 1ms solution
|
easy-java-solution-1ms-solution-by-khush-o5yx
|
There are atmost two possible combinations for any targetSeconds\n\teg. targetSeconds = 745\n\tminutes = 745/60 = 12;\n\tseconds = 745%12 = 25;\n\t=> first comb
|
khushi3
|
NORMAL
|
2022-02-05T18:43:03.414110+00:00
|
2022-02-05T18:45:19.728484+00:00
| 54 | false |
1. There are atmost two possible combinations for any targetSeconds\n\teg. targetSeconds = 745\n\tminutes = 745/60 = 12;\n\tseconds = 745%12 = 25;\n\t=> first combination 12:25\n\t\n\tNow since after adding 60 to seconds, it wont exceed 99, we can have\n\tcombination of (minutes-1, seconds+60)\n\t=> second combination 11:85\n\t\n\tcost = min(costOfFirstCombination, costOfSecondCombination);\n\t\n\tInside findCost function, we first store the minutes and seconds in an array\n\ttime[] = {1,2,2,5}\n\t-If we initially have any zeroes, we leave them as it is\n\t-If start is same as current digit, we don\'t need to move, we just need to push that button\n\t-if start is not same as current digit, then we need to do both, move and push , so we add both of its cost\n\t-and we set the startAt as the current digit, so if the next digit is same as the current digit, we don\'t move anywhere.\n\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int minutes = targetSeconds/60;\n int seconds = targetSeconds%60;\n int cost = Integer.MAX_VALUE;\n\n cost = Math.min(\n findCost(minutes, seconds, startAt, moveCost, pushCost),\n findCost(minutes-1, seconds+60, startAt, moveCost, pushCost)); \n\t\t\t\n return cost;\n }\n \n int findCost(int minutes, int seconds, int startAt, int moveCost, int pushCost){\n if(minutes>99 || seconds>99) return Integer.MAX_VALUE;\n int[] time = new int[4];\n time[1] = minutes%10;\n time[0] = minutes/10;\n \n time[3] = seconds%10;\n time[2] = seconds/10;\n \n boolean flag = false; \n //flag for checking if first digit occured\n\t\t//so that we skip only the trailing zeroes, not the inclusive zeroes\n //00:76, if this is the case, we need to ignore first two zeroes\n //12:00, we can\'t ignore any zeroes here\n //09:60, we just ignore first zero\n int cost = 0;\n \n for(int i=0; i<4; i++){\n if(time[i]==0 && !flag){\n continue;\n }\n else if(time[i]==startAt){\n flag = true;\n cost += pushCost;\n }\n else{\n flag = true;\n cost += pushCost + moveCost;\n startAt = time[i];\n }\n }\n \n return cost; \n }\n}\n```
| 1 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Java | Easy Solution | 100% | Maximum Two Possible Ways
|
java-easy-solution-100-maximum-two-possi-6lq2
|
Solution : Minimum(cost of all possible ways)\n\nMinimum Possible Ways : ONE(1) => minutes : seconds\nMaximum Possible Ways : TWO(2) => (1) minutes : seconds an
|
codeabhi07
|
NORMAL
|
2022-02-05T18:02:38.529705+00:00
|
2022-02-05T18:07:15.164536+00:00
| 134 | false |
Solution : **Minimum(cost of all possible ways)**\n\nMinimum Possible Ways : ONE(1) => **minutes : seconds**\nMaximum Possible Ways : TWO(2) => (1) **minutes : seconds** and (2) **(minutes-1) : (seconds+60)**\n\n**Intuition behind maximum possible ways**:\n\nWays to write 99 seconds => 01:39 and 00:99\nBut, 1:39 and :99 are also valid.\nSo, maximum possible ways comes to be FOUR(4).\n\nWait, If you carefully observer 01:39 and 1:39\nYou always find 1:39 as minimum solution, as extra ZEROs(0) will always add extra pushCost and moveCost.\n\nThus, we can neglect possible ways with prepending zeroes.\n\n\n\n\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n //minutes\n int min=targetSeconds/60;\n //seconds\n int sec=targetSeconds%60;\n \n int ans=Integer.MAX_VALUE;\n Integer n=0;\n \n //Edge case: When minutes exceeds 99 \n //e.g., targetSeconds=6008 => Only one possible way 99:68\n if(min>99){ min=99; sec+=60; }\n \n //When rem <= 39, Two ways are possible : (1) min:rem (2) min-1:rem+60 \n //e.g., targetSeconds=500 => (1) 8:20 (2) 7:80\n if (sec <= 39) {\n //calculating cost for (2) min-1:rem+60\n Integer m=(min-1)*100+sec+60;\n ans=Math.min(ans,cost(startAt,moveCost, pushCost,m.toString().toCharArray()));\n } \n \n //calculating cost for (1) min:rem\n n=min*100+sec;\n //finding minimun b/w (1) and (2), if (2) exists \n ans=Math.min(ans,cost(startAt,moveCost, pushCost, n.toString().toCharArray()));\n \n \n return ans;\n }\n \n //cost(1,2,1,{\'1\',\'0\',\'0\',\'0\'}) => returns 6\n //cost(1,2,1,{\'9,\'6\',\'0\'}) =>returns 9\n public int cost(int startAt,int moveCost, int pushCost, char num[]){\n \n int ans=pushCost*num.length;\n \n for(char c: num){\n if(startAt!=c-\'0\'){\n ans+=moveCost; \n startAt=c-\'0\';\n }\n }\n return ans; \n }\n}\n```
| 1 | 0 |
['Java']
| 1 |
minimum-cost-to-set-cooking-time
|
Runtime 32 ms [Python3] deep explanation with comments.
|
runtime-32-ms-python3-deep-explanation-w-2wan
|
we will divide this into parts ok! first part if 0<target<10 and second part 10<target<100 and third part greater than 100.\nFirst Part:\n\t\tThe maximum cost w
|
laxminarayanak
|
NORMAL
|
2022-02-05T17:47:28.520969+00:00
|
2022-02-05T18:00:02.557517+00:00
| 114 | false |
we will divide this into parts ok! first part if 0<target<10 and second part 10<target<100 and third part greater than 100.\n**First Part:**\n\t\tThe maximum cost will be let max_cost=1*(moveCost +pushCost) for any case go check by ourself once. So if startAt==targetseconds means we are not making move cost so we return max_cost -moveCost else return max_cost.Easy Right.\n\n**Second Part**\n\t\t\tThe maximun cost will be let max_cost=2*(moveCost+pushCost) for any case. We will use\n\t\t\tsome function to return count if recently pushed is repeated. for example look below function.For example:let\'s take target=99 and startAt=9 using below function we get q=2 check it by ourself. From q it says that q no of moveCost are additional in max_cost so we remove q*moveCost from max_cost.Easy right.\n\t\t\t\n\n**Third Case:**\n\tGet the maximum and minimun minutes from below code as show i=max and j=min:\n\t\n\n**Case 1: j<10**\n\t\t\t\t\t\tHere maximum cost is 3*(moveCost+pushCost) check it by ourself. It same as second part here we are going to run loop for k in range( j , min(i,9)+1) # we are only considering 0<k<10#.\n\t\t\t\t\t\tHere k=mins and to get remaining seconds h=abs(targetSeconds-(k*60)) and finally update h with str(k)+str(h)#mins+seconds# and remaining is same as second part except we need to \nupdate self.z=startAt to compare new k.\n**Case 2:i>=10:**\n\t\t\t\t\t\t\tHere max_cost=4*(move_Cost+push_Cost) and same as case 1 but for loop range(max(10,j),i+1)# i should be greater than 9#\n\n```class Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n #fun to find count of previous push\n def leng(t):\n q=0\n if t==self.z:\n q+=1\n return q\n self.z=startAt\n #i=max of minutes\n i=targetSeconds//60\n if i>99:\n i=int(99)\n #j=min of minutes\n j=math.ceil((targetSeconds-99)/60)\n if j<0:\n j=0\n #return value mi\n mi=float(inf)\n #first part\n if targetSeconds<10:\n #max cost\n m=1*(moveCost+pushCost)\n if startAt==targetSeconds:\n m-=moveCost\n return m\n #second part\n if targetSeconds<100:\n #max cost\n m=2*(moveCost+pushCost)\n h=targetSeconds\n q=0\n for b in str(h):\n q+=leng(int(b))\n self.z=int(b)\n mi=m-q*(moveCost)\n #third part case1\n if j<10:\n #max cost\n m=3*(moveCost+pushCost)\n #k sholud be in range(0,10) \n for k in range(j,min(i,9)+1):\n h=abs(targetSeconds-(k*60))\n #h=0 but in pratical it should be 00\n if h==0:\n h=\'00\'\n #if h ==single b=digit in pratical it sholud be 0prefixed\n elif h<10:\n h=str(\'0\')+str(h)\n #mins+seconds\n h=str(k)+str(h)\n q=0\n for b in str(h):\n q+=leng(int(b))\n self.z=int(b)\n #self.z=startAt because we need to restart for next iteration\n self.z=startAt\n y=m-q*(moveCost)\n mi=min(y,mi)\n if i>=10:\n #max cost\n m=4*(moveCost+pushCost)\n for k in range(max(10,j),i+1):\n h=abs(targetSeconds-(k*60))\n #h=0 but in pratical it should be 00\n if h==0:\n h=\'00\'\n #if h ==single b=digit in pratical it sholud be 0prefixed\n elif h<10:\n h=str(\'0\')+str(h)\n #mins+seconds\n h=str(k)+str(h)\n q=0\n for i in h:\n q+=leng(int(i))\n self.z=int(i)\n #self.z=startAt because we need to restart for next iteration\n self.z=startAt\n y=m-q*(moveCost)\n k=int(k//10)\n mi=min(y,mi)\n \n return mi\n \n ```\n **PLEASE UPVOTE MY FRIEND YOU WON\'T LOOSE NOTHING BY UPVOTING!**
| 1 | 0 |
['Python']
| 0 |
minimum-cost-to-set-cooking-time
|
Java | min cost of (up to) 2 possible options, O(1)
|
java-min-cost-of-up-to-2-possible-option-6mhm
|
Only up to 2 ways of setting the cooking time exist (ignoring those with explicitly pressed leading zeros) - just calculate their cost and pick the smallest.\n`
|
prezes
|
NORMAL
|
2022-02-05T17:37:51.230099+00:00
|
2022-02-05T17:55:11.382899+00:00
| 37 | false |
Only up to 2 ways of setting the cooking time exist (ignoring those with explicitly pressed leading zeros) - just calculate their cost and pick the smallest.\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int ans= Integer.MAX_VALUE, mins= targetSeconds/60, secs= targetSeconds%60;\n if(mins>0 && secs<40) // solution exists with >= 60 seconds\n ans= cost(startAt, moveCost, pushCost, mins-1, secs+60);\n if(mins<100) //solution exists with <60 seconds\n ans= Math.min(ans, cost(startAt, moveCost, pushCost, mins, secs));\n return ans;\n }\n \n int cost(int startAt, int moveCost, int pushCost, int mins, int secs){\n int i=0, ans= 0, dig[]= {mins/10, mins%10, secs/10, secs%10};\n while(dig[i]==0) i++; // skip initial zeros\n for(int curr= startAt; i<4; curr= dig[i++]) // calculate cost of digits\n ans+= pushCost + (dig[i]!=curr ? moveCost : 0);\n return ans;\n }\n}
| 1 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
Java | Easy
|
java-easy-by-changeme-c36r
|
We have two choices:\n Convert targetSeconds to minutes and seconds as is\n Convert targetSeconds to minutes and seconds and try to move one minute to seconds (
|
changeme
|
NORMAL
|
2022-02-05T17:08:02.856160+00:00
|
2022-04-27T02:58:27.432701+00:00
| 52 | false |
We have two choices:\n* Convert `targetSeconds` to minutes and seconds as is\n* Convert `targetSeconds` to minutes and seconds and try to move one minute to seconds (if we have a room)\n\n**Hidden test case**: `minCostSetTime(0,1,1,6039)` - 99 mins, 99 seconds\nThe result is 100 mins 39 seconds, if we convert to minutes and seconds as is => We need to check if we use 4 digits only\n\n```\npublic int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n\tint mins = targetSeconds / 60;\n\tint secs = targetSeconds % 60;\n\n\t// convert to minutes and seconds as is\n\tint res = getCost(mins * 100 + secs, startAt, moveCost, pushCost);\n\n\t// try to move one minute to seconds\n\tif (secs + 60 <= 99 && mins > 0) {\n\t\tres = Math.min(res, getCost((mins - 1) * 100 + (secs + 60), startAt, moveCost, pushCost));\n\t}\n\n\treturn res;\n}\n\nprivate int getCost(int targetSeconds, int startAt, int moveCost, int pushCost) {\n\t// hidden test case, we can only have 4 digits, minCostSetTime(0,1,1,6039)\n\tif (targetSeconds > 9999) {\n\t\treturn Integer.MAX_VALUE;\n\t}\n\tint res = 0;\n\tvar ch = String.valueOf(targetSeconds).toCharArray();\n\tvar pre = (char) (startAt + \'0\');\n\tfor (int i = 0; i < ch.length; i++) {\n\t\tif (ch[i] != pre) {\n\t\t\tres += moveCost;\n\t\t}\n\t\tres += pushCost;\n\t\tpre = ch[i];\n\t}\n\treturn res;\n}\n```\n
| 1 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
javascript dfs/backtracking build clock string 792ms
|
javascript-dfsbacktracking-build-clock-s-wwlo
|
\nlet start, mc, pc, res, sec;\nconst minCostSetTime = (startAt, moveCost, pushCost, targetSeconds) => {\n start = startAt, mc = moveCost, pc = pushCost, res
|
henrychen222
|
NORMAL
|
2022-02-05T17:06:55.902436+00:00
|
2022-02-05T17:31:52.219341+00:00
| 103 | false |
```\nlet start, mc, pc, res, sec;\nconst minCostSetTime = (startAt, moveCost, pushCost, targetSeconds) => {\n start = startAt, mc = moveCost, pc = pushCost, res = Number.MAX_SAFE_INTEGER, sec = targetSeconds;\n dfs(0, \'\');\n return res;\n};\n\nconst dfs = (idx, cur) => {\n if (cur.length > 4) return;\n if (cur.length == 1 || cur.length == 2) { // length is 1 or 2, only can be minutes like "76", "9"\n if (cur - \'0\' == sec) res = Math.min(res, cal(cur))\n }\n if (cur.length == 4 || cur.length == 3) { // length is 3 or 4, calculate hours and seconds\n let h, m;\n if (cur.length == 3) {\n h = cur.slice(0, 1) - \'0\';\n m = cur.slice(1) - \'0\';\n } else {\n h = cur.slice(0, 2) - \'0\';\n m = cur.slice(2) - \'0\';\n }\n if (h * 60 + m == sec) res = Math.min(res, cal(cur))\n }\n for (let i = 0; i < 10; i++) { // build clock string\n cur += i + \'\';\n dfs(i, cur);\n cur = cur.slice(0, -1);\n }\n};\n\nconst cal = (s) => { // calculate built clock string seconds\n let sum = 0, pre = start;\n for (const c of s) {\n if (c - \'0\' != pre) {\n sum += mc;\n }\n sum += pc;\n pre = c - \'0\';\n }\n return sum;\n};\n```
| 1 | 0 |
['Backtracking', 'Depth-First Search', 'JavaScript']
| 0 |
minimum-cost-to-set-cooking-time
|
[Python3] Dijkstra
|
python3-dijkstra-by-tulkasm-upjq
|
Formalize as a graph problem and interpret (time, digit) as a node, where time is the current cooking time and digit is the digit we have our finger on. The cos
|
tulkasm
|
NORMAL
|
2022-02-05T17:04:56.993561+00:00
|
2022-02-05T17:04:56.993603+00:00
| 68 | false |
Formalize as a graph problem and interpret (time, digit) as a node, where time is the current cooking time and digit is the digit we have our finger on. The cost of an edge is dependent on whether we use the digit we have our finger on or not. Now apply Dijkstra for sparse graphs.\n\n```python\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n if targetSeconds == 0:\n return 0\n \n def newState(state, digit):\n return "".join([state[1:], str(digit)])\n \n def stateSecs(state): \n minutes = int(state[:2])\n secs = int(state[2:])\n return minutes * 60 + secs\n \n def neighs(state, finger_digit):\n for i in range(10):\n new_state = newState(state, i)\n if i == finger_digit:\n yield (new_state, i, pushCost)\n else: \n yield (new_state, i, pushCost + moveCost)\n \n \n INF = float("inf") \n distance = defaultdict(lambda: INF) \n distance[("0000", startAt)] = 0\n heap = [(0, startAt, "0000")]\n while heap:\n d, figdig, state = heapq.heappop(heap)\n if stateSecs(state) == targetSeconds:\n return d\n \n if d == distance[(state, figdig)]: \n for new_state, new_finger, cost in neighs(state, figdig):\n trydist = d + cost\n if trydist < distance[(new_state, new_finger)]:\n distance[(new_state, new_finger)] = trydist\n heapq.heappush(heap, (trydist, new_finger, new_state))\n return -1 \n```
| 1 | 0 |
['Heap (Priority Queue)']
| 0 |
minimum-cost-to-set-cooking-time
|
C++ easy to understand solution
|
c-easy-to-understand-solution-by-uncle_j-ixju
|
Convert time to string and ignore the starting 0\'s. There are two ways:\n1. min + sec\n2. (min-1) + (sec+60) , (if min > 0 && sec < 40)\n3. return minimum cos
|
uncle_john
|
NORMAL
|
2022-02-05T16:53:27.932910+00:00
|
2022-02-05T16:54:51.759503+00:00
| 77 | false |
Convert time to string and ignore the starting 0\'s. There are two ways:\n1. min + sec\n2. (min-1) + (sec+60) , (if min > 0 && sec < 40)\n3. return minimum cost for these two\n\n```\nclass Solution {\npublic:\n int getcost(int startAt, int moveCost, int pushCost, string& str) {\n if(str.empty()) return INT_MAX;\n int ret = 0;\n for(auto &c : str) {\n if(c - \'0\' == startAt) ret += pushCost;\n else {\n ret += moveCost + pushCost;\n startAt = c - \'0\';\n }\n }\n return ret;\n }\n \n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int min = targetSeconds / 60;\n int sec = targetSeconds % 60;\n \n if(min > 99) {\n min = 99;\n sec += 60;\n }\n \n // convert time to string ignoring starting 0\'s\n string str1(""), str2("");\n \n if(min) {\n if(sec < 10) str1 = to_string(min) + "0" + to_string(sec);\n else str1 = to_string(min) + to_string(sec);\n } else {\n str1 = to_string(sec);\n }\n \n if(min && sec < 40) {\n if(min-1) str2 = to_string(min-1) + to_string(sec+60);\n else str2 = to_string(sec+60);\n }\n \n int x = getcost(startAt, moveCost,pushCost, str1);\n int y = getcost(startAt, moveCost, pushCost, str2);\n \n return std::min(x, y);\n }\n};\n```
| 1 | 0 |
['C', 'C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Simple c++ sol, beats 100%-- all edge cases explained
|
simple-c-sol-beats-100-all-edge-cases-ex-07gj
|
\n--Just need to take care of edge cases in this problem, problem is easy\n\n\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int
|
rishabh_29
|
NORMAL
|
2022-02-05T16:44:40.875314+00:00
|
2022-02-16T09:10:27.132389+00:00
| 52 | false |
\n--Just need to take care of edge cases in this problem, problem is easy\n\n\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n \n int cost=INT_MAX, m=targetSeconds/60, n=targetSeconds%60, tc=0, prev=startAt;\n if(m>=100) m=m-1, n=n+60; \n \n int m1=m-1, n1=n+60;\n vector<int> v1,v2;\n \n // cout<<m<<" "<<n<<endl;\n \n while(m)\n {\n int r=m%10;\n v1.push_back(r);\n m/=10;\n }\n reverse(v1.begin(), v1.end());\n \n while(n)\n {\n int r=n%10;\n v2.push_back(r);\n n/=10;\n }\n \n if(v1.size()>0 and v2.size()<2)\n {\n if(v2.size()==0) \n {\n v2.push_back(0);\n v2.push_back(0);\n }\n else v2.push_back(0);\n }\n reverse(v2.begin(), v2.end());\n \n \n for(int i=0; i<v2.size(); i++) v1.push_back(v2[i]);\n \n \n // for(int i=0; i<v1.size(); i++) cout<<v1[i]<<" "; \n //cout<<endl;\n \n for(int i=0; i<v1.size(); i++)\n {\n if(v1[i]==prev) tc+=pushCost;\n else\n {\n tc+=moveCost;\n prev=v1[i];\n tc+=pushCost;\n }\n }\n cost=min(tc,cost);\n \n \n // cout<<m1<<" "<<n1<<endl;\n // if we can represent the time by decresing 1 hr and incresing 60 seconds.\n if(m1>=0 and n1<100)\n {\n // cout<<m1<<" "<<n1<<endl;\n \n vector<int> v3,v4;\n while(m1)\n {\n int r=m1%10;\n v3.push_back(r);\n m1/=10;\n }\n reverse(v3.begin(), v3.end());\n \n while(n1)\n {\n int r=n1%10;\n v4.push_back(r);\n n1/=10;\n }\n if(v3.size()>0 and v4.size()<2)\n {\n if(v4.size()==0) \n {\n v4.push_back(0);\n v4.push_back(0);\n }\n else v4.push_back(0);\n }\n reverse(v4.begin(), v4.end());\n \n for(int i=0; i<v2.size(); i++) v3.push_back(v4[i]);\n \n //for(int i=0; i<v3.size(); i++) cout<<v3[i]<<" "; \n \n tc=0, prev=startAt;\n for(int i=0; i<v3.size(); i++)\n {\n if(v3[i]==prev) tc+=pushCost;\n else\n {\n tc+=moveCost;\n prev=v3[i];\n tc+=pushCost;\n }\n } \n cost=min(tc,cost);\n }\n return cost;\n }\n};
| 1 | 0 |
['Array', 'C']
| 0 |
minimum-cost-to-set-cooking-time
|
C++ | Recursive Solution | Beats 100% | 0ms
|
c-recursive-solution-beats-100-0ms-by-om-hx5q
|
Here is my brute force recursive solution that beats 100%\n\ncpp\nclass Solution {\npublic:\n int dp(int moveCost, int pushCost, int targetSeconds, int lastD
|
omarito
|
NORMAL
|
2022-02-05T16:40:18.567687+00:00
|
2022-02-05T16:40:36.204175+00:00
| 45 | false |
Here is my brute force recursive solution that beats 100%\n\n```cpp\nclass Solution {\npublic:\n int dp(int moveCost, int pushCost, int targetSeconds, int lastDigit, int idx)\n {\n if (targetSeconds < 0)\n return INT_MAX;\n \n if (idx == 1) {\n if (targetSeconds >= 10) {\n return INT_MAX;\n }else{\n return lastDigit == targetSeconds ? pushCost : pushCost + moveCost;\n }\n }\n \n if (targetSeconds == 0)\n return dp(moveCost, pushCost, targetSeconds, 0, idx - 1) + (lastDigit == 0 ? pushCost : pushCost + moveCost);\n \n int m = idx == 4 ? 60*10 : (idx == 3 ? 60 : (idx == 2 ? 10 : 1));\n int result = INT_MAX;\n \n for (int i = 0; i <= 9; i++) {\n if (m * i <= targetSeconds) {\n int cost = dp(moveCost, pushCost, targetSeconds - m * i, i, idx - 1);\n if (cost != INT_MAX) {\n result = min(result, cost + (lastDigit == i ? pushCost : pushCost + moveCost));\n }\n }\n }\n \n return result;\n }\n \n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int mn = INT_MAX;\n for (int i = 4; i >= 1; i--) {\n mn = min(mn, dp(moveCost, pushCost, targetSeconds, startAt, i));\n }\n return mn;\n }\n};\n```
| 1 | 0 |
['Recursion', 'C']
| 0 |
minimum-cost-to-set-cooking-time
|
C++ || MIN-MAX logic, O(1) Time Complexity Solution and O(1) Space.100% faster
|
c-min-max-logic-o1-time-complexity-solut-bomj
|
Logic is explained in code, we need to check minimum minute that can be possible and maximum minute that can be possible.\n\n\n int cal(int minute, int second,
|
karnalrohit
|
NORMAL
|
2022-02-05T16:37:57.827196+00:00
|
2022-02-05T16:39:03.370149+00:00
| 69 | false |
Logic is explained in code, we need to check minimum minute that can be possible and maximum minute that can be possible.\n\n```\n int cal(int minute, int second, int pc, int mc, int sa) {\n int cost = 0;\n\n // m2 represent second digit of minute, m1 represent first digit of minute\n int m2 = minute % 10;\n minute /= 10;\n int m1 = minute % 10;\n\n// s2 represent second digit of minute, s1 represent first digit of minute\n int s2 = second % 10;\n second /= 10;\n int s1 = second % 10;\n\n/* check if first digit of minute is >0 ,\n if no, then we don\'t type \'0\' because it will auto normalize, \n if Yes, then check whether start at is same as first digit(m1),\n if no, then increment cost by move cost+push cost\n if yes, then increment cost by push cost\n set start at to m1\n*/ \n if (m1 > 0) {\n if (sa != m1) {\n cost += pc + mc;\n } else {\n cost += pc;\n }\n sa = m1;\n }\n \n/* check if second digit of minute is >0 or we have typed first digit m1 of minute (then cost>0) ,\n if no, then we don\'t type \'0\' because it will auto normalize, \n if Yes, then check whether start at is same as second digit(m2),\n if no, then increment cost by move cost+push cost\n if yes, then increment cost by push cost\n set start at to m2\n*/\n if (m2 > 0 || cost > 0) {\n if (sa != m2) {\n cost += pc + mc;\n } else {\n cost += pc;\n }\n sa = m2;\n }\n\n /*Just like minute we also need to check whether we need to type s1 and s2 , if we already typed previous digit (that means cost>0)\n then we have no choice except typing s1 and s2, else if cost==0 that means m1=0,m2=0, so if s1=0 then we don\'t type s1 else we need to type \n it . At the end we always have to type s2 because there can always be second for example 00:01, 00:55.let take 00:00 ,even if we start at 0\n then also without pushing(typing) 0 it cannot normalize to 00:00 so, we have to push last digit of second ie s2 */\n if (s1 > 0 || cost > 0) {\n if (sa != s1) {\n cost += pc + mc;\n } else {\n cost += pc;\n }\n sa = s1;\n }\n\n if (sa != s2) {\n cost += pc + mc;\n } else {\n cost += pc;\n }\n sa = s2;\n\n return cost;\n}\nint minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n // Lmin denote minimum minute that we can set to form given tarrget Second\n int lmin = 0;\n if (targetSeconds > 99) lmin = ceil((1.0 * (targetSeconds - 99)) / 60);\n\n //Rmin denote maximum minute that we can set to form given target Second \n int rmin = targetSeconds / 60;\n rmin = min(99, rmin);\n\n\n // Taking Minute from lmin to rmin and computing Second required to form Target Second And Then finding operation needed\n int ans = INT_MAX;\n for (int i = lmin; i <= rmin; i++) {\n int sec = targetSeconds - i * 60;\n int op = cal(i, sec, pushCost, moveCost, startAt);\n ans = min(op, ans);\n }\n return ans;\n}\n```\n\nTime Complexity: O(1)\nminimum minute always be ceil(1.0*targetSeconds-99)/60)\nmaximum minute always be floor(targetSeconds/60)\n\ndifference in between always be <=2 so loop will always for loop run 2times,\nnow each loop iteration call cal function which calculate operation needed that is basically O(1)\n\nSpace Complexity: O(1) because no extra space used
| 1 | 0 |
['Math', 'C', 'C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Jav | All Possible Permutation
|
jav-all-possible-permutation-by-ayushsax-jast
|
\nclass Solution {\n public int solve(List<Integer> list, int startAt, int moveCost, int pushCost){\n int res = 0;\n if(list.get(0) != startAt)
|
AyushSaxena5500
|
NORMAL
|
2022-02-05T16:23:25.787823+00:00
|
2022-02-05T16:23:25.787857+00:00
| 54 | false |
```\nclass Solution {\n public int solve(List<Integer> list, int startAt, int moveCost, int pushCost){\n int res = 0;\n if(list.get(0) != startAt){\n res+=moveCost;\n }\n res+=pushCost;\n \n for(int i=1; i<list.size(); i++){\n if(list.get(i) != list.get(i-1))\n res+=moveCost;\n res+=pushCost;\n }\n return res;\n }\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int cost=(int) 1e8;\n /*\n Just make all possible combinations of 4 digit number from 0-9\n \n */\n for(int i=0; i<=9; i++){\n for(int j=0; j<=9; j++){\n for(int k=0; k<=9; k++){\n for(int l=0; l<=9; l++){\n \n List<Integer> list=new ArrayList<>();\n \n int sec = i*10*60 + j*60 + k*10 + l;\n \n if(sec == targetSeconds){\n list.add(i);\n list.add(j);\n list.add(k);\n list.add(l);\n \n cost=Math.min(cost, solve(list, startAt, moveCost, pushCost));\n \n while(list.get(0) == 0){\n list.remove(0);\n cost=Math.min(cost, solve(list, startAt, moveCost, pushCost));\n }\n }\n }\n }\n }\n }\n \n return cost;\n }\n}\n```
| 1 | 0 |
['Probability and Statistics', 'Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Typescript/Javascript solution
|
typescriptjavascript-solution-by-georgii-xc67
|
Simple, but tons of corner cases. :/\nI send wrong solutions 3 times during contest... All in all -- love this problem :)\n\n\nfunction minCostSetTime(\n sta
|
georgiiperepechko
|
NORMAL
|
2022-02-05T16:12:20.600613+00:00
|
2022-02-05T16:32:35.613894+00:00
| 104 | false |
Simple, but tons of corner cases. :/\nI send wrong solutions 3 times during contest... All in all -- love this problem :)\n\n```\nfunction minCostSetTime(\n startAt: number,\n moveCost: number,\n pushCost: number,\n targetSeconds: number,\n): number {\n const mins = Math.floor(targetSeconds / 60);\n const seconds = targetSeconds % 60;\n const options = [\n `${\n mins > 0 ? mins : \'\' // avoid \'0\'\n }${\n `${seconds}`.padStart(mins > 0 ? 2 : 0, \'0\') // avoid "12" for 1 minutes 2 seconds AND \'01\' for 0 minutes 1 seconds\n }`\n ];\n \n if (targetSeconds > 59 && (seconds + 60 <= 99)) { // remember: we can dial 1 minute 30 seconds as "160" OR "90" as long as seconds are below 99\n options.push(`${\n (mins - 1) > 0 ? mins - 1 : \'\' // once again avoid "0"\n }${\n seconds + 60 // no need to check here, since we know that number is double digits\n }`);\n }\n \n let minSum = Infinity;\n\n for (const option of options) {\n if (option.length > 4) continue; // will ensure that we only accept 100 minutes 00 seconds as 9960 :/\n let optionSum = 0;\n let current = `${startAt}`;\n for (let i = 0; i < option.length; i++) {\n const n = option[i];\n if (n !== current) {\n optionSum += moveCost;\n current = n;\n }\n optionSum += pushCost;\n }\n if (optionSum < minSum) {\n minSum = optionSum;\n }\n }\n return minSum;\n};\n```
| 1 | 0 |
['TypeScript', 'JavaScript']
| 0 |
minimum-cost-to-set-cooking-time
|
C# | Evaluate all possible mins and seconds combination for target
|
c-evaluate-all-possible-mins-and-seconds-lreq
|
\npublic class Solution {\n public int MinCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int mins = targetSeconds/60;\n
|
solankiurja3
|
NORMAL
|
2022-02-05T16:09:05.656198+00:00
|
2022-02-05T16:09:05.656224+00:00
| 69 | false |
```\npublic class Solution {\n public int MinCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int mins = targetSeconds/60;\n int sec = targetSeconds%60;\n int ans = int.MaxValue;\n if(mins>99){\n sec=targetSeconds-5940;\n mins=99;\n }\n \n while(sec<100 && mins>=0){\n ans = Math.Min(ans,Calculate(mins,sec,startAt,moveCost,pushCost));\n mins--;\n sec+=60;\n }\n return ans;\n \n }\n public int Calculate(int mins,int sec,int startAt, int moveCost, int pushCost){\n char prev = startAt.ToString()[0];\n string ssec = sec<10 && mins>0 ? "0"+sec : sec.ToString();\n string smins = mins.ToString();\n int ans=0;\n if(mins>0){\n for(int i=0;i<smins.Length;i++){\n if(prev!=smins[i]) ans+=moveCost;\n prev = smins[i];\n ans+=pushCost;\n }\n }\n for(int i=0;i<ssec.Length;i++){\n if(prev!=ssec[i]) ans+=moveCost;\n prev = ssec[i];\n ans+=pushCost;\n }\n return ans;\n }\n}\n```
| 1 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
C# - Just loop through each possible seconds
|
c-just-loop-through-each-possible-second-3xe4
|
```csharp\npublic class Solution \n{\n public int MinCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds)\n {\n int result = int
|
christris
|
NORMAL
|
2022-02-05T16:08:31.153068+00:00
|
2022-02-05T16:08:31.153093+00:00
| 59 | false |
```csharp\npublic class Solution \n{\n public int MinCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds)\n {\n int result = int.MaxValue;\n \n for (int i = 0; i <= 99 && i <= targetSeconds; i++)\n { \n if ((targetSeconds - i) % 60 == 0)\n {\n int m = ((targetSeconds - i) / 60) * 100 + i;\n\n int onM = m switch\n {\n < 10 => oneDigit(startAt, moveCost, pushCost, m),\n < 100 => twoDigit(startAt, moveCost, pushCost, m),\n < 1000 => threeDigit(startAt, moveCost, pushCost, m),\n < 10000 => fourDigit(startAt, moveCost, pushCost, m),\n _ => int.MaxValue\n };\n\n result = Math.Min(result, onM);\n }\n }\n \n return result;\n }\n\n private int oneDigit(int startAt, int moveCost, int pushCost, int num)\n {\n int digit = num % 10;\n return (startAt == digit ? 0 : moveCost) + pushCost;\n }\n\n private int twoDigit(int startAt, int moveCost, int pushCost, int num)\n {\n int first = num / 10;\n int second = num % 10;\n return (startAt == first ? 0 : moveCost) + (first == second ? 0 : moveCost) + 2 * pushCost;\n }\n\n private int threeDigit(int startAt, int moveCost, int pushCost, int num)\n {\n List<int> digits = new List<int>();\n while (num > 0)\n {\n digits.Add(num % 10);\n num /= 10;\n }\n\n int first = digits[^1];\n int second = digits[^2];\n int third = digits[^3];\n int current = (startAt == first ? 0 : moveCost) + (first == second ? 0 : moveCost)\n + (second == third ? 0 : moveCost) + 3 * pushCost;\n\n return current;\n }\n\n private int fourDigit(int startAt, int moveCost, int pushCost, int num)\n {\n List<int> digits = new List<int>();\n while (num > 0)\n {\n digits.Add(num % 10);\n num /= 10;\n }\n\n int first = digits[^1];\n int second = digits[^2];\n int third = digits[^3];\n int fourth = digits[^4];\n int current = (startAt == first ? 0 : moveCost) + (first == second ? 0 : moveCost)\n + (second == third ? 0 : moveCost) + (third == fourth ? 0 : moveCost)\n + 4 * pushCost;\n\n return current;\n }\n}\n````
| 1 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
Java Solution | Precompute everything | Short explanation
|
java-solution-precompute-everything-shor-bnx8
|
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
|
sandip_jana
|
NORMAL
|
2022-02-05T16:07:38.747624+00:00
|
2022-02-05T16:11:36.674346+00:00
| 77 | false |
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```
| 1 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
Java simple
|
java-simple-by-xavi_an-iz9o
|
```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int min = targetSeconds / 60;\n
|
xavi_an
|
NORMAL
|
2022-02-05T16:03:13.597116+00:00
|
2022-02-05T16:03:13.597159+00:00
| 74 | false |
```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int min = targetSeconds / 60;\n int sec = targetSeconds % 60;\n long out = Long.MAX_VALUE;\n if(min < 100)\n out = Math.min(out, getCost(startAt, moveCost, pushCost, min * 100 + sec)); \n if(sec < 40 && min > 0){\n out = Math.min(out, getCost(startAt, moveCost, pushCost, (min-1) *100 + (sec + 60)));\n }\n return (int)out;\n }\n \n private long getCost(int n, int moveCost, int pushCost, int x){\n long sum = 0;\n \tfor(int i=1;i<4;i++) {\n \t\tint d = (int)Math.pow(10, i);\n \t\tif(x / d == 0) {\n \t\t\tx %= d;\n \t\t}else {\n \t\t\tbreak;\n \t\t}\n \t}\n \tString s = String.valueOf(x);\n \tfor(char c : s.toCharArray()) {\n \t\tint e = c-\'0\';\n \t\tif(e != n) {\n \t\t\tsum += moveCost + pushCost;\n \t\t}else {\n \t\t\tsum += pushCost;\n \t\t}\n \t\tn = e;\n \t}\n return sum;\n }\n}
| 1 | 0 |
[]
| 0 |
minimum-cost-to-set-cooking-time
|
Simple Python Solution | Beats 100% and 78%
|
simple-python-solution-beats-100-and-78-acxd3
|
Code
|
imkprakash
|
NORMAL
|
2025-04-12T08:30:46.574295+00:00
|
2025-04-12T08:30:46.574295+00:00
| 1 | false |
# Code
```python3 []
class Solution:
def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:
target_min, target_sec = targetSeconds // 60, targetSeconds % 60
combinations = []
ans = float('inf')
while target_sec <= 99:
if target_min > 99:
target_sec += 60
target_min -= 1
continue
s = ''
if target_min:
s += str(target_min)
if target_min:
if target_sec < 10:
s += '0' + str(target_sec)
else:
s += str(target_sec)
else:
if target_sec:
s += str(target_sec)
combinations.append(s)
target_sec += 60
target_min -= 1
startAt = str(startAt)
for combination in combinations:
curr_pos = startAt
curr_cost = 0
for i in combination:
if i != curr_pos:
curr_cost += moveCost
curr_cost += pushCost
curr_pos = i
ans = min(ans, curr_cost)
return ans
```
| 0 | 0 |
['Math', 'Python3']
| 0 |
minimum-cost-to-set-cooking-time
|
Precompute ways
|
precompute-ways-by-theabbie-ig3i
| null |
theabbie
|
NORMAL
|
2025-04-10T05:22:40.817643+00:00
|
2025-04-10T05:22:40.817643+00:00
| 2 | false |
```python3 []
v = {}
for i in range(10):
for j in range(10):
for k in range(10):
for l in range(10):
m = 10 * i + j
s = 10 * k + l
sec = 60 * m + s
if sec not in v:
v[sec] = []
arr = [i, j, k, l]
while arr and arr[0] == 0:
arr.pop(0)
v[sec].append(arr)
class Solution:
def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:
res = float('inf')
for pos in v[targetSeconds]:
curr = 0
prev = startAt
for btn in pos:
if btn != prev:
curr += moveCost
curr += pushCost
prev = btn
res = min(res, curr)
return res
```
| 0 | 0 |
['Python3']
| 0 |
minimum-cost-to-set-cooking-time
|
Implementation
|
implementation-by-jihyuk3885-2kxp
|
Intuition10:00 <=> 09:60
mm:ss <=> mm-1:ss+60Try two cases for given targetSeconds.ApproachCosts:
moveCost : cost for changing number
pushCost: cost for fixing
|
jihyuk3885
|
NORMAL
|
2025-03-16T04:44:25.033994+00:00
|
2025-03-16T04:44:25.033994+00:00
| 6 | false |
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
10:00 <=> 09:60
mm:ss <=> mm-1:ss+60
Try two cases for given targetSeconds.
# Approach
<!-- Describe your approach to solving the problem. -->
Costs:
- moveCost : cost for changing number
- pushCost: cost for fixing number
Initial Conditions:
- startAt : previous digit
- targetSeconds : generate two targets (mm:ss, mm-1:ss+60)
1. transform targetSeconds to unit of minute and second
mm = targetSeconds / 60, ss = targetSeconds % 60
2. generate second target : if mm > 0 and ss < 40, then mm-1:ss+60
3. call a helper function that calculates cost of generated (mm:ss) targets.
- set prev_digit = startAt
- for every digits in `mm*100 + ss`,
if (prev_digit != curr_digit) addup moveCost
addup pushCost
- return sum of costs
4. return mimimum sum of costs.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(1)$$
takes const time for every calculation.
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$$O(1)$$
no additional heap spaces.
# Code
```cpp []
class Solution {
public:
int minCostSetTime(int startAt, int moveCost, int pushCost,
int targetSeconds) {
auto cost = [&](char prev_num, int m, int s) {
if (m < 0 || m > 99) return INT_MAX;
if (s < 0 || s > 99) return INT_MAX;
int res = 0;
for (char num : to_string(m * 100 + s)) {
res += (pushCost + (prev_num == num ? 0 : moveCost));
prev_num = num;
}
return res;
};
int m = targetSeconds / 60, s = targetSeconds % 60;
return min(cost(startAt + '0', m, s),
cost(startAt + '0', m - 1, s + 60));
}
};
```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
C++ ✅✅[ 0 ms Beats 100.00% ] 👍🏻 Easy Intuitive
|
c-0-ms-beats-10000-easy-intuitive-by-dhr-e9e5
|
IntuitionWe need to determine the minimum cost required to enter a given time on a digital clock by evaluating all possible valid representations of the time.Ap
|
dhruvsahni89
|
NORMAL
|
2025-03-09T18:53:20.593171+00:00
|
2025-03-09T18:53:20.593171+00:00
| 9 | false |
### Intuition
We need to determine the minimum cost required to enter a given time on a digital clock by evaluating all possible valid representations of the time.
### Approach
1. **Understanding the Time Format:**
- Each minute consists of 60 seconds.
- The clock can display up to **99 seconds** instead of transitioning to the next minute.
2. **Generating Possible Representations:**
- Compute the standard minute-second representation:
**minutes = target / 60
**
**seconds = target % 60**
- Consider alternative representations by adjusting the minute count downwards while ensuring the seconds value remains **≤ 99**.
- Example:
- `10:00` can also be represented as `09:60`
- `09:30` can be written as `08:90` (both equivalent to 570 seconds).
3. **Calculating the Input Effort (Score):**
- Start from `startAt` and track the previous digit.
- Increment the effort count only when transitioning to a different digit.
4. **Handling Edge Cases:**
- Ensure the final displayed time has exactly **four digits** by removing any leading zeros.
By evaluating all valid representations and calculating their input effort, we determine the one requiring the least cost.
# Code
```cpp []
class Solution {
public:
int fun(int startAt, int moveCost, int pushCost,int min,int sec){
vector<int>v;
string str=to_string(min);
for(auto c:str)
{
v.push_back(c-'0');
}
str=to_string(sec);
if(str.size()==1)v.push_back(0);
for(auto c:str)
{
v.push_back(c-'0');
}
int i=0;
while(v[i]==0){
i++;
}
vector<int>vv;
for(int j=i;j<v.size();j++)vv.push_back(v[j]);
v=vv;
int prev=startAt;
if(v.size()>4)return INT_MAX;
int cnt=0;
for(int i=0;i<v.size();i++){
if(v[i]==prev){
cnt+=pushCost;
}
else {
cnt+=pushCost;
cnt+=moveCost;
prev=v[i];
}
}
return cnt;
}
int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
int ans=INT_MAX;
int n=targetSeconds;
int minutes=n/60;
int sec=n-(minutes*60);
vector<int>v;
int cost=fun(startAt,moveCost,pushCost,minutes,sec);
ans=min(ans,cost);
while(minutes--){
int sec=n-(minutes*60);
if(sec<=99){
int cost=fun(startAt,moveCost,pushCost,minutes,sec);
ans=min(ans,cost);
}
else break;
}
return ans;
return 0;
}
};
```
| 0 | 0 |
['Simulation', 'C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Fastest 0 ms Solution
|
fastest-0-ms-solution-by-yogeshkothari-bvd9
|
Code
|
YogeshKothari
|
NORMAL
|
2025-01-24T13:49:07.289676+00:00
|
2025-01-24T13:49:07.289676+00:00
| 2 | false |
# Code
```java []
class Solution {
public int minCostSetTime(int start, int move, int push, int target) {
int min = target/60;
int sec = target%60;
int c1 = Integer.MAX_VALUE;
if(min < 100){
c1 = findCost(start, move, push, time(min, sec));
}
min--;
sec += 60;
if(min < 0 || sec > 99){
return c1;
}
int c2 = findCost(start, move, push, time(min, sec));
return Math.min(c1, c2);
}
int[] time(int min, int sec){
int[] t = new int[4];
t[0] = min/10;
t[1] = min%10;
t[2] = sec/10;
t[3] = sec%10;
return t;
}
public int findCost(int curr, int move, int push, int[] t) {
int cost = 0, i=0;
while(t[i] == 0){
i++;
}
while(i < 4){
if(t[i] != curr){
curr = t[i];
cost += move;
}
cost += push;
i++;
}
return cost;
}
}
```
| 0 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Fastest 0 ms Solution
|
fastest-0-ms-solution-by-yogeshkothari-x077
|
Code
|
YogeshKothari
|
NORMAL
|
2025-01-24T13:49:05.095861+00:00
|
2025-01-24T13:49:05.095861+00:00
| 5 | false |
# Code
```java []
class Solution {
public int minCostSetTime(int start, int move, int push, int target) {
int min = target/60;
int sec = target%60;
int c1 = Integer.MAX_VALUE;
if(min < 100){
c1 = findCost(start, move, push, time(min, sec));
}
min--;
sec += 60;
if(min < 0 || sec > 99){
return c1;
}
int c2 = findCost(start, move, push, time(min, sec));
return Math.min(c1, c2);
}
int[] time(int min, int sec){
int[] t = new int[4];
t[0] = min/10;
t[1] = min%10;
t[2] = sec/10;
t[3] = sec%10;
return t;
}
public int findCost(int curr, int move, int push, int[] t) {
int cost = 0, i=0;
while(t[i] == 0){
i++;
}
while(i < 4){
if(t[i] != curr){
curr = t[i];
cost += move;
}
cost += push;
i++;
}
return cost;
}
}
```
| 0 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
2162. Minimum Cost to Set Cooking Time
|
2162-minimum-cost-to-set-cooking-time-by-gbmw
|
IntuitionApproachComplexity
Time complexity:
Space complexity:
Code
|
G8xd0QPqTy
|
NORMAL
|
2025-01-18T04:06:48.926269+00:00
|
2025-01-18T04:06:48.926269+00:00
| 9 | false |
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int minCostSetTime(int initialPosition, int costToMove, int costToPush, int totalSeconds) {
vector<string> timeCombinations;
int minutesPart = totalSeconds / 60;
int secondsPart = totalSeconds % 60;
if (minutesPart < 100) {
timeCombinations.push_back(to_string(minutesPart * 100 + secondsPart));
}
if (secondsPart < 40 && minutesPart > 0) {
timeCombinations.push_back(to_string((minutesPart - 1) * 100 + secondsPart + 60));
}
int minimumExpense = INT_MAX;
for (const string& time : timeCombinations) {
int totalExpense = time.length() * costToPush;
char currentPosition = '0' + initialPosition;
for (char digit : time) {
if (digit != currentPosition) {
totalExpense += costToMove;
}
currentPosition = digit;
}
minimumExpense = min(minimumExpense, totalExpense);
}
return minimumExpense;
}
};
```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Generate all possible ways and calculate cost. Simple, Easy to understand. Beats 100%.
|
generate-all-possible-ways-and-calculate-ee8z
|
Code
|
Sketch42
|
NORMAL
|
2025-01-15T19:30:19.560413+00:00
|
2025-01-15T19:30:19.560413+00:00
| 7 | false |
# Code
```cpp []
class Solution {
public:
int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
vector<string> ways;
int minutes = targetSeconds / 60;
int seconds = targetSeconds % 60;
if (minutes < 100) ways.push_back(to_string(minutes*100 + seconds));
if (seconds < 40) ways.push_back(to_string((minutes-1)*100 + seconds + 60));
int minCost = INT_MAX;
for (string way : ways) {
int cost = way.length() * (pushCost + moveCost);
char currPos = '0' + startAt;
for (char ch : way) {
if (ch == currPos) cost -= moveCost;
currPos = ch;
}
minCost = min(minCost, cost);
}
return minCost;
}
};
```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Simple Java solution 0ms - 20 lines with explanation
|
simple-java-solution-0ms-20-lines-with-e-hlv8
|
Explanation3 important things to notice:
Only 2 options to create a given targetSeconds
minutes:seconds
minutes-1:seconds+60
There are some restrictions:
se
|
8BYpiMkeZ5
|
NORMAL
|
2025-01-08T19:28:08.952604+00:00
|
2025-01-08T19:28:08.952604+00:00
| 8 | false |
# Explanation
3 important things to notice:
- Only 2 options to create a given `targetSeconds`
1. `minutes`:`seconds`
2. `minutes-1`:`seconds+60`
- There are some restrictions:
- `seconds` is max. 99, hence if `seconds` part is more than 39 we cannot add 60, so we can only use option 1.
- `minutes` may be 0, then we cannot subtract 1, so we can only use option 1.
- `minutes` may be greater than 99 hence we have to add 60s, so we can only use option 2.
- If we have leading zeros we should skip them to minimize cost
# Code
```java []
class Solution {
private int startAt, moveCost, pushCost;
public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
this.startAt = startAt; this.moveCost = moveCost; this.pushCost = pushCost;
int m = targetSeconds / 60, s = targetSeconds % 60;
if (s >= 40 || m == 0) return calculateCost(m, s);
if (m > 99) return calculateCost(m - 1, s + 60);
return Math.min(calculateCost(m, s), calculateCost(m - 1, s + 60));
}
private int calculateCost(int m, int s) {
var digits = new int[] { m / 10, m % 10, s / 10, s % 10 };
int prev = startAt, cost = 0;
for (int digit : digits) {
if (digit == 0 && cost == 0) continue;
if (digit != prev) cost += moveCost;
cost += pushCost;
prev = digit;
}
return cost;
}
}
```
| 0 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Solution
|
solution-by-dovanan95-mt6e
|
IntuitionApproachComplexity
Time complexity:
Space complexity:
Code
|
dovanan95
|
NORMAL
|
2024-12-26T13:31:05.225416+00:00
|
2024-12-26T13:31:05.225416+00:00
| 3 | false |
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
#include <vector>
#include <algorithm>
class Solution {
public:
int findMin(vector<int>& inputs){
int minVal = inputs[0];
for(int i = 0; i< inputs.size(); i++){
if(inputs[i] < minVal){
minVal = inputs[i];
}
}
return minVal;
}
vector<vector<int>> secondToTimeArr(int& targetSecond){
vector<vector<int>> output = {};
vector<int> minsecConcat;
if(targetSecond < 10){
return {{targetSecond}};
}
if(targetSecond <= 99){
output.push_back({targetSecond/10, targetSecond%10});
if(targetSecond >= 60){
output.push_back({1, (targetSecond - 60)/10,(targetSecond - 60)%10});
}
}
int minuteCnt = targetSecond / 60;
int secondCnt = targetSecond % 60;
if(minuteCnt > 99){
minuteCnt -= 1;
secondCnt += 60;
}
vector<int> minuteArr = {minuteCnt/10, minuteCnt%10};
if(minuteArr[0] == 0){
minuteArr.erase(minuteArr.begin());
}
vector<int> secondArr = {secondCnt/10, secondCnt%10};
minsecConcat.insert(minsecConcat.end(), minuteArr.begin(), minuteArr.end());
minsecConcat.insert(minsecConcat.end(), secondArr.begin(), secondArr.end());
output.push_back(minsecConcat);
if(secondCnt == 0){
int newMin = minuteCnt - 1;
vector<int> newminconcat = {newMin/10, newMin%10, 6, 0};
if(newminconcat[0] == 0){
newminconcat.erase(newminconcat.begin());
}
output.push_back(newminconcat);
}
if(secondCnt + 60 <= 99){
int newMinCnt = minuteCnt - 1;
int newSecCnt = secondCnt + 60;
vector<int> newminsecConcat = {};
newminsecConcat = {newMinCnt/10, newMinCnt%10, newSecCnt/10, newSecCnt%10};
if(newminsecConcat[0] == 0){
newminsecConcat.erase(newminsecConcat.begin());
}
output.push_back(newminsecConcat);
}
if(secondCnt >= 60){
}
return output;
}
int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
vector<vector<int>> sectoTime = secondToTimeArr(targetSeconds);
vector<int> moveCostColl;
for(int i=0; i < sectoTime.size(); i++){
for(int j=0; j< sectoTime[i].size();j++){
cout << sectoTime[i][j];
}
cout << endl;
int mc = 0;
if(sectoTime[i].size() == 0){
moveCostColl.push_back(mc);
continue;
}
int itemCount = sectoTime[i].size();
if(sectoTime[i][0] == startAt){
if(sectoTime[i][0] != 0){
mc += pushCost;
}
}
else{
mc += (moveCost + pushCost);
}
int indx = 0;
for(int j = indx; j < itemCount - 1; j++){
if(sectoTime[i][j] == sectoTime[i][j + 1]){
mc += pushCost;
}
else{
mc += moveCost + pushCost;
}
cout << j <<" " << sectoTime[i][j] << " " << mc << endl;
}
moveCostColl.push_back(mc);
}
return findMin(moveCostColl);
}
};
```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Kotlin | O(1) solution
|
kotlin-o1-solution-by-aw_s-1gko
|
Intuition\n Describe your first thoughts on how to solve this problem. \nWe know that any targetSeconds value can be represented in a regular way, when seconds
|
aw_s
|
NORMAL
|
2024-10-23T06:13:27.460181+00:00
|
2024-10-23T06:13:27.460212+00:00
| 2 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe know that any `targetSeconds` value can be represented in a regular way, when seconds part has range from `0` to `59`:\n* `targetSeconds` = 159\n* `minutes` = 2; `seconds` = 39.\n\nBut in our case seconds have range from `0` to `99`, that creates another possibility (in some cases) to represent given `targetSeconds` value in extended format:\n* `targetSeconds` = 159\n* `minutes` = 1; `seconds` = 99.\n\nAfter that we just need to calculate which format in \'cheaper\' to calculate given `startAt`, `moveCost` and `pushCost` parameters.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Transform `targetSeconds` to a regular and extended (if possible) formats;\n2. Calculate costs;\n3. Return cheaper.\n\n# Complexity\n- Time complexity: $$O(1)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```kotlin []\nclass Solution {\n fun minCostSetTime(startAt: Int, moveCost: Int, pushCost: Int, targetSeconds: Int): Int {\n val variants = mutableListOf<String>()\n\n val timeInRegularFormat = targetSeconds.asRegularFormat()\n val costForTimeInRegularFormat = calculateCost(timeInRegularFormat, startAt, moveCost, pushCost)\n val costForTimeInExtendedFormat = targetSeconds.asExtendedFormat()?.let { time -> calculateCost(time, startAt, moveCost, pushCost) } ?: Int.MAX_VALUE\n\n return minOf(costForTimeInRegularFormat, costForTimeInExtendedFormat)\n }\n\n private fun Int.asRegularFormat(): String {\n val minutes = (this / 60).coerceAtMost(99)\n val seconds = this - minutes * 60\n\n val sb = StringBuilder()\n if (minutes > 0) {\n sb.append("$minutes")\n }\n if (seconds == 0) {\n sb.append("00")\n } else if (seconds < 10 && sb.isNotEmpty()) {\n sb.append("0")\n sb.append("$seconds")\n } else {\n sb.append("$seconds")\n }\n return sb.toString()\n }\n\n private fun Int.asExtendedFormat(): String? {\n val minutes = (this / 60).coerceAtMost(99) - 1\n if (minutes >= 0) {\n val seconds = this - minutes * 60\n if (seconds <= 99) {\n val sb = StringBuilder()\n if (minutes > 0) {\n sb.append("$minutes")\n }\n if (seconds == 0) {\n sb.append("00")\n } else if (seconds < 10 && sb.isNotEmpty()) {\n sb.append("0")\n sb.append("$seconds")\n } else {\n sb.append("$seconds")\n }\n return sb.toString()\n }\n }\n return null\n }\n\n private fun calculateCost(time: String, startAt: Int, moveCost: Int, pushCost: Int): Int {\n var currentDigit = startAt\n var result = 0\n (0..time.lastIndex).forEach { timeCharIdx ->\n val digit = time[timeCharIdx].digitToInt()\n if (digit == currentDigit) {\n result += pushCost\n } else {\n result += moveCost\n result += pushCost\n currentDigit = digit\n }\n }\n return result\n }\n\n}\n```
| 0 | 0 |
['Kotlin']
| 0 |
minimum-cost-to-set-cooking-time
|
Simple Two Combination Solution
|
simple-two-combination-solution-by-priya-lwvj
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
priyarathi7
|
NORMAL
|
2024-09-29T11:42:18.047268+00:00
|
2024-09-29T11:42:18.047290+00:00
| 1 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int minCost = Integer.MAX_VALUE;\n\n int minutes = targetSeconds / 60;\n int seconds = targetSeconds % 60;\n\n // Check (minutes, seconds) and (minutes - 1, seconds + 60)\n if (minutes < 100) {\n String timeStr = String.format("%02d%02d", minutes, seconds);\n minCost = Math.min(minCost, calculateCost(startAt, moveCost, pushCost, timeStr));\n }\n\n // Also check if we can represent the time as (minutes - 1, seconds + 60)\n if (minutes >= 0 && seconds + 60 < 100) {\n String timeStr = String.format("%02d%02d", minutes - 1, seconds + 60);\n minCost = Math.min(minCost, calculateCost(startAt, moveCost, pushCost, timeStr));\n }\n\n return minCost;\n }\n\n private int calculateCost(int startAt, int moveCost, int pushCost, String timeStr) {\n int totalCost = 0;\n int currentPos = startAt; \n \n int startIndex = 0;\n while (startIndex < timeStr.length() && timeStr.charAt(startIndex) == \'0\') {\n startIndex++;\n }\n\n for (int i = startIndex; i < timeStr.length(); i++) {\n int digit = timeStr.charAt(i) - \'0\';\n \n if (currentPos != digit) {\n totalCost += moveCost;\n currentPos = digit;\n }\n \n totalCost += pushCost;\n }\n \n return totalCost;\n }\n}\n```
| 0 | 0 |
['Math', 'Enumeration', 'Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Python 3: Slow and Ugly, But Quick and Easy
|
python-3-slow-and-ugly-but-quick-and-eas-5vbp
|
Intuition\n\nTime processing questions are irritating.\n\nSometimes you have irregular formats like a seconds field with 6,7,8,9 in it!\n\nAre you tired of wran
|
biggestchungus
|
NORMAL
|
2024-09-18T07:43:59.661535+00:00
|
2024-09-18T07:43:59.661566+00:00
| 6 | false |
# Intuition\n\nTime processing questions are irritating.\n\nSometimes you have irregular formats like a seconds field with 6,7,8,9 in it!\n\nAre you tired of wrangling with off-by-one errors? Base 60 conversions? Minutes and seconds got you down?\n\nThere\'s hope! Introducing **extreme brute force: try every possible digit combination edition!**\n\nIt\'s ugly and it\'s stupid. But it works. And if it works it\'s not stupid, right??\n\n## Brute Force: Try All Buttons\n\nIterate from 1 to [9999](https://youtu.be/SiMHTK15Pik?t=5).\n\nFor each, convert the first two digits to minutes and the second two to seconds. Then compute the duration `t` in seconds.\n\nIf `t == target` then\n* get the digits of `t`\n* each digit must be pressed so add `pushCost * len(digits)`\n* and each time we have to move our finger we add `moveCost`\n\nReturn the minimum cost seen this way.\n\n## Clever: m-1 s+60 Equivalence\n\nI picked this up from looking at other submissions, not sure who came up with it.\n\nThe idea is\n* we can enter the digits normally, the usual 0..59 seconds format\n* OR we can enter something larger than 59 for seconds and use the rollover.\n\n**We can compute the digits after rolling over, i.e. the "normalized time," with the usual `% 60` and `// 60` logic.**\n\nIf we did roll over, then **the equivalent state before rolling over is `m-1` and `s+60` for normalized minutes `m` and seconds `s`.**\n\nSo we can compute the cost of\n* entering the normalized `m` and `s`\n* entering the irregular `m-1` and `s+60` that is equivalent\n * if `s+60 >= 100` then we can\'t enter this, invalid combination\n * if `m-1 < 0` then similarly this is invalid\n * so we just have to check for these before computing the second way\n\n**This is the only rollover possible and thus we only have to compute costs for <= 2 digit strings instead of 9999.**\n\n# Time Problems in General\n\nMost of the time (heh) you can afford to spend `1e4` operations in an OA to get the answer. It\'s not pretty but it works, and often speed is of the essence. OAs are graded on tests passed then time taken then cleverness.\n\nIn-person interviews I think weight the cleverness a bit more, but still better to pass test cases and finish the question than wrangle with a clever idea the whole time.\n\nBut really I\'m just salty because I tried to come up with the clever solution but couldn\'t make it work within about 5 minutes so I gave up and went with brute force.\n\n# Complexity\n- Time complexity: `O(1e4)` try all digits\n\n- Space complexity: `O(1)`\n\n# Code\n```python3 []\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n # ahhhhhh yeah\n\n # only worth moving if we\'re going to push another button\n\n # clever ways: probably\n\n # OR: super brute force: generate all digit combinations, convert to time, if equal to targetSeconds then compute cost\n\n # max time is 6039\n # is that 59:99?\n # 60:39 yep!\n\n minCost = math.inf\n for n in range(1, 9999+1):\n s = n % 100\n m = n // 100\n t = 60*m + s\n if t == targetSeconds:\n digits = []\n while n:\n digits.append(n % 10)\n n //= 10\n\n cost = pushCost * len(digits)\n b = startAt\n for d in reversed(digits):\n if d != b:\n # move to d\n cost += moveCost\n b = d\n minCost = min(minCost, cost)\n\n return minCost\n```
| 0 | 0 |
['Python3']
| 0 |
minimum-cost-to-set-cooking-time
|
Simple Brute Force Solution O(1)
|
simple-brute-force-solution-o1-by-shreyk-0yvu
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
shreyk_23
|
NORMAL
|
2024-09-16T12:47:18.767784+00:00
|
2024-09-16T12:47:18.767807+00:00
| 7 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n O(1)\n\n- Space complexity:\n O(1).\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int mm = targetSeconds % 60, hh = targetSeconds / 60;\n int ans = INT_MAX;\n\n // First possibility: setting the time directly\n if (hh < 100) { // Only valid if hh is less than 100 (two digits max)\n int umm = mm / 10, lmm = mm % 10, uhh = hh / 10, lhh = hh % 10;\n int cost1 = 0;\n int currentPos = startAt;\n\n // Handle each digit to calculate cost\n if (uhh > 0) {\n if (uhh != currentPos) {\n cost1 += moveCost;\n currentPos = uhh;\n }\n cost1 += pushCost;\n }\n\n if (uhh > 0 || lhh > 0) {\n if (lhh != currentPos) {\n cost1 += moveCost;\n currentPos = lhh;\n }\n cost1 += pushCost;\n }\n\n if (uhh > 0 || lhh > 0 || umm > 0) {\n if (umm != currentPos) {\n cost1 += moveCost;\n currentPos = umm;\n }\n cost1 += pushCost;\n }\n\n if (uhh > 0 || lhh > 0 || umm > 0 || lmm > 0) {\n if (lmm != currentPos) {\n cost1 += moveCost;\n currentPos = lmm;\n }\n cost1 += pushCost;\n }\n\n ans = cost1;\n }\n\n // Second possibility: adjusting time (subtract 1 minute from hh and add 60 to mm)\n if (hh > 0 && mm + 60 < 100) {\n hh -= 1;\n mm += 60;\n int umm = mm / 10, lmm = mm % 10, uhh = hh / 10, lhh = hh % 10;\n int cost2 = 0;\n int currentPos = startAt;\n\n // Handle each digit to calculate cost\n if (uhh > 0) {\n if (uhh != currentPos) {\n cost2 += moveCost;\n currentPos = uhh;\n }\n cost2 += pushCost;\n }\n\n if (uhh > 0 || lhh > 0) {\n if (lhh != currentPos) {\n cost2 += moveCost;\n currentPos = lhh;\n }\n cost2 += pushCost;\n }\n\n if (uhh > 0 || lhh > 0 || umm > 0) {\n if (umm != currentPos) {\n cost2 += moveCost;\n currentPos = umm;\n }\n cost2 += pushCost;\n }\n\n if (uhh > 0 || lhh > 0 || umm > 0 || lmm > 0) {\n if (lmm != currentPos) {\n cost2 += moveCost;\n currentPos = lmm;\n }\n cost2 += pushCost;\n }\n\n ans = std::min(ans, cost2);\n }\n\n return ans;\n }\n};\n\n```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Easy to understand Solution
|
easy-to-understand-solution-by-jatin56-qmd4
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n1. We will firstly find
|
jatin56
|
NORMAL
|
2024-09-16T09:11:45.648683+00:00
|
2024-09-16T09:11:45.648713+00:00
| 3 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. We will firstly find all the possible ways or combinations.\n2. Secondly we will find the minCost by traversing over each combination.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n vector<string> allPossible(int targetSeconds) {\n vector<string> possibles;\n for (int min = 0; min <= 99; min++) {\n int targetSum = targetSeconds;\n int sec = 0;\n targetSum -= (min * 60);\n if(targetSum < 0) break ;\n if (targetSum >= 0 && targetSum <= 99) {\n sec = targetSum;\n targetSum -= sec;\n }\n if (targetSum == 0) {\n string a = to_string(min);\n string b = sec > 9 ? to_string(sec) : "0" + to_string(sec);\n possibles.push_back(a + b);\n }\n }\n return possibles;\n }\n int minCostSetTime(int startAt, int moveCost, int pushCost,\n int targetSeconds) {\n vector<string> ans = allPossible(targetSeconds);\n int minCost = INT_MAX;\n for (auto s : ans) {\n int i = 0;\n int cost = 0;\n // remove the leading zero\n int num = stoi(s);\n s = to_string(num);\n while(i < s.length()){\n int n = s[i] - \'0\';\n // check whether it matches with the startAt \n if(n == startAt && i == 0){\n if(n != 0){\n // push it \n cost += pushCost;\n }\n // move forwar \n if(i + 1 < s.length() && s[i] != s[i + 1]) cost += moveCost;\n i++;\n }else{\n if(i == 0){\n // i have to move\n cost += moveCost;\n }\n cost += pushCost;\n if(i + 1< s.length() && s[i] != s[i + 1]) cost += moveCost;\n i++;\n }\n }\n minCost = min(cost, minCost);\n }\n return minCost;\n }\n};\n```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Simple | O(1) both Time and Space Complexity
|
simple-o1-both-time-and-space-complexity-04og
|
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
|
RaghavAgarwal15
|
NORMAL
|
2024-09-16T07:54:41.179143+00:00
|
2024-09-16T07:54:41.179168+00:00
| 8 | false |
# 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```
| 0 | 0 |
['Math', 'Enumeration', 'C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Brute Force ans Easy to Understand || 0 ms || Beats 100.00%
|
brute-force-ans-easy-to-understand-0-ms-hdmra
|
Intuition\n Describe your first thoughts on how to solve this problem. \nOnly 4 digits (1 ~ 9999)\n\n# Approach\n Describe your approach to solving the problem.
|
JeTKuo
|
NORMAL
|
2024-09-05T19:08:16.918371+00:00
|
2024-09-05T19:08:16.918392+00:00
| 0 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOnly 4 digits (1 ~ 9999)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nGenerate all possible ways for 4 digits (1 ~ 9999), and check the total seconds for each way, if same as targetSeconds, caculate the cost and keep the minimum one.\n\n# Complexity\n- Time complexity: O(9999)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\n int startAt, moveCost, pushCost;\n int costTime(string s) {\n int ans = 0;\n int N = s.size();\n for (int i = 0 ; i < N ; i++) {\n if (i == 0) {\n if ((s[i]-\'0\') != startAt) ans += moveCost;\n }\n else {\n if (s[i] != s[i-1]) ans += moveCost;\n }\n ans += pushCost;\n }\n return ans;\n }\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n this->startAt = startAt;\n this->moveCost = moveCost;\n this->pushCost = pushCost;\n int ans = INT_MAX;\n for (int i = 1 ; i <= 9999 ; i++) {\n if (((i/100*60) + (i%100)) == targetSeconds) {\n // printf("Check %d\\n", i);\n ans = min(ans, costTime(to_string(i)));\n }\n }\n return ans;\n }\n};\n```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Simple, easy to understand. And with Explanation
|
simple-easy-to-understand-and-with-expla-dlni
|
Intuition\n Describe your first thoughts on how to solve this problem. \nI reference from Uncle_John\'s code. He made a great code but didn\'t made any comment.
|
Lyon_codegeek
|
NORMAL
|
2024-08-19T11:22:03.295948+00:00
|
2024-08-19T12:40:47.276004+00:00
| 4 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI reference from Uncle_John\'s code. He made a great code but didn\'t made any comment.\nLet me make comment here.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst, we have convert targetSeconds into mins and secs, and beware once min > 100, it have to force +60 secs due to its\' check program.\nFrom here,there are 2 types of input in this question\n1. With minutes, and seconds >= 40\n2. With minutes, but sceonds < 40, which means we can minutes sub 1, and move 1 minutes to secs. Ex: 3:39 = 2:99, 1:20 = 80(sec)\nRest of things is value conver to_string. Checck if minute present, and check if second below 10 needs add char 0 befoe it.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int ToCheckCost(int start, int mc, int pc, string ts){\n int res = 0;\n if (ts.length() == 0) return INT_MAX;\n for(char aa : ts){\n if (aa - \'0\' != start){\n res += mc + pc;\n start = aa - \'0\';\n }\n else{\n res += pc;\n }\n }\n return res;\n }\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int min = targetSeconds / 60;\n int sec = targetSeconds % 60;\n if (min >= 100){\n min -= 1;\n sec += 60;\n }\n string str1, str2;\n //str1\n if (min){\n if (sec >= 10){\n str1 = to_string(min) + to_string(sec);\n }else{\n str1 = to_string(min) + "0" + to_string(sec);\n }\n }else{\n str1 = to_string(sec);\n }\n //str2\n if ((min) && (sec < 40))\n {\n if (min == 1){\n str2 = to_string(sec + 60);\n }else{\n str2 = to_string(min-1) + to_string(sec+60);\n }\n }\n //\n int r1 = ToCheckCost(startAt, moveCost, pushCost, str1);\n int r2 = ToCheckCost(startAt, moveCost, pushCost, str2);\n return std::min(r1, r2);\n }\n};\n```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
2162. Minimum Cost to Set Cooking Time.cpp
|
2162-minimum-cost-to-set-cooking-timecpp-wzmu
|
Code\n\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int minute = targetSeconds/60,
|
202021ganesh
|
NORMAL
|
2024-07-24T11:40:07.915284+00:00
|
2024-07-24T11:40:07.915322+00:00
| 0 | false |
**Code**\n```\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int minute = targetSeconds/60, second = targetSeconds%60; \n int ans = INT_MAX; \n for (auto& [m, s] : vector<pair<int, int>>{{minute, second}, {minute-1, second+60}}) {\n if (0 <= m && m < 100 && s < 100) {\n int cost = 0, prev = startAt; \n bool found = false; \n for (auto& x : {m/10, m%10, s/10, s%10}) \n if (x || found) {\n if (x != prev) {cost += moveCost; prev = x;}\n cost += pushCost; \n found = true; \n }\n ans = min(ans, cost); \n }\n }\n return ans; \n }\n};\n```
| 0 | 0 |
['C']
| 0 |
minimum-cost-to-set-cooking-time
|
Java solution
|
java-solution-by-vharshal1994-g0r6
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
vharshal1994
|
NORMAL
|
2024-06-07T14:47:40.745919+00:00
|
2024-08-11T09:30:02.281276+00:00
| 3 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n /**\n Idea is to first define helper fn that calculates total cost to set time in mm:ss.\n This would be of the form mm * 100 + ss.\n Then we need to find out all valid mm:ss for targetSeconds.\n This we can do by iterating mm from 0 to targetSeconds/60. Leftover would be ss.\n */\n \n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n List<int[]> possibleMMSS = possibleMMSS(targetSeconds);\n long minCost = Long.MAX_VALUE;\n for (int[] mmss : possibleMMSS) {\n int mm = mmss[0];\n int ss = mmss[1];\n minCost = Math.min(minCost, findCost(mm, ss, startAt, moveCost, pushCost));\n }\n return (int) minCost;\n }\n\n private List<int[]> possibleMMSS(int targetSeconds) {\n List<int[]> possibleMMSS = new ArrayList<>();\n for (int minute = 0; minute <= 99; minute++) {\n int seconds = targetSeconds - (minute * 60);\n if (seconds > 99) {\n continue;\n }\n if (seconds < 0) {\n break;\n }\n possibleMMSS.add(new int[]{minute, seconds});\n }\n return possibleMMSS;\n }\n\n private long findCost(int mm, int ss, int startAt, int moveCost, int pushCost) {\n String time = String.valueOf(mm * 100 + ss);\n\n long totalCost = 0;\n char startAtCh = (char) (startAt + \'0\');\n for (int i = 0; i < time.length(); i++) {\n char ch = time.charAt(i);\n\n if (ch != startAtCh) {\n totalCost += moveCost;\n startAtCh = ch;\n }\n\n totalCost += pushCost;\n }\n\n return totalCost;\n }\n}\n```
| 0 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Simple easy to understand code
|
simple-easy-to-understand-code-by-n00bs4-6mc3
|
Intuition\nwell it took me a lot of time to get this solution right, although it is not that complex but initially i was thinking we can move to any digit with
|
n00bs404
|
NORMAL
|
2024-06-01T04:40:00.798834+00:00
|
2024-06-01T04:40:00.798858+00:00
| 1 | false |
# Intuition\nwell it took me a lot of time to get this solution right, although it is not that complex but initially i was thinking we can move to any digit with move cost and push the button, that itself looked hard. But after reading it again it was easy, sicne we are starting from 0 and pushing the next digit only\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n //target representation\n int ans = Integer.MAX_VALUE;\n for(int x: getNumbers(targetSeconds)){\n List<Integer> list = new LinkedList();\n while(x!=0){\n list.add(x%10);\n x=x/10;\n }\n ans = Math.min(ans, cost(list, startAt, pushCost, moveCost));\n }\n return ans;\n }\n\n public int cost(List<Integer> list, int startAt, int pushCost, int moveCost){\n int cost = 0;\n for(int i=list.size()-1;i>=0;i--){\n if(startAt == list.get(i)){\n cost += pushCost;\n } else {\n cost += moveCost + pushCost;\n startAt = list.get(i);\n }\n }\n return cost;\n }\n\n public List<Integer> getNumbers(int x){\n List<Integer> list = new LinkedList<>();\n for(int i=0;i<100;i++){\n if(x-i >= 0 && (x-i)%60 == 0 && (x-i)/60 < 100) {\n list.add(((x-i)/60)*100 + i);\n if(list.get(0) == 0){\n list.removeFirst();\n }\n if(list.get(0) == 0){\n list.removeFirst();\n }\n }\n }\n return list;\n }\n}\n```
| 0 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
0ms Java
|
0ms-java-by-kumy24-g1fi
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
kumy24
|
NORMAL
|
2024-05-28T15:55:21.586468+00:00
|
2024-05-28T15:55:21.586501+00:00
| 0 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(targetSeconds / 60)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n int minCost = 1000000;\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int mins = targetSeconds / 60; \n int sec = targetSeconds % 60;\n \n while (sec <= 99 && mins >= 0){\n if(mins <= 99) { \n int c = cost(mins, sec, pushCost, moveCost, startAt);\n minCost = Math.min(minCost, c);}\n mins--;\n sec+=60;\n } \n\n return minCost;\n }\n public int cost(int min, int sec, int pushCost, int moveCost, int startAt) {\n int[] a = new int[]{min/10, min%10, sec / 10, sec % 10};\n int prev = startAt;\n int i = 0, push = 0, move = 0;\n while(a[i] == 0) i++;\n while(i < 4) {\n if(a[i] != prev) {\n move++;\n prev = a[i];\n }\n push++;\n i++;\n }\n return (push * pushCost) + (move * moveCost);\n }\n}\n```
| 0 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Intuitive Java Solution
|
intuitive-java-solution-by-gauravkabra-vcr5
|
Intuition \nThis is basically a simulation problem. It does not involve any DSA - just keeping in mind constraints and knowing string iteration should work.\n\n
|
gauravkabra
|
NORMAL
|
2024-04-23T10:32:12.946751+00:00
|
2024-04-23T10:32:12.946785+00:00
| 1 | false |
# Intuition \nThis is basically a simulation problem. It does not involve any DSA - just keeping in mind constraints and knowing string iteration should work.\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1) externally, O(N) for internal stack\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int mins = targetSeconds/60;\n int secs = targetSeconds % 60;\n\n while (mins > 99) {\n mins--;\n secs += 60;\n }\n\n int min = Integer.MAX_VALUE;\n while (secs <= 99) {\n String time = Integer.toString(mins) + String.format("%02d", secs);\n time = time.replaceAll("^0+(?!$)", "");\n min = Math.min(\n min,\n helper(startAt, moveCost, pushCost, time, 0, time.length(), 0)\n );\n mins--;\n secs += 60;\n }\n return min;\n }\n\n private int helper(int ch, int move, int push, String time, int curr, int N, int cost) {\n if (curr >= N) {\n return cost;\n }\n\n int digit = time.charAt(curr)-\'0\';\n if (ch == digit) {\n return helper(ch, move, push, time, curr+1, N, cost+push);\n } else {\n return helper(digit, move, push, time, curr+1, N, cost+move+push);\n }\n }\n}\n```
| 0 | 0 |
['Simulation', 'Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Simple function-based solution
|
simple-function-based-solution-by-anitha-ksva
|
Intuition\nmin = targetSeconds/60;\nsec = targetSeconds%60;\nThere can be only 2 possible ways\neither answer will be min60 + sec = target\nor (min-1)60 + sec +
|
anitha-chiluvuri
|
NORMAL
|
2024-04-14T03:34:47.173753+00:00
|
2024-04-14T03:41:58.336056+00:00
| 14 | false |
# Intuition\nmin = targetSeconds/60;\nsec = targetSeconds%60;\nThere can be only 2 possible ways\neither answer will be min*60 + sec = target\nor (min-1)*60 + sec + 60 = target\n\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n // To find correct time format of 4 digit\n // int min, int sec => mmss format\n string toTime(int t) {\n string timeStr = "";\n if(t < 10) {\n timeStr += \'0\';\n timeStr += to_string(t);\n } else {\n timeStr = to_string(t/10) + to_string(t%10);\n }\n return timeStr;\n }\n\n int cost(int m, int s, int startAt, int moveCost, int pushCost) {\n int cost = 0, i = 0;\n string time = toTime(m) + toTime(s);\n // Avoiding leading zeros as we don\'t need to push it\n while(time[i] == \'0\' && i < 4)i++;\n for(; i < time.size(); i++){\n int digit = time[i] - \'0\';\n if(digit != startAt) {\n startAt = digit;\n cost += (moveCost + pushCost);\n } else {\n cost += pushCost;\n }\n }\n return cost;\n }\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int m = targetSeconds/60;\n int s = targetSeconds%60;\n int minCost = INT_MAX;\n // There can be only 2 possible ways\n // either answer will be m*60 + sec = target\n // or (m-1)*60 + sec + 60 = target\n if(m <= 99)\n minCost = cost(m, s, startAt, moveCost, pushCost);\n if(s + 60 < 100) {\n minCost = min(minCost, cost(m-1, s + 60, startAt, moveCost, pushCost));\n }\n return minCost;\n }\n};\n```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
JS
|
js-by-manu-bharadwaj-bn-ky0r
|
Code\n\nvar minCostSetTime = function (startAt, moveCost, pushCost, targetSeconds) {\n let min = Math.trunc(targetSeconds / 60)\n let sec = targetSeconds
|
Manu-Bharadwaj-BN
|
NORMAL
|
2024-04-02T14:16:59.509172+00:00
|
2024-04-02T14:16:59.509269+00:00
| 16 | false |
# Code\n```\nvar minCostSetTime = function (startAt, moveCost, pushCost, targetSeconds) {\n let min = Math.trunc(targetSeconds / 60)\n let sec = targetSeconds % 60\n let minRes = Infinity\n if (min <= 99) check(min, sec)\n if (min > 0 && sec <= 39 && min <= 100) check(min - 1, sec + 60)\n return minRes\n\n function check(min, sec) {\n let str = (min === 0 ? "" : String(min)) + (min === 0 ? String(sec) : String(sec).padStart(2, \'0\'))\n let currentRes = 0\n let newStart = startAt\n for (let i = 0; i < str.length; i++) {\n if (newStart != str[i]) {\n newStart = +str[i]\n currentRes += moveCost\n }\n currentRes += pushCost\n }\n minRes = Math.min(currentRes, minRes)\n }\n};\n```
| 0 | 0 |
['JavaScript']
| 1 |
minimum-cost-to-set-cooking-time
|
Minimum Cost to Set Timer at Target Time
|
minimum-cost-to-set-timer-at-target-time-p289
|
Intuition\nThe problem appears to involve determining the minimum cost required to set a timer to reach a target time, considering the cost of moving the timer\
|
kumarritul089
|
NORMAL
|
2024-03-16T05:24:33.181965+00:00
|
2024-03-16T05:24:33.181996+00:00
| 11 | false |
# Intuition\nThe problem appears to involve determining the minimum cost required to set a timer to reach a target time, considering the cost of moving the timer\'s digits and pushing the timer\'s button.\n\n# Approach\n1. Initialize a vector to store the possible representations of the target time in terms of hours and minutes.\n2. If the target time is less than 100 seconds, convert it to a string and store it directly in the vector.\n3. If the target time is greater than or equal to 100 seconds, convert it to hours and minutes. Store both representations in the vector.\n4. Calculate the minimum cost for each representation by iterating through the vector:\n - Initialize the cost and current time.\n - Iterate through each digit of the representation:\n - If the digit is different from the current digit of the timer, add the move cost and update the current time.\n - Add the push cost for pressing the timer button.\n - Update the minimum cost found so far.\n5. Return the minimum cost.\n\n# Complexity\n- Time complexity: O(n), where n is the number of representations of the target time in the vector. Each representation requires iterating through its digits.\n- Space complexity: O(n), where n is the number of representations of the target time stored in the vector.\n# Code\n``` C++ []\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int target) {\n vector<string> v;\n if(target < 100){\n v.push_back(to_string(target));\n int m = target / 60, sc = target % 60;\n string ssc = "";\n if(sc < 10){\n ssc += \'0\';\n ssc += to_string(sc);\n }else{\n ssc = to_string(sc);\n }\n v.push_back(to_string(m)+ssc);\n }else{\n int m = target / 60, sc = target % 60;\n if(m < 100){\n string ssc = "";\n if(sc < 10){\n ssc += \'0\';\n ssc += to_string(sc);\n }else{\n ssc = to_string(sc);\n }\n v.push_back(to_string(m)+ssc);\n }\n if(60 + sc < 100){\n v.push_back(to_string(m-1)+ to_string(sc+60));\n }\n }\n int res = INT_MAX;\n for(int i=0; i<v.size(); i++){\n int cost = 0, cur = startAt;\n for(int j=0; j<v[i].size(); j++){\n if(v[i][j]-\'0\' != cur){\n cost += moveCost;\n cur = v[i][j]-\'0\';\n }\n cost += pushCost;\n }\n res = min(res, cost);\n }\n return res;\n }\n};\n```\n```javascript []\nfunction minCostSetTime(startAt, moveCost, pushCost, target) {\n let v = [];\n if (target < 100) {\n v.push(target.toString());\n let m = Math.floor(target / 60);\n let sc = target % 60;\n let ssc = "";\n if (sc < 10) {\n ssc += \'0\';\n ssc += sc.toString();\n } else {\n ssc = sc.toString();\n }\n v.push(m.toString() + ssc);\n } else {\n let m = Math.floor(target / 60);\n let sc = target % 60;\n if (m < 100) {\n let ssc = "";\n if (sc < 10) {\n ssc += \'0\';\n ssc += sc.toString();\n } else {\n ssc = sc.toString();\n }\n v.push(m.toString() + ssc);\n }\n if (60 + sc < 100) {\n v.push((m - 1).toString() + (sc + 60).toString());\n }\n }\n let res = Number.MAX_SAFE_INTEGER;\n for (let i = 0; i < v.length; i++) {\n let cost = 0;\n let cur = startAt;\n for (let j = 0; j < v[i].length; j++) {\n if (parseInt(v[i][j]) !== cur) {\n cost += moveCost;\n cur = parseInt(v[i][j]);\n }\n cost += pushCost;\n }\n res = Math.min(res, cost);\n }\n return res;\n}\n\n```\n]\n
| 0 | 0 |
['Math', 'Enumeration', 'C++', 'JavaScript']
| 0 |
minimum-cost-to-set-cooking-time
|
Beats 100%
|
beats-100-by-dev_lazy25-oh9e
|
Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\n fun minCostSetTime(\n startAt: Int,\n moveCos
|
dev_lazy25
|
NORMAL
|
2024-03-09T15:14:40.290936+00:00
|
2024-03-09T15:14:40.290963+00:00
| 4 | false |
# Complexity\n- Time complexity:\n$$O(1)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution {\n fun minCostSetTime(\n startAt: Int,\n moveCost: Int,\n pushCost: Int,\n targetSeconds: Int\n ): Int {\n val minutes = targetSeconds / 60\n val seconds = targetSeconds % 60\n\n return min(\n calculateMinCost(minutes, seconds, startAt, moveCost, pushCost),\n calculateMinCost(minutes - 1, seconds + 60, startAt, moveCost, pushCost)\n )\n }\n\n private fun calculateMinCost(\n minutes: Int,\n seconds: Int,\n startAt: Int,\n moveCost: Int,\n pushCost: Int,\n ) : Int {\n var prevDigit = startAt\n if(minutes !in 0..99 || seconds !in 0..99) return Int.MAX_VALUE\n\n val digit = intArrayOf(minutes / 10, minutes % 10, seconds / 10, seconds % 10)\n\n var index = 0\n while(index < 4 && digit[index] == 0) {\n index++\n }\n\n var totalCost = 0\n\n while(index < 4) {\n if(digit[index] != prevDigit) {\n totalCost += moveCost\n }\n totalCost += pushCost\n\n prevDigit = digit[index]\n index++\n }\n return totalCost\n }\n}\n```
| 0 | 0 |
['Array', 'Math', 'Enumeration', 'Kotlin']
| 0 |
minimum-cost-to-set-cooking-time
|
👍Runtime 473 ms Beats 100.00% of users with Scala
|
runtime-473-ms-beats-10000-of-users-with-6r5c
|
Code\n\nobject Solution {\n def minCostSetTime(startAt: Int, moveCost: Int, pushCost: Int, targetSeconds: Int): Int = {\n val mins = targetSeconds / 6
|
pvt2024
|
NORMAL
|
2024-03-02T00:20:14.378825+00:00
|
2024-03-02T00:20:14.378854+00:00
| 4 | false |
# Code\n```\nobject Solution {\n def minCostSetTime(startAt: Int, moveCost: Int, pushCost: Int, targetSeconds: Int): Int = {\n val mins = targetSeconds / 60\n val secs = targetSeconds % 60\n val firstOption = cost(mins, secs, startAt, moveCost, pushCost)\n val secondOption = cost(mins - 1, secs + 60, startAt, moveCost, pushCost)\n Math.min(firstOption, secondOption)\n }\n\n private def cost(mins: Int, secs: Int, startAt: Int, moveCost: Int, pushCost: Int): Int = {\n if (mins > 99 || secs > 99 || mins < 0 || secs < 0) return Int.MaxValue\n val s = (mins * 100 + secs).toString\n var curr = (startAt + \'0\').toChar\n var res = 0\n for (i <- 0 until s.length) {\n if (s.charAt(i) == curr) res += pushCost\n else {\n res += pushCost + moveCost\n curr = s.charAt(i)\n }\n }\n res\n }\n}\n```
| 0 | 0 |
['Scala']
| 0 |
minimum-cost-to-set-cooking-time
|
Rust, O(1)
|
rust-o1-by-kichooo-k1ne
|
Code\n\nimpl Solution {\n pub fn min_cost_set_time(\n start_at: i32,\n move_cost: i32,\n push_cost: i32,\n target_seconds: i32,\n
|
kichooo
|
NORMAL
|
2024-02-07T00:50:39.957557+00:00
|
2024-02-07T00:50:39.957577+00:00
| 8 | false |
# Code\n```\nimpl Solution {\n pub fn min_cost_set_time(\n start_at: i32,\n move_cost: i32,\n push_cost: i32,\n target_seconds: i32,\n ) -> i32 {\n fn cost(start_at: i32, move_cost: i32, push_cost: i32, minutes: i32, seconds: i32) -> i32 {\n let mut number = minutes * 100 + seconds;\n let mut cost = 0;\n let mut current_digit = number % 10;\n while number > 0 {\n cost += push_cost;\n if current_digit != number % 10 {\n cost += move_cost;\n current_digit = number % 10;\n }\n number /= 10;\n }\n\n if current_digit != start_at {\n cost += move_cost;\n }\n cost\n }\n\n let minutes = target_seconds / 60;\n let seconds = target_seconds % 60; \n \n if minutes >= 100 {\n return cost(start_at, move_cost, push_cost, minutes - 1, seconds + 60)\n }\n\n let mut best_cost = cost(start_at, move_cost, push_cost, minutes, seconds);\n if minutes > 0 && seconds <= 39 {\n cost(start_at, move_cost, push_cost, minutes - 1, seconds + 60).min(best_cost)\n } else {\n best_cost\n }\n }\n}\n\n```
| 0 | 0 |
['Rust']
| 0 |
minimum-cost-to-set-cooking-time
|
Java Modular Code || Easy to Understand
|
java-modular-code-easy-to-understand-by-qmjwn
|
Code\n\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n List<Integer> times = buildTime(t
|
abhishekkr1706
|
NORMAL
|
2024-01-24T19:15:32.195955+00:00
|
2024-01-24T19:15:32.195990+00:00
| 2 | false |
# Code\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n List<Integer> times = buildTime(targetSeconds);\n int minCost = Integer.MAX_VALUE;\n for (int time: times) {\n int cost = 0;\n int localStart = startAt;\n int divisor = (int) Math.pow(10, getNoOfDigits(time)-1);\n while (divisor > 0) {\n int digit = time / divisor;\n if (digit != localStart) {\n cost += moveCost;\n }\n cost += pushCost;\n localStart = digit;\n time %= divisor;\n divisor /= 10;\n }\n minCost = Math.min(minCost, cost);\n }\n return minCost;\n }\n\n private int getNoOfDigits(int num) {\n int count = 0;\n while (num > 0) {\n num /= 10;\n count++;\n }\n return count;\n }\n\n private List<Integer> buildTime(int targetSeconds) {\n int min = targetSeconds / 60;\n int sec = targetSeconds % 60;\n List<Integer> list = new ArrayList<>();\n if (min < 100) {\n if (min > 0) list.add(min * 100 + sec);\n else list.add(sec);\n }\n if (min > 0 && sec+60 < 100) {\n min -= 1; \n sec += 60;\n if (min > 0) list.add(min * 100 + sec);\n else list.add(sec);\n }\n return list;\n }\n}\n```
| 0 | 0 |
['Java']
| 0 |
minimum-cost-to-set-cooking-time
|
Constant time and space (atmost 100 iterations)
|
constant-time-and-space-atmost-100-itera-1jx9
|
\n# Code\n\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n min_cost = float(\'in
|
svittal
|
NORMAL
|
2023-11-26T23:24:25.662024+00:00
|
2023-11-26T23:24:25.662050+00:00
| 10 | false |
\n# Code\n```\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n min_cost = float(\'inf\')\n \n def calculateCost(time_str, startAt, moveCost, pushCost):\n cost = 0\n for digit in time_str:\n if digit != \'0\' or cost > 0:\n cost += moveCost if startAt != int(digit) else 0\n cost += pushCost\n startAt = int(digit)\n return cost\n \n for minn in range(100):\n sec = targetSeconds - minn * 60\n if 0 <= sec < 100:\n time_str = f\'{minn:02d}{sec:02d}\'\n cost = calculateCost(time_str, startAt, moveCost, pushCost)\n min_cost = min(min_cost, cost)\n\n return min_cost\n\n```
| 0 | 0 |
['Python3']
| 0 |
minimum-cost-to-set-cooking-time
|
C++ solution
|
c-solution-by-pejmantheory-cxqb
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
pejmantheory
|
NORMAL
|
2023-09-02T20:36:29.634176+00:00
|
2023-09-02T20:36:29.634203+00:00
| 19 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public:\n int minCostSetTime(int startAt, int moveCost, int pushCost,\n int targetSeconds) {\n int ans = INT_MAX;\n int mins = targetSeconds > 5999 ? 99 : targetSeconds / 60;\n int secs = targetSeconds - mins * 60;\n\n auto getCost = [&](int mins, int secs) -> int {\n int cost = 0;\n char curr = \'0\' + startAt;\n for (const char c : to_string(mins * 100 + secs))\n if (c == curr) {\n cost += pushCost;\n } else {\n cost += moveCost + pushCost;\n curr = c;\n }\n return cost;\n };\n\n while (secs < 100) {\n ans = min(ans, getCost(mins, secs));\n --mins;\n secs += 60;\n }\n\n return ans;\n }\n};\n\n```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
Simple c++ with explanation | faster than 100% | TC: O(1), SC: O(1)
|
simple-c-with-explanation-faster-than-10-q6jh
|
Intuition\nAfter looking at few examples it can be easily seen that there will be broadly two possible cases. And we will calculate cost of these two cases and
|
baymax16
|
NORMAL
|
2023-08-28T13:44:06.348498+00:00
|
2023-08-28T13:44:06.348530+00:00
| 20 | false |
# Intuition\nAfter looking at few examples it can be easily seen that there will be broadly two possible cases. And we will calculate cost of these two cases and find the minimum of the two. \n\n1. Split our seconds to `mm:ss` -> Do this in a strict way \n2. If we can add few more seconds to `:ss` part \n\nFor 1st case we just need to calculate how many minutes are there in targetSeconds, and what is the remaining number of seconds. (This is our case-1, but we also need to make sure that minutes does not exceed 99 minutes)\n\nFor 2nd case, we need to check if we can add one extra minute (60 seconds) to our current remaining seconds (it should not exceed 99 seconds). \n\nOther than these we will not worry about prepending zeros since there is no cost associated with it. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n# Code\n```\nclass Solution {\npublic:\n // Used to calculate the cost to reach from startAt -> val (by updating startAt to val)\n int findCost(int &startAt,int moveCost,int pushCost,int val){\n int ans = 0;\n if(startAt!=val){\n ans+=moveCost;\n }\n ans+=pushCost;\n startAt = val;\n return ans;\n }\n\n // Find cost to reach given minute and second. \n int findMinCost(int startAt,int moveCost,int pushCost,int min,int sec){\n if(min==0 && sec==0) return 0;\n int ans = 0;\n int m0 = min%10, m1 = min/10, s0 = sec%10, s1 = sec/10;\n // This array helps in avoiding prepending zeros calculation\n vector <int> time = {m1,m0,s1,s0};\n \n int ind = 0;\n while(ind<4 && time[ind]==0) ind++;\n for(int i=ind;i<4;i++){\n ans+=findCost(startAt,moveCost,pushCost,time[i]);\n }\n return ans;\n }\n\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int min1 = targetSeconds/60;\n int sec1 = targetSeconds%60;\n \n int min2 = -1, sec2=-1;\n if(min1 >=1 && sec1+60 <=99){\n min2 = min1-1;\n sec2 = sec1+60;\n }\n\n int ans = INT_MAX;\n if(min1<100)\n ans = findMinCost(startAt,moveCost,pushCost,min1,sec1);\n if(min2!=-1)\n ans = min(ans,findMinCost(startAt,moveCost,pushCost,min2,sec2));\n return ans;\n }\n};\n```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
C++/Python, solution with explanation
|
cpython-solution-with-explanation-by-shu-6vvu
|
First, we can transform targetSeconds into minute: second where second < 60, another choice is to transform a minute into 60 second,\nminute-1: second+60, and r
|
shun6096tw
|
NORMAL
|
2023-05-24T11:40:49.801609+00:00
|
2023-05-24T11:40:49.801656+00:00
| 6 | false |
First, we can transform targetSeconds into ```minute: second``` where ```second < 60```, another choice is to transform a minute into 60 second,\n```minute-1: second+60```, and return min cost between two choices.\n\n### c++\n```cpp\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n auto cost = [&] (int pos, int minute, int second) {\n if (min(minute, second) < 0 || max(minute, second) > 99) return INT_MAX;\n int cost = 0;\n for (auto ch: to_string(minute * 100 + second)) {\n cost += pushCost + (pos != ch - \'0\'? moveCost: 0);\n pos = ch - \'0\';\n }\n return cost;\n };\n int m = targetSeconds / 60, s = targetSeconds % 60; \n return min(cost(startAt, m, s), cost(startAt, m-1, s+60));\n }\n};\n```\n### python\n```python\nclass Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n def cost(pos, minute, second):\n if min(minute, second) < 0 or max(minute, second) > 99: return float(\'inf\')\n cost = 0\n for ch in str(minute * 100 + second):\n cost += pushCost + (moveCost if int(ch) != pos else 0)\n pos = int(ch)\n return cost\n minute, second = targetSeconds // 60, targetSeconds % 60\n return min(cost(startAt, minute, second), cost(startAt, minute-1, second+60))\n```
| 0 | 0 |
['C', 'Python']
| 0 |
minimum-cost-to-set-cooking-time
|
✅ 0ms Beat 100% | ✨ TC O(1) SC O(1) | 🏆 Most Efficient | 👍 Simplest Intuitive Solution
|
0ms-beat-100-tc-o1-sc-o1-most-efficient-1k6jo
|
Intuition\n Describe your first thoughts on how to solve this problem. \nThere is only one way to format a standard form mm:ss for a given time duration (the ta
|
hero080
|
NORMAL
|
2023-05-16T06:58:17.435021+00:00
|
2023-05-16T06:59:10.438552+00:00
| 67 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThere is only one way to format a **standard form** $$mm:ss$$ for a given time duration (the `targetSeconds`), when we specify that $$ss$$ is less than 60.\n\nThere might be an **alternative form** when $ss$ of the standard form is less than 40. That is by converting 1 minute to 60 seconds.\n\nWe just need to try both forms and find the smallest cost.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$\\Theta(1)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$\\Theta(1)$$\n\n# Code\n```\nconstexpr int kFactors[] = {600, 60, 10, 1};\n\nclass Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n start_at_ = startAt + \'0\';\n move_cost_ = moveCost;\n push_cost_ = pushCost;\n\n // Find the standard form first.\n string sequence = "0000";\n for (int i = 0; i < 4; ++i) {\n auto r = std::div(targetSeconds, kFactors[i]);\n sequence[i] = r.quot + \'0\';\n targetSeconds = r.rem;\n }\n int cost = 10\'000\'000;\n // Large `targetSeconds` generates bad stardard form,\n // for example 6540 => [10 9 0 0]\n // In that case only the alternative form below applies.\n if (sequence[0] <= \'9\') {\n cost = CostOf(sequence);\n }\n\n // Now we try to use the alternative form.\n if (sequence[2] <= \'3\') {\n sequence[2] += 6;\n if (--sequence[1] < \'0\') { // handles carry\n sequence[1] += 10;\n --sequence[0];\n }\n if (sequence[0] >= \'0\') { // Validates the form after carry.\n cost = min(cost, CostOf(sequence));\n }\n }\n return cost;\n }\n\n int CostOf(string_view sequence) const {\n int cost = 0;\n char d = start_at_;\n for (char c : sequence) {\n if (c == \'0\' && cost == 0) {\n // Ignores leading 0s\n continue;\n }\n cost += push_cost_ + (d == c ? 0 : move_cost_);\n d = c;\n }\n // cout << "cost of [" << sequence << "] = " << cost << endl;\n return cost;\n }\n\n private:\n char start_at_;\n int move_cost_;\n int push_cost_;\n};\n```
| 0 | 0 |
['C++']
| 0 |
minimum-cost-to-set-cooking-time
|
100% beats
|
100-beats-by-krish25052004-ena5
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
krish25052004
|
NORMAL
|
2023-05-15T04:24:37.886600+00:00
|
2023-05-15T04:24:37.886627+00:00
| 7 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n int targetsec=targetSeconds/60;\n int time=targetSeconds%60;\n if(targetsec==100){\n targetsec=targetsec-1;\n time+=60;\n }\n \n\n int mincost=1000000;\n int i=2;\n while(i>0){\n if(targetsec==100){\n i--;\n continue;\n }\n int mincost1=0;\n if(targetsec==0){\n \n int moves=0;\n int pushes=0;\n if(time>=10){\n moves=2;\n moves-=time/10==startAt?1:0;\n moves-=time/10==time%10?1:0;\n pushes=2;\n }\n else{\n moves=time%10==startAt?0:1;\n pushes=1;\n }\n \n mincost1=pushes*pushCost+moves*moveCost;\n if(time>=60){\n targetsec=1;\n time=time%60;\n\n }\n else{\n \n i--;\n }\n }\n else if(targetsec>=1){\n \n int moves=0;\n int pushes=0;\n if(targetsec>=10){\n moves=4;\n }\n else{\n moves=3;\n }\n if(targetsec<10){\n moves-=targetsec==startAt?1:0;\n }\n else{\n moves-=targetsec/10==startAt?1:0;\n moves-=targetsec/10==targetsec%10?1:0;\n }\n \n if(time<10){\n moves-=targetsec%10==0?1:0;\n moves-=time==0?1:0;\n }\n else{\n moves-=targetsec%10==time/10?1:0;\n moves-=time/10==time%10?1:0;\n }\n\n pushes=targetsec<10?3:4;\n if(time<=39){\n \n targetsec-=1;\n time=time+60;\n }\n else if(time>=60){\n targetsec+=1;\n time=time-60;\n }\n else{\n i--;\n }\n \n mincost1=pushes*pushCost+moves*moveCost;\n \n }\n mincost=Math.min(mincost,mincost1);\n i--;\n }\n return mincost;\n }\n}\n```
| 0 | 0 |
['Java']
| 0 |
patients-with-a-condition
|
REGEXP one liner (MySQL)
|
regexp-one-liner-mysql-by-michael_v-8koz
|
Going through different solutions, I didn\'t see anyone use a regular expression with a boundary. So, I decided to post one.\n\nSELECT * FROM patients WHERE con
|
Michael_V
|
NORMAL
|
2022-05-21T06:12:29.296227+00:00
|
2022-05-21T06:12:29.296254+00:00
| 36,883 | false |
Going through different solutions, I didn\'t see anyone use a regular expression with a boundary. So, I decided to post one.\n```\nSELECT * FROM patients WHERE conditions REGEXP \'\\\\bDIAB1\'\n```\n\nThe expression `conditions REGEXP \'\\\\bDIAB1\'` is actually the same as `conditions LIKE \'% DIAB1%\' OR\nconditions LIKE \'DIAB1%\';`, but it is obviously shorter. \uD83D\uDE09\n\nThe reason they are the same is that `\\b` matches either a non-word character (in our case, a space) or the position before the first character in the string. Also, you need to escape a backslash with another backslash, like so: `\\\\b`. Otherwise, the regular expression won\'t evaluate.\n\nP.S. `\\b` also matches the position after the last character, but it doesn\'t matter in the context of this problem.\n
| 249 | 0 |
['MySQL']
| 24 |
patients-with-a-condition
|
Simple MySQL Solution, faster than 100%
|
simple-mysql-solution-faster-than-100-by-nxeo
|
\nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR\nCONDITIONS LIKE \'DIAB1%\';\n
|
anshulkapoor018
|
NORMAL
|
2020-07-26T09:46:01.507450+00:00
|
2020-07-26T09:46:01.507511+00:00
| 28,210 | false |
```\nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR\nCONDITIONS LIKE \'DIAB1%\';\n```
| 156 | 1 |
[]
| 19 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.