Next Closest Time


  • 0

    Click here to see the full article post


  • 0
    C

    For the second method, I am still confused about the sentence "The best possible time != start is the one with the largest (time - start) % (24 * 60)", why this best possible is the closet time?


  • -1
    L

    I get output for 23:59 as 22:22 which is wrong.


  • 0

    @chd I corrected it to better explain.

    @lahiru The only times that use digits from {2, 3, 5, 9} are 22:22 and 23:59. After 23:59 (so when it's midnight), 22:22 will occur before 23:59, so it is the answer.


  • 0
    L

    @awice Sure I get it. Thanks a lot.


  • 0
    T

    Great article! Thanks!


  • 0
    M

    @awice I think the following sentence accidentally has a repeated part:

    "while times like 00:10 = 10 means that (10 - 720) % (24 * 60) = -710 % (24 * 60) = 730 seconds have elapsed."

    I think you want to take out the first "% (24 * 60)" so it reads:
    "while times like 00:10 = 10 means that (10 - 720) = -710 % (24 * 60) = 730 seconds have elapsed."

    Just trying to help, great answer! Thanks for the thorough explanation :-)


  • 0
    A

    There is a faster way to do this. The time is less than 4*4 instead of 4^4.


  • 0
    L

    @Ark-kun How is that possible?


  • 0
    L

    Typos in the 2nd solution: (725 - 720) % (24 * 60) = 5 minutes instead of (725 - 720) % (24 * 60) = 5 seconds


  • 0
    M

    Why don't we break the loop as soon as we find our answer?


  • 0
    A

    @mahekjasani : I guess you are talking about the second approach. I think in this approach, candidate answers are not guaranteed to be generated in a serial order w.r.t time and allowed digit constraints. For example for "20:56", early return will be wrong answer of "22:50" instead of "22:00"


  • 0
    Y

    The following line in the 2nd solution seems to be overcomplicated and not necessary to use floored modulus or normal modulus (especially the floored modulus is not very common and hard to understand/explain/use):

    int candElapsed = Math.floorMod(cur - start, 24 * 60);
    

    Since cur - start can never be equal or bigger than 24 * 60, because cur can be as big as 23:59 which is 1439, and start can be as small as 00:00 which is 0 (this example is not applicable in the solution anyways since none of those 4 digits matches, but just want to show the extreme case). So we only need to care about the negative cases (where cur - start is negative).

    In that case, the negative value can never be equal or smaller than -24 * 60 since cur can be as small as 00:00 and start can be as big as 23:59, resulting in -1439, (again this extreme case is a bad example). so all in all I can just replace this modulus operation with:

    int candElapsed = (cur - start < 0) ? cur - start + 24 * 60 : cur - start;
    

    to get back to the range of 0 < candElapsed < 1440


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.