Second Minimum Time to Reach Destination - Leetcode 2045 - Python

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

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

  • @fancypants6062
    @fancypants6062 2 หลายเดือนก่อน +58

    Congrats on the grass-touching :)

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

      💚

  • @ParodyCSEDept
    @ParodyCSEDept 2 หลายเดือนก่อน +17

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

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

  • @guruprasath2862
    @guruprasath2862 2 หลายเดือนก่อน +8

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

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

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

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

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

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

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

  • @yashk1853
    @yashk1853 2 หลายเดือนก่อน +1

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

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

  • @subhashvadlamani9412
    @subhashvadlamani9412 2 หลายเดือนก่อน +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]})"

  • @jiankuang9890
    @jiankuang9890 2 หลายเดือนก่อน

    @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?

  • @mohit7717
    @mohit7717 2 หลายเดือนก่อน

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

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

    been waiting for this one

  • @anonymousgreen5080
    @anonymousgreen5080 2 หลายเดือนก่อน +3

    Nice work bro. 👏

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

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

    • @ForAeonz
      @ForAeonz 2 หลายเดือนก่อน

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

  • @shreyasi1757
    @shreyasi1757 2 หลายเดือนก่อน

    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

  • @yang5843
    @yang5843 2 หลายเดือนก่อน +3

    you a g, thanks for uploading

  • @rostislav_engineer
    @rostislav_engineer 2 หลายเดือนก่อน

    thank you, NeetCode

  • @jaatharsh
    @jaatharsh 2 หลายเดือนก่อน

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

  • @gary1614
    @gary1614 2 หลายเดือนก่อน

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

  • @juanmacias5922
    @juanmacias5922 2 หลายเดือนก่อน +1

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

  • @MisTree118
    @MisTree118 2 หลายเดือนก่อน

  • @MP-ny3ep
    @MP-ny3ep 2 หลายเดือนก่อน +1

    Thank you for the daily !

  • @PiyushYadav-pl9jm
    @PiyushYadav-pl9jm 2 หลายเดือนก่อน +1

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

  • @TANISHQLOHIA
    @TANISHQLOHIA 2 หลายเดือนก่อน

    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

  • @KnightMirkoYo
    @KnightMirkoYo 2 หลายเดือนก่อน

    Our savior :3

  • @Supakills101
    @Supakills101 2 หลายเดือนก่อน

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

  • @SanjeeviKumar-g4e
    @SanjeeviKumar-g4e 2 หลายเดือนก่อน

    can you slove the problem number 1687 in leetcode

  • @sushilkhadka8069
    @sushilkhadka8069 2 หลายเดือนก่อน

    Why DFS gives TLE but BFS doesn't?

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

      Shortest path is more efficient with BFS

  • @qulinxao
    @qulinxao 2 หลายเดือนก่อน

    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)

  • @yang5843
    @yang5843 2 หลายเดือนก่อน

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

  • @lokesh8660
    @lokesh8660 2 หลายเดือนก่อน

    🙂

  • @nishant7ng
    @nishant7ng 2 หลายเดือนก่อน +1

    Mandal apla ahbhari ayhai

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

    saw question gave up

  • @sankhadip_roy
    @sankhadip_roy 2 หลายเดือนก่อน +1

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

  • @EdificioPepinutalini
    @EdificioPepinutalini 2 หลายเดือนก่อน

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

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

    • @EdificioPepinutalini
      @EdificioPepinutalini 2 หลายเดือนก่อน

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

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