LeetCode 139. Word Break - Interview Prep Ep 79

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • ⭐ Shop on Amazon to support me: www.amazon.com/?tag=fishercod...
    ⭐ NordVPN to protect your online privacy: go.nordvpn.net/aff_c?offer_id...
    ⭐ NordPass to help manage all of your passwords: go.nordpass.io/aff_c?offer_id...
    Problem link on LeetCode:
    Word Break: leetcode.com/problems/word-br...
    ⭐ Support my channel and connect with me:
    / @fishercoder
    Algorithm explained:
    We use an extra boolean array of size n+1 to serve as a cache, which means up to position dp[i], it's possible that there is a valid word in the given dictionary to break the string into. We use two pointers to loop through all possible substring breakdowns and store the temporary results into this boolean array called DP to help us cut repetitive computations. In the end, we could just return DP[N].
    // TOOLS THAT I USE:
    ○ Memory Foam Set Keyboard Wrist Rest Pad - amzn.to/3cOGOAj
    ○ Electric Height Adjustable Standing Desk - amzn.to/2S9YexJ
    ○ Apple Magic Keyboard (Wireless, Rechargable) - amzn.to/36gy5FJ
    ○ Apple Magic Trackpad 2 (Wireless, Rechargable) - amzn.to/36ltimu
    ○ Apple MacBook Pro - amzn.to/30iSvKE
    ○ All-In One Printer - amzn.to/34etmSi
    ○ Apple AirPods Pro - amzn.to/2GpVYQf
    ○ My new favorite Apple Watch - amzn.to/2EIIUFd
    // MY FAVORITE BOOKS:
    ○ Introduction to Algorithms - amzn.to/36hxHXD
    ○ Designing Data-Intensive Applications - amzn.to/2S7snOg
    ○ Head First Java - amzn.to/2ScLDKa
    ○ Design Patterns - amzn.to/2SaGeU2
    Follow me on Github for complete LeetCode solutions: github.com/fishercoder1534/Le...
    Support me on Patreon: / fishercoder
    My ENTIRE Programming Equipment and Computer Science Bookshelf:
    www.amazon.com/shop/fishercoder
    And make sure you subscribe to my channel!
    Your comments/thoughts/questions/advice will be greatly appreciated!
    #Kahnsalgorithm #graphsearch #topologicalsorting #softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures

Komentáře • 50

  • @Somun-a
    @Somun-a Před 2 lety +3

    I have two words for you "Monospace font" :) Things will align properly if you use one.

  • @toekneema
    @toekneema Před 3 lety +44

    How could anyone come up with this solution without having seen it before

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

      exactly!!!!!!

    • @DarrienGlasser
      @DarrienGlasser Před 2 lety

      Interview problems are horrible

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

      No one does, you just have to slug your way through by practising.

    • @8Trails50
      @8Trails50 Před 2 lety +4

      if you do it top down you'll see how you get to the bottom up. the problem is all these "DP" videos give you the bottom up first - but the intuition comes from the top down.

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

      All programming interviews expect you to have seen these things before, if you have well and good, you have good enough level to write the code they have already thought or at least debug it. Consider an example of solving such questions in interviews if no programming sites existed, apart from easy and some medium level, majority of the questions would never get solved with an efficient solution

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

    How about converting the list to a set?

  • @jeffreylee8557
    @jeffreylee8557 Před 3 lety +1

    Why do we need to "break"? We've already set j < i, j will never exceed i's index, right? I don't get it sorry

  • @user-ic1lc5bw3r
    @user-ic1lc5bw3r Před 3 lety

    why the second change is an optimization?
    what is special at running from i-1 to 0 instead of running from 0 to i-1?
    @Fisher Coder

  • @bitodam
    @bitodam Před 3 lety +3

    HashSet for storing words would optimize it more, right?

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

    No sane person would figure this on their own.

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

    Great work. Am using python but here are some suggestions. Line 15 to be 'break' instead of 'continue' since we are decrementing j, the gap will keep increasing and will keep being larger than the maxLen. Second suggestion is converting wordDict to SET so that you can check the existence of word in constant time in Line 17.

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

    Could you please explain, how did you reach those two optimizations?

    • @MiddleEasternInAmerica
      @MiddleEasternInAmerica Před 3 lety +4

      why would one use dynamic programming ?
      if you draw the state space tree/ recursion tree, you'll find overlapping problems. hence, we need to look into top-
      down/memoization OR bottom-up/tabulization techniques.
      the same problem solution by GeeksForGeeks, they started with drawing the recursion tree.
      we try to avoid the brute force which is O(2 ^n) , which mostly result in Time Limit Exceeded error, by chaching the subproblems results and reuse it.
      - the possible approaches to solve this question (from leetcode solution article) :
      1- Approach 1: Brute Force { Time Complexity: O(2^n), Space Complexity: O(n) }.
      2- Approach 2: Recursion with memoization { Time Complexity: O(n^3), Space Complexity: O(n) }.
      3- Approach 3: Using Breadth-First-Search { Time Complexity: O(n^3), Space Complexity: O(n) }.
      4- Approach 4: Using Dynamic Programming { Time Complexity: O(n^3), Space Complexity: O(n) }.
      Time Complexity O(n^3): there are two nested loops, and substring computation in each iteration
      Space Complexity O(n): there is memoization/dp array/HashSet/or whatever DS we use to momize.
      Hope this answer your question :) :) good luck :):)

  • @kunalkheeva
    @kunalkheeva Před rokem +1

    perfect explanation, appreciate you, also the +1 thing

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

    hi @ 4:50 why did we decide to increment j..even though we had i was still at "C"

    • @hyli3210
      @hyli3210 Před 3 lety

      actually, after each move of i, j should move from 0 to i-1.
      e.g.
      LeetCode
      Leet, Cod, Le, etCo, de, ode

  • @prasathkarthiban1466
    @prasathkarthiban1466 Před rokem +1

    Great explanation sir thank you

  • @speed-stick
    @speed-stick Před 3 měsíci

    Why in the code you increment j until it reaches i and only then increment i, but in the presentation you increment i first and then at some unclear point you increment j?

  • @cindyhancao96
    @cindyhancao96 Před 3 lety +6

    Great video and thanks for the explanation! I watched several videos and this is the only one that explained both the logic and the code clearly. Keep uploading more videos!

  • @knyto2
    @knyto2 Před 3 lety +11

    Thanks for your explanation! It would be helpful if the data in the visual explanation was all aligned and spaced evenly. Other than that, I could understand it.

  • @sarojpanigrahy
    @sarojpanigrahy Před 4 lety

    Sorry, but I did not get it. If possible, would you please explain again, why you are taking boolean array of size n+1 again ?

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

      He is using a dp array - which is a cache for dynamic programming problems, each index means something, example: dp[4] == true, because “leet” (which are the first 4 letters of the input) IS A WORD

  • @allyc1965
    @allyc1965 Před 3 lety +1

    Third optimization:
    When j counts down from i-1 to 0, the continue in the if statement can be replaced with break.

  • @toekneema
    @toekneema Před 3 lety

    Why did you iterate in such a weird way, can't you just do the regular 2d for loop with the slight change of i

    • @killerthoughts6150
      @killerthoughts6150 Před 3 lety

      it is more intuitive, i came up with something like that but didn't think about saving checking the previous word existence (Like dp[1]= F, so L doesn't exist)..

  • @ashwinkrishnan463
    @ashwinkrishnan463 Před 3 lety +1

    It looks like Sliding Window on top of Dynamic Programming.

  • @nirmalm80
    @nirmalm80 Před 3 lety +1

    Good explanation, we can also calculate minLength to optimise along with maxLength

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

    What is the time complexity of this approach? Is it O(n^3) as substring operation takes O(n) time?

  • @learner_1228
    @learner_1228 Před 3 lety

    great!

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

    This is the simplest explanation I have seen so far, thank you sir

  • @joereeve2569
    @joereeve2569 Před 3 lety +3

    Best explanation on YT, thanks!

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

    this person is an example for having both knowledge and no patience/clarity both at the same time , haha

  • @1murkeybadmayn
    @1murkeybadmayn Před 4 měsíci

    i don't understand your explanation, why are you incrementing i before j? j is nested inside the i loop but you started by looping through i before j??

    • @FisherCoder
      @FisherCoder  Před 4 měsíci

      If you set a few breakpoints and walkthrough the code, you'll have a better sense of the code. Good luck!

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

    Any one also getting exception?

    • @ivandrofly
      @ivandrofly Před 2 měsíci

      Was using c# in c# substring needs to s.Substring(i, j-i)

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

    thanks