Second Minimum Time to Reach Destination - Leetcode 2045 - Python

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 47

  • @fancypants6062
    @fancypants6062 4 місяці тому +58

    Congrats on the grass-touching :)

  • @ParodyCSEDept
    @ParodyCSEDept 4 місяці тому +17

    Michael Cole: The streak.. is ov-
    Neetcode-: Hold my video!

  • @guruprasath2862
    @guruprasath2862 4 місяці тому +8

    I am happy that somehow i figured out this math part by myself.

  • @vizardas
    @vizardas 4 місяці тому +1

    Thanks for the good explanation! I kept exceeding the time limit without the trick of enqueuing each node at most twice. One more optimisation: since all edges are bidirectional, there are only 2 possible cases for the second shortest path. Let's call the shortest path A and suppose it goes over M edges. Either there is a longer path B that goes over M+1 edges, or we take path A and then at the end go to any node and back. So the second shortest path is always either 1 or 2 edges longer than the shortest path. You can apply this to quit BFS early -- if you found a shortest path, finish processing that layer, and exit the while loop. If the final node is in the queue, there is a path with M+1 edges. Otherwise, the second shortest path is M+2 edges. You can convert number of edges to time at the very end.

  • @sachinkashi
    @sachinkashi 4 місяці тому +4

    Watch his videos even though i code in CPP. Just for the logic he provides like no one else on you tube.

  • @rahulsihara8946
    @rahulsihara8946 4 місяці тому

    making sure the visiting twice each node is the trickiest part here and that's where i got stuck. Thanks for the explanation.

  • @subhashvadlamani9412
    @subhashvadlamani9412 4 місяці тому +1

    Hi NeetCodeIO. I think there is a problem in this code where the code can get stuck by visiting non final nodes two times(at different times) and exit out of the loop without returning inside the loop. This happened to me with the first example and I printed the visit_times dict outside the while loop and I got this value "defaultdict(, {1: [0, 6], 2: [3, 13]})"

  • @sunder-kirei
    @sunder-kirei 4 місяці тому +4

    been waiting for this one

  • @yashwantmoharil5892
    @yashwantmoharil5892 4 місяці тому +2

    pls make a vid on Count the Number of Substrings With Dominant Ones

  • @yashk1853
    @yashk1853 4 місяці тому +1

    Thank you for this video!! I just realized I was messing up this part only - 19:43

  • @TANISHQLOHIA
    @TANISHQLOHIA 4 місяці тому +1

    this code seems wrong because we are visiing adjacent nodes of root node at time zero while we should be visiting them at time 3

  • @anonymousgreen5080
    @anonymousgreen5080 4 місяці тому +3

    Nice work bro. 👏

  • @mohit7717
    @mohit7717 4 місяці тому

    What a clean explanation on youtube.... Realy liked your every video.... From where you have learnt

  • @qulinxao
    @qulinxao 4 місяці тому +1

    class Solution:
    def secondMinimum(self, n: int, e: List[List[int]], t: int, c: int) -> int:
    a,b,q=defaultdict(list),defaultdict(lambda:[0]),deque([(1,0)])
    for x,y in e: a[x].append(y); a[y].append(x)
    while q:
    v,d=q.popleft()
    if(d//c)%2:d+=c-(d%c)
    d+=t
    for u in a[v]:
    if (f:=b[u])[-1]!=d and len(f)

  • @Yeelleeay
    @Yeelleeay 4 місяці тому +3

    Welp I realized that I was adding nodes again and again in the queue but couldn't figure out a proper way to prevent it. NeetCode to the rescue as always :)

    • @a3rdtierguy864
      @a3rdtierguy864 4 місяці тому

      I am also thinking what is condition to prevent addition in queue because this will gonna be tle

  • @jaatharsh
    @jaatharsh 4 місяці тому

    can't thankyou enouf, ur true saviour,
    keep up the awesome work @NeetCode

  • @jiankuang9890
    @jiankuang9890 4 місяці тому

    @NeetCodeIO
    On line 23, why would `nei_times.append(cur_time)`?
    I think the `cur_time` is still for `node`, not for `nei`, since we have cur_time updated at the end of while loop?

  • @arihantsinghrana2173
    @arihantsinghrana2173 4 місяці тому +3

    I was wondering just how are you able to solve every question and just exactly much time does it take for you to solve them ??
    cause if I take it upon myself to solve these it would easily take my whole day and even then it's not guaranteed that I would solve it 😥

  • @rostislav_engineer
    @rostislav_engineer 4 місяці тому

    thank you, NeetCode

  • @yang5843
    @yang5843 4 місяці тому +3

    you a g, thanks for uploading

  • @gary1614
    @gary1614 4 місяці тому

    Thank you for the video! So clear now. You de best!

  • @MP-ny3ep
    @MP-ny3ep 4 місяці тому +1

    Thank you for the daily !

  • @ForAeonz
    @ForAeonz 4 місяці тому +1

    I never understood why BFS works and why the edge weight is unit. The weight of the each edge can either be the default time or longer if you at red light. Clearly edge weight can change dynamically depending on the state

    • @NeetCodeIO
      @NeetCodeIO  4 місяці тому

      Yes but the wait time will apply evenly to every single node within a given "level" of the BFS.

    • @ForAeonz
      @ForAeonz 4 місяці тому

      @@NeetCodeIO Ahh I think I understand now... many thanks!

  • @shreyasi1757
    @shreyasi1757 4 місяці тому

    Hey neetcode! can you please also start uploading solution tutorials for leetcode , codeforces etc contests questions (like once a week or twice), it will be very beneficial

  • @juanmacias5922
    @juanmacias5922 4 місяці тому +1

    It's all fun and games. Until the grass touches back...

  • @PiyushYadav-pl9jm
    @PiyushYadav-pl9jm 4 місяці тому +1

    Hey, neetcode, please solve today's contest's Q3

  • @yang5843
    @yang5843 4 місяці тому

    Java Solution
    class Solution {
    public int secondMinimum(int n, int[][] edges, int time, int change) {
    Map map = new HashMap();
    for (int i=1;i

  • @Supakills101
    @Supakills101 4 місяці тому

    POV: you have 30 minutes to solve this and the interviewer says it is trivial

  • @KnightMirkoYo
    @KnightMirkoYo 4 місяці тому

    Our savior :3

  • @nishant7ng
    @nishant7ng 4 місяці тому +1

    Mandal apla ahbhari ayhai

  • @MisTree118
    @MisTree118 4 місяці тому

  • @SanjeeviKumar-g4e
    @SanjeeviKumar-g4e 4 місяці тому

    can you slove the problem number 1687 in leetcode

  • @pratyushthakur8478
    @pratyushthakur8478 4 місяці тому +2

    saw question gave up

  • @sushilkhadka8069
    @sushilkhadka8069 4 місяці тому

    Why DFS gives TLE but BFS doesn't?

    • @NeetCodeIO
      @NeetCodeIO  4 місяці тому

      Shortest path is more efficient with BFS

  • @lokesh8660
    @lokesh8660 4 місяці тому

    🙂

  • @sankhadip_roy
    @sankhadip_roy 4 місяці тому +1

    10:20 as an asian I can't relate sorry

  • @EdificioPepinutalini
    @EdificioPepinutalini 4 місяці тому

    I seriously dont understand why anybody would practice leet code problems anymore (aside for an interview). They are out dated now with advancements in LLMs. Its like asking accountants to use pen and paper for an interview instead of just letting them use excel.

    • @NeetCodeIO
      @NeetCodeIO  4 місяці тому +9

      Who do you think is gonna fix the bad code LLMs write? Definitely not you

    • @EdificioPepinutalini
      @EdificioPepinutalini 4 місяці тому

      @@NeetCodeIO Im just saying i never felt leet code was an accurate testament or assessment to being able to code. I've met so many people that were incredible at leet code problems, but hadn't actually built anything their whole lives. They had no idea how databases, HTTP, websockets, etc. worked. Anyone can get good a leetcode, that doesn't make you a good software engineer by any means. And with regards to LLMs, for me at least they are incredible and save me a ton of time. Just the other day I needed a function that would recursively filter a nested JSON. Instead of waisting my time thinking of this (or googling it), i asked chatGPT and it gave me a working solution in a minute. Maybe you consider figuring out functions like this important, but i dont. I consider creating things for clients in order to make money important. Nobody in the real world cares about how quickly you solved a algorithm, they care about what you deliver.

  • @gowtham_codes
    @gowtham_codes 4 місяці тому

    Bro’s brain is hacked
    What a vision and thinking🫡