Minimum Time Difference - Leetcode 539 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ม.ค. 2025

ความคิดเห็น • 21

  • @BillyL6
    @BillyL6 4 หลายเดือนก่อน +8

    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 4 หลายเดือนก่อน +12

    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  4 หลายเดือนก่อน +2

      Oh good point 💡

  • @MikPosp
    @MikPosp 4 หลายเดือนก่อน +4

    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 3 หลายเดือนก่อน

      yep good luck using this in an interview

  • @2EOGIY
    @2EOGIY 4 หลายเดือนก่อน +7

    That solution is missing the fact that there will be no bigger distance than 12h, so half of 1440

    • @NeetCodeIO
      @NeetCodeIO  4 หลายเดือนก่อน +4

      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 4 หลายเดือนก่อน +2

      @@NeetCodeIO print(f"{write}")

  • @adilansari-hq9ge
    @adilansari-hq9ge 4 หลายเดือนก่อน +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.

    • @sineflake
      @sineflake 4 หลายเดือนก่อน

      Oh counting sort!

  • @Ryan-g7h
    @Ryan-g7h 3 หลายเดือนก่อน

    note that the min part only works when the time is sorted, ty for the great solution

  • @saarthak09
    @saarthak09 4 หลายเดือนก่อน +1

    My JS Solution beats 97% Thank You NeetCode :)

  • @albin_joby
    @albin_joby 4 หลายเดือนก่อน +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

    • @Sharuthi727
      @Sharuthi727 3 หลายเดือนก่อน +1

      thank you bro

  • @leo881010able
    @leo881010able 4 หลายเดือนก่อน +2

    The part you initialize the res was mind blowing, gotta watch daily problem everyday no matter whether I can solve the question✨

  • @Corgislife1215
    @Corgislife1215 4 หลายเดือนก่อน +12

    Like if you love neetcode!

    • @pravyn350
      @pravyn350 4 หลายเดือนก่อน

      Off course 😂

    • @teress1316
      @teress1316 4 หลายเดือนก่อน

      downvote if you don't like people begging for likes.

  • @provarence7361
    @provarence7361 3 หลายเดือนก่อน

    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 4 หลายเดือนก่อน

    Neetcode make a video on sieve of eratosthenes please

  • @greedyfishbones
    @greedyfishbones 4 หลายเดือนก่อน +1

    Avg neetcode W