Open the Lock - Leetcode 752 - Python

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 CODING SOLUTIONS: • Coding Interview Solut...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    🌲 TREE PLAYLIST: • Invert Binary Tree - D...
    💡 GRAPH PLAYLIST: • Course Schedule - Grap...
    💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
    💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
    💡 BINARY SEARCH PLAYLIST: • Binary Search
    📚 STACK PLAYLIST: • Stack Problems
    Problem Link: leetcode.com/problems/open-th...
    Python Code: github.com/neetcode-gh/leetco...
    0:00 - Read the problem
    2:14 - Drawing Explanation
    7:43 - Coding Explanation
    leetcode 752
    This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
    #facebook #interview #python
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • Věda a technologie

Komentáře • 61

  • @NeetCode
    @NeetCode  Před 2 lety +10

    Python Code: github.com/neetcode-gh/leetcode/blob/main/752-Open-the-Lock.py
    Java Code: github.com/ndesai15/coding-java/blob/master/src/com/coding/patterns/bfs/OpenLock.java

    • @masternobody1896
      @masternobody1896 Před 2 lety +1

      can you make a video how we get a job at google

    • @masternobody1896
      @masternobody1896 Před 2 lety

      get good at leetcode? will it give me a job?

    • @thoufic907
      @thoufic907 Před 2 lety

      What was ur first thought when u saw this problem?

  • @prajwal2583
    @prajwal2583 Před 2 lety +93

    I thought you won't be posting anymore since you got a job at Google, your videos helps a lot

    • @chinesemimi
      @chinesemimi Před 2 lety +9

      He’s been there for a year already!

    • @rawallon
      @rawallon Před 2 lety +1

      @@chinesemimi That in covid years of human years?

  • @tinali7149
    @tinali7149 Před 2 lety +3

    I tired to search this problem in your channel this morning but couldn’t find the video, and then you posted this video after I had lunch!

  • @NaqushabNeyazee
    @NaqushabNeyazee Před 2 lety +12

    At this point I have started listening to your content as a podcast. Great work!!

    • @rawallon
      @rawallon Před 2 lety

      Who needs asmr when we got this

  • @ambroseaurelian9696
    @ambroseaurelian9696 Před 2 lety +7

    Thank you, this why I love this channel the dedication. One day I will make you proud!

  • @showmethemoney824
    @showmethemoney824 Před 2 lety +1

    Appreciate your effort and don’t stop posting

  • @user-sk7tp3wv4m
    @user-sk7tp3wv4m Před 3 měsíci

    appreciate your efforts and big thanks to your explanation. It helps me not to quit practicing the leetcoode

  • @lavanyam3224
    @lavanyam3224 Před 2 lety +5

    Just in case if you didn't know, -1%10 gives 9 in python. so we don't have to add 10 and perform modulo. Great video btw!

  • @hassan7569
    @hassan7569 Před 2 lety +2

    Another day, another NeetCode video.

  • @orangethemeow
    @orangethemeow Před 2 lety +2

    It's so smart to add deadends to the visited set

  • @shinsatori184
    @shinsatori184 Před 2 lety

    Thanks for the explanation. So clear!

  • @MP-ny3ep
    @MP-ny3ep Před 3 měsíci

    Great explanation as always. Thank you !

  • @unitycatalog
    @unitycatalog Před 2 lety +2

    The space complexity is actually O(log 10000 to the base 8) because each combination only has 8 neighbor combinations, and the space needed for visited can be reduced by having a array of Boolean of size 10k (that is 10kb) or using a bit set (size 1.25 KB)

  • @YasirYSR
    @YasirYSR Před 2 lety +1

    in Love with this channel

  • @fightmeonclubpenguin7748
    @fightmeonclubpenguin7748 Před 2 lety +4

    I got this question in my Amazon interview a couple months ago!

  • @homyakMilashka
    @homyakMilashka Před 3 měsíci

    I was so scared of this problem before I saw the graph and explanation, your explanations are golden.

  • @yunaf4609
    @yunaf4609 Před 2 lety +3

    Great explanation! This one was tricky because it uses the word "minimum" so I totally thought it was a DP problem and tried to attack it using DFS + memoization and I got stuck. But your explanation was really clear! :)

    • @zhen9445
      @zhen9445 Před 2 lety

      I think DFS + memoization still work if you keep update a minimum path variable and search all the working path? BFS can ensure when you first get the target, you are in the shortest path, so you don't need to search all the working path

  • @Deschuttes
    @Deschuttes Před 2 lety

    Thanks need, please keep going.

  • @fa11en1ce
    @fa11en1ce Před 2 lety +1

    Would've been interesting to see this solved using a* if you use each lock digit as a coordinate

  • @metarus208
    @metarus208 Před 2 lety

    thank you for soldiering on

  • @utkarshagupte1178
    @utkarshagupte1178 Před 2 lety

    Wonderful explanation.

  • @nathamuni9435
    @nathamuni9435 Před 2 lety +1

    Ooh 🤯 every intuitive

  • @jeevisiran21
    @jeevisiran21 Před 2 lety +4

    Hi Neetcode thanks for your neat explaination...can you pls solve leetcode 465. Optimal account balance problem...this question asked by Google and uber...and this problem is pretty unique!

  • @tianmingguo8271
    @tianmingguo8271 Před 2 lety

    Great explanation.

  • @sudhaarulmani9218
    @sudhaarulmani9218 Před 2 lety

    Thank you 🙏

  • @chenzhuo9
    @chenzhuo9 Před 2 lety +4

    Hi Neet! Thanks for your videos! They are really helpful! Could you pls do 987. Vertical Order Traversal of a Binary Tree? This is a really popular question for FB (top 10). I think it will help many people! Thanks

  • @irarose3536
    @irarose3536 Před 2 lety

    Thanks!

  • @ks-mq3fm
    @ks-mq3fm Před 2 lety +1

    Hi your videos are amazing
    Please share your interview experience

  • @shahzebnaveed4776
    @shahzebnaveed4776 Před 2 lety +1

    For 1000 (10^4) possible combinations, we are also generating lock states within the outer while loop. Shouldn't that make the time complexity O(N * 10^4)?

  • @wesammustafa9004
    @wesammustafa9004 Před 2 lety

    Simple and straightforward explanations. I'd appreciate it if you could extend the time allotted for explaining time and space complexity.

  • @beyond-convention
    @beyond-convention Před 2 lety

    your videos are very good. what tools do you use to create videos

  • @telnet8674
    @telnet8674 Před 2 lety

    thank you

  • @Mcfly868
    @Mcfly868 Před 2 lety

    I wonder if A-Star search algo would improve average run time efficiency. It basically applies an estimation-cost-to-destination. depending on current_steps_taken + estimation-cost-to-destination, it will prioritize which state in the queue should be processed next. Reason i say i wonder, is because the worst case would be nlgn, which is worse than O(10000) in this case

  • @stanleyokoli212
    @stanleyokoli212 Před 2 lety +1

    Who has an idea what tool neetcode use for presentation? the whiteboard/pen part? Thanks

    • @CostaKazistov
      @CostaKazistov Před 2 lety

      I was wondering that too.
      Have seen others do whiteboard/pen with OneNote.
      But I'm sure there are better options than OneNote available (especially on Mac).

  • @amirbayat5713
    @amirbayat5713 Před 2 lety

    More of a Python question. How come we didn't return the res in the helper function but the for loop in the main function picked the child of children up?
    How come we can iterate through the res without having it returned?
    Thanks

  • @AchintBhat
    @AchintBhat Před 5 měsíci

    I was wondering if this could be solved by implementing a bidirectional search ... would be more efficient

  • @vatsalsinha9273
    @vatsalsinha9273 Před 2 lety

    Will it be a better approach to reverse the problem and start from target as root node and then try and reach 0000... ? I think it might save us some calculation of nodes not needed to trace in the tree.
    What do you think ?

    • @MrKulkoski
      @MrKulkoski Před 2 lety +2

      it would be literally the same, the start and end positions don't matter

  • @pastori2672
    @pastori2672 Před 3 měsíci

    felt terrible comming up with this solution just to know its basically as efficient as you can get it 😆👍

  • @techenthusiast3966
    @techenthusiast3966 Před 2 lety

    Which softwares do you use to record and draw?

  • @mohithadiyal6083
    @mohithadiyal6083 Před 2 lety +1

    What is Neetcode's leetcode profile?

  • @nishantingle1438
    @nishantingle1438 Před 2 lety

    Once you see 3:01 then its just good ol' BFS

  • @barnetthan9391
    @barnetthan9391 Před 3 měsíci

    ur a goat

  • @commonsolutions8825
    @commonsolutions8825 Před 9 měsíci

    Shouldn’t “0000” be added to the visit set as well? What happens when children(1000) returns “0000”, wouldn’t that cause an infinite loop?

    • @commonsolutions8825
      @commonsolutions8825 Před 9 měsíci

      It won’t cause an infinite loop but the queue will have “0000” appended to it twice. I think…

  • @dpynsnyl
    @dpynsnyl Před 11 měsíci

    Shouldn't we have added "0000" to the visit set in the beginning?

  • @nishadkanago4393
    @nishadkanago4393 Před rokem

    I have written code like this in c++ but this is leading to TLE and I cant figure out why ? Can anyone help
    class Solution {
    public:
    vector children(string x) {
    vector v;
    for (int i=0; i

    • @ilanvinograd2631
      @ilanvinograd2631 Před 3 měsíci +1

      Hi, even though it's been over 9 months, I'd be happy if this information is still relevant.
      Your code didn't work because of the line if (find(s.begin(), s.end(), child) == s.end()). You don’t need to use iteration from beginning to end because it's inefficient. Instead, you can use the more efficient find() function of an unordered set, like s.find(child) == s.end(). This approach reduces the time complexity to O(1) when you change from set to unordered_set.
      Here's an example:
      class Solution {
      public:
      vector children(string x) {
      vector v;
      for (int i = 0; i < 4; i++) {
      char a = (((x[i] - '0') + 1) % 10) + '0';
      v.push_back(x.substr(0, i) + a + x.substr(i + 1));
      char b = (((x[i] - '0') - 1 + 10) % 10) + '0';
      v.push_back(x.substr(0, i) + b + x.substr(i + 1));
      }
      return v;
      }
      int openLock(vector& deadends, string target) {
      if (find(deadends.begin(), deadends.end(), "0000") != deadends.end()) return -1;
      queue q;
      q.push({"0000", 0});
      unordered_set s({"0000"}); //

  • @jay-hk5oo
    @jay-hk5oo Před 3 měsíci

    can we use dfs?

  • @supremoluminary
    @supremoluminary Před 3 měsíci

    You go into the code too soon. Not enough explanation. Sorry.