Next Closest Time

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

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

  • @KevinNaughtonJr
    @KevinNaughtonJr  5 ปีที่แล้ว +26

    Forgot to add a break statement if you find the time you generated to not be valid (i.e. there's no point in checking the rest of the digits if you have already found one that invalidates the current time) so if you guys want to marginally speed up your algorithm go for it. Or if you wanna roast me for forgetting that's cool too I guess your choice

    • @JM_utube
      @JM_utube 4 ปีที่แล้ว

      i think in an interview setting, merely explaining how you could optimize your algorithm is sufficient. that invalidation is a little more complicated than a 1 or 2 minute change

    • @meghnath1993
      @meghnath1993 4 ปีที่แล้ว +1

      I like your videos. It would be great if you could explain time and space complexities for your solutions.

    • @Justdailylife
      @Justdailylife 3 ปีที่แล้ว

      Will not roast chill, got the problem explanation.

  • @edwardnewgate2198
    @edwardnewgate2198 5 ปีที่แล้ว +18

    Great approach!
    But I think there's a simpler approach- we can do just increase the minute by 1.
    If the minute reaches 60, we increase the hour
    If the hour reaches 24, we just reset the day.
    I found my approach quite simple as well. Would love your feedback!

    • @MrLukezeng
      @MrLukezeng 5 ปีที่แล้ว

      Aman Bhardwaj I agree

    • @JM_utube
      @JM_utube 4 ปีที่แล้ว +2

      you could write stringToTime and timeToString helper functions. both are 1-3 lines. they'll return an integer representing number of minutes from 0 to that time. so the answer ends up as follows:
      const END_OF_DAY = stringToTime('23:59');
      const startTime = stringToTime(time);
      const digits = new Set([time[0], time[1], time[3], time[4]]);
      for (let i = startTime + 1; i < END_OF_DAY; i++) { //loop until end of day
      const cur = timeToString(i);
      if (digits.has(cur[0]) && digits.has(cur[1]) && digits.has(cur[3]) && digits.has(cur[4])) {
      return cur;
      }
      }
      for (let i = 0; i < startTime; i++) { //start over at time 0
      const cur = timeToString(i);
      if (digits.has(cur[0]) && digits.has(cur[1]) && digits.has(cur[3]) && digits.has(cur[4])) {
      return cur;
      }
      }

  • @magic8ball3mt
    @magic8ball3mt 5 ปีที่แล้ว +17

    Clean approach! I'm an ex-G engineer, so hope you don't mind feedback.
    The upper bound to your solution will take 60*24=1440 iterations (in the case of 00:00). One observation is that there are only 4*4*4*4=256 possible time strings, and we can even prune out invalid strings such as 44:90. A more efficient solution would be to recursively generate possible strings and pull out the correct one.

    • @anshulabhinav13
      @anshulabhinav13 5 ปีที่แล้ว

      Sounds interesting.
      Would you be kind enough to give ur recursive solution here? I can't seem to wrap my head around it.

    • @anandbharti7735
      @anandbharti7735 5 ปีที่แล้ว

      I was thinking about the same.Here provided solution is brute force.

    • @JM_utube
      @JM_utube 4 ปีที่แล้ว +1

      I agree that there are 2 methods - check all possible times and see if the digit combination is valid / check all possible permutations and minimize the difference. I think in the interview I'd probably go with kevin's approach because it's SO MUCH SIMPLER TO CODE and you don't have to write the recursive permutations function. however, given 45-60 minutes you could probably do either. but so much of a coding interview is about communication and understanding the needs of the company versus your capabilities. so if you exhaust all of your time with the coding problem, you won't be able to learn much about the actual role. This, I think, is a glaring problem with most software interview schedules. So much time is spent on problem solving as opposed to direct discussions to see if both parties' career goals can be met.

    • @jeromeeusebius
      @jeromeeusebius 4 ปีที่แล้ว

      @@JM_utube agree with last part. There should be discussion on fit. Not everyone can code well under pressure but I get it that knowing algorithms and data structures will make on a better programmer.

  • @galanoth17
    @galanoth17 2 ปีที่แล้ว +1

    Can anyone solve this in 30 min if they have never seen this problem before? I was totally off track and never thought about this solution because it seems too brute force, but I couldn't come up with anything better.

  • @MadeByDAY
    @MadeByDAY 5 ปีที่แล้ว +6

    Ugh math-ish problems are the worst, but dealing with time-related code seems like an important thing in the industry! Thankfully, there are just enough memes in this video to distract me from the fact that I dislike math-ish problems and allows me to focus on the problem itself 😂
    Thanks for the upload Kevin and keep up the great work!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +3

      Thanks Dom! I do my best to keep the videos short, entertaining, and comical (if possible) :)

  • @chaengsaltz829
    @chaengsaltz829 2 ปีที่แล้ว

    Love your approach. Thanks for sharing!

  • @contact2ck
    @contact2ck 5 ปีที่แล้ว +3

    Hi Kevin,
    Kindly explain 489. Robot Room Cleaner leetcode problem.

  • @contact2ck
    @contact2ck 5 ปีที่แล้ว +2

    For beginner PFB better solution:
    public static String nextClosestTime(String time) {
    HashSet set = new HashSet();
    set.add(new Integer(time.charAt(0)- '0'));
    set.add(new Integer(time.charAt(1)- '0'));
    set.add(new Integer(time.charAt(3)- '0'));
    set.add(new Integer(time.charAt(4)- '0'));
    int hours = Integer.parseInt(time.substring(0,2));
    int mins = Integer.parseInt(time.substring(3));
    while(true) {
    mins++;
    hours = (hours + mins/60) % 24;
    mins = mins % 60;
    if(set.contains(hours/10) &&
    set.contains(hours%10) &&
    set.contains(mins/10) &&
    set.contains(mins%10)) break;
    }
    return String.format("%02d", hours) + ":" + String.format("%02d", mins);
    }

  • @div9210
    @div9210 6 หลายเดือนก่อน

    Thanks Buddy, you made it look like an easy problem.

  • @suhasnayak4704
    @suhasnayak4704 5 ปีที่แล้ว +1

    Suggestion :In general, it would be helpful if you can explain the problem and your approach over the white board at the beginning and then start with coding. That would make much more sense than just directly jumping into coding!

    • @asphix
      @asphix 5 ปีที่แล้ว +3

      For me I prefer these short videos as they don’t tend to be drawn out and pedantic

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +8

      Hey Suhas, I do my best to explain while also keeping the video short and hopefully amusing. I think some people will get bored if I take too long to explain something so i do my best to explain quickly or explain as i go!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +1

      @@asphix Yeah I try my best to keep them short and to the point because I worry that people will get bored if the explanations are too drawn out

    • @vothiquynhyen09
      @vothiquynhyen09 5 ปีที่แล้ว

      yes please keep the videos short and to the point like this. I have seen so many other long boring videos where the person tried to use a white board and I just wanted them to move to the coding part but they never did or did so in a hurried way

  • @kimstuart7989
    @kimstuart7989 2 ปีที่แล้ว

    Just curious but for anyone who has gone through an interview, with this being one of Google's favorite questions, is this something that could be given during a first round interview in the mix of the 2-3 questions to solve in 45 minutes? Or would this be more of an isolated type of higher round systems design type question where you get the whole time to break this problem down? I'm trying to gauge the amount of time I give myself (on my timer) to solve these types of questions vs the Fibonacci type questions.

  • @sridhargana155
    @sridhargana155 2 ปีที่แล้ว

    I get a Time Limit Exceeded Error for this approach

  • @harsh4002
    @harsh4002 5 ปีที่แล้ว +3

    It'll help if you talk about the time and space complexity of your algorithms too.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +2

      Hey Harsh yeah you're right, I've been trying to be better about that I just forget to sometimes. I'd say this solution's stime complexity is O(N) where N is the number of possible times we can generate and I think the space complexity is constant

    • @mrkhntr
      @mrkhntr 3 ปีที่แล้ว +2

      @@KevinNaughtonJr I'd think this would be a constant time algorithm as N is constant here, you can walk thru all possible times in a constant amount of time (24*60 iterations)

  • @akshayavenkatesan2912
    @akshayavenkatesan2912 5 ปีที่แล้ว +1

    You are soooo amazing ! I have my Google interview tonight ..I hope all these helps !

    • @aryansaxenafanclub3184
      @aryansaxenafanclub3184 5 ปีที่แล้ว

      Hey, I have my interview in 1 week, could you please share how many questions were asked? and what was the topic?

    • @MIDNightPT4
      @MIDNightPT4 5 ปีที่แล้ว

      Did you get the job?

    • @akshayavenkatesan2912
      @akshayavenkatesan2912 5 ปีที่แล้ว

      @@MIDNightPT4 no ! Rejected

    • @akshayavenkatesan2912
      @akshayavenkatesan2912 5 ปีที่แล้ว

      @@aryansaxenafanclub3184 hopefully urs went well !!

  • @ShaliniNegi24
    @ShaliniNegi24 4 ปีที่แล้ว +1

    I am not able to understand this line
    minute = (minute + 1 ) % ( 24 * 60)
    Someone please help.

    • @kaushiktummalapalli8145
      @kaushiktummalapalli8145 4 ปีที่แล้ว

      same! I'm also stuck here :(

    • @sagarahuja4386
      @sagarahuja4386 3 ปีที่แล้ว +1

      So we are dealing in military time from 00:00 to 23:59. Suppose clock is at 23:59, adding next minute would give it 00:00 instead 24:00
      So to keep the time in this range(00:00 to 23:59), we need to mod our time value by total minutes in 24 hrs i.e 24*60 to keep minutes in the range. So whenever we are at 23:59 and clock strikes next minute, we should achieve 00:00.
      Hope this helps!!

  • @Johnathanaa7
    @Johnathanaa7 5 ปีที่แล้ว

    Great explanation, thank you!

  • @Nobody2310
    @Nobody2310 5 ปีที่แล้ว +1

    Wow! What an approach! Made it so easy! Thanks kevin! I was banging my head making it so complicated , it is funny, i was actually trying like grabbing the last digit adding 1 to it and checking if it is under minutes limit and so on..totally dumb and faulty approach..lol. Thanks a lot!

  • @ishan_kumar
    @ishan_kumar 5 ปีที่แล้ว

    Explains all problem efficiently then no issue. Misses one break and everyone loses their mind. XD

  • @saisanjaynagarur
    @saisanjaynagarur 5 ปีที่แล้ว

    kevin can you explain and solve leetcode problem no.1067 digit count in range

  • @binodha
    @binodha 5 ปีที่แล้ว

    Hello Kevin, love your videos, quite helpful, Thank you so much. One odd question: what was that music track with Piano in the background at the beginning of this video? Couldn't find it myself

  • @zn4798
    @zn4798 3 ปีที่แล้ว

    hope you can publish more video about leetcode solutions

  • @contact2ck
    @contact2ck 5 ปีที่แล้ว +1

    Hi Kevin,
    Can you explain how this code will satisfy
    Input: "23:59"
    Output: "22:22"

    • @thehaipro
      @thehaipro 5 ปีที่แล้ว +1

      This is for the next day

    • @fatcat22able
      @fatcat22able ปีที่แล้ว +1

      The "minutes = (minutes + 1) % (24 x 60)" line will keep the total minutes from exceeding 1439, which will bind the time to stay within 00:00 to 23:59 inclusive. So for that example, 23:59 will keep iterating from 00:00 onward until it hits 22:22. Remember the problem is asking for the *next* closest time, which doesn't include the closest time before.

    • @contact2ck
      @contact2ck ปีที่แล้ว +1

      @@fatcat22able Thanks my friend !

  • @aayushsingh3140
    @aayushsingh3140 5 ปีที่แล้ว

    heyyy can you please discuss the optimal BST problem

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      What's the problem number? I'll put it on my list :)

  • @talirabetti8066
    @talirabetti8066 5 ปีที่แล้ว

    Thanks for the video! What's the motivation for your brute force solution vs. a solution that only iterates through the relevant digits?

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Anytime Tali! This is just simulating a clock and adding a minute every time, but I guess you could of also taken the valid digits and made permutations of those digits and then checked if the time you have is greater than your starting time?

  • @chandanagp1774
    @chandanagp1774 5 ปีที่แล้ว

    Can you please make a video on longest palindromic substring. Leetcode 5?

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +1

      Thanks for the suggestion I'll see what I can do!

  • @alex.perreras
    @alex.perreras 3 ปีที่แล้ว

    You forgot to put break when isValid=False found

  • @rijuban4321
    @rijuban4321 4 ปีที่แล้ว +1

    pause the video at exactly 4:50 ,look at kevin's face , thank me later

  • @fnumeherunnsa7491
    @fnumeherunnsa7491 5 ปีที่แล้ว

    Thank you making all the problems seem so easy. If its ok would you please do The Median of Two sorted Array (Leetcode No 4) .Thanks Again :)

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Anytime!!! I'll see what I can do :)

  • @shikharchaudhary6984
    @shikharchaudhary6984 4 ปีที่แล้ว +1

    Google does not care about this solution. What it cares about is developing thinking and solving it by second approach, that is by using only the given 4 digits. This is what all matters in a Google interview.
    Next time if you want to solve a Google question, then don't do the naive one. Instead, do the one which requires some thinking.

  • @vk1618
    @vk1618 4 ปีที่แล้ว

    #review