Minimum Time Difference - Leetcode 539 - Python

  Рет қаралды 8,446

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 21
@BillyL6
@BillyL6 2 ай бұрын
My idea is different, hope this helps someone. My solution does use extra space. it uses a min heap to sort it in nlogn time after fixing the edge case. The way to beat the issue of the time going past midnight is to just add 1440 minutes or 24 hours to each timePoints as a new point. If you think about it, 00:00 is no different from 24:00, so we're just adding the extra day to fix our edge case issue. For example, if the test case is [00:00, 23:59], the list is now [00:00,23:59, 24:00, 47:59] in a min heap. Then you can easily just convert it to all to minutes. Then in a simple while loop, pop from head, let's call it a, and do an lowest = min(lowest, abs(a - minHeap[0])) and watch out for the edge case of when minHeap is length of 1 as it doesn't have anything to compare if it's the last item in the heap.
@yourick93
@yourick93 2 ай бұрын
There is one missing point of using the second approach. In the line 23 we have to iterate till the `last_m` (max possible minute from the line 15) instead of `len(exists)`. It will improve efficiency for some test cases.
@NeetCodeIO
@NeetCodeIO 2 ай бұрын
Oh good point 💡
@MikPosp
@MikPosp 2 ай бұрын
Thanks for your video! Here is a solution in one line: def findMinDifference(self, a): return min(map(sub,(q:=sorted(int(s[:2])*60+int(s[3:]) for s in a))[1:]+[q[0]+1440],q))
@provarence7361
@provarence7361 Ай бұрын
yep good luck using this in an interview
@adilansari-hq9ge
@adilansari-hq9ge 2 ай бұрын
As always Awesome solution. Last year had interview with DocuSign and I could not clear that round , all because i used built in sorting , but they were expecting counting Sort.
@ACium.
@ACium. 2 ай бұрын
Oh counting sort!
@Ryan-g7h
@Ryan-g7h 2 ай бұрын
note that the min part only works when the time is sorted, ty for the great solution
@2EOGIY
@2EOGIY 2 ай бұрын
That solution is missing the fact that there will be no bigger distance than 12h, so half of 1440
@NeetCodeIO
@NeetCodeIO 2 ай бұрын
good point! technically 1440 is still a more precise upperbound than float("inf"), but you're write, 720 should be sufficient as well
@Axel.Blazer
@Axel.Blazer 2 ай бұрын
@@NeetCodeIO print(f"{write}")
@saarthak09
@saarthak09 2 ай бұрын
My JS Solution beats 97% Thank You NeetCode :)
@albin_joby
@albin_joby 2 ай бұрын
if timePoints.count(min(timePoints)) > 1: return 0 for i in range(len(timePoints)): h,m = map(int,timePoints[i].split(":")) timePoints[i] = m+(h*60) timePoints.sort() res = 1440 - timePoints[-1] + timePoints[0] for i in range(1, len(timePoints)): diff = timePoints[i]-timePoints[i-1] res = min(res, diff) return res
@parica117
@parica117 2 ай бұрын
thank you bro
@leo881010able
@leo881010able 2 ай бұрын
The part you initialize the res was mind blowing, gotta watch daily problem everyday no matter whether I can solve the question✨
@provarence7361
@provarence7361 Ай бұрын
So for the second part, you are not computing the two diffs for each set of time points correct? You said you would in the explanation but I dont think this approach does unless I'm wrong
@cineaddict1633
@cineaddict1633 2 ай бұрын
Neetcode make a video on sieve of eratosthenes please
@greedyfishbones
@greedyfishbones 2 ай бұрын
Avg neetcode W
@Corgislife1215
@Corgislife1215 2 ай бұрын
Like if you love neetcode!
@pravyn350
@pravyn350 2 ай бұрын
Off course 😂
@teress1316
@teress1316 2 ай бұрын
downvote if you don't like people begging for likes.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 147 М.
Minimum Array End - Leetcode 3133 - Python
23:22
NeetCodeIO
Рет қаралды 10 М.
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 166 МЛН
My Calendar I - Leetcode 729 - Python
12:57
NeetCodeIO
Рет қаралды 12 М.
Dear Functional Bros
16:50
CodeAesthetic
Рет қаралды 553 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 215 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 642 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 245 М.
Shortest Subarray with Sum at Least K - Leetcode 862 - Python
27:57