babybear4812
babybear4812
  • 82
  • 225 816
How to Solve ANY Dynamic Programming Problem
A comprehensive and exhaustive guide to answering ANY DP problem that may come your way. Let me know if you have any questions :)
ARTICLE: medium.com/@mitrovic.aleksandar/the-ultimate-guide-to-dynamic-programming-65865ef7ec5b
UNIQUE PATHS VIDEO: ua-cam.com/video/vzzJpyDyE3g/v-deo.html
leetcode.com/problems/unique-paths/
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Let's connect on LinkedIn!
www.linkedin.com/in/aleksmitrovic/
Follow me on Medium!
medium.com/@mitrovic.aleksandar
Questions or suggestions for new videos? Email me!
babybear4812@gmail.com
Переглядів: 7 919

Відео

MAXIMUM PERFORMANCE OF A TEAM (Leetcode) - Code & Whiteboard
Переглядів 3 тис.3 роки тому
An O(NlogN) time and O(N) space solution to Leetcode 1383 - Maximum Performance of a Team, as being asked by DoorDash! Let me know down below if you have any questions :) leetcode.com/problems/maximum-performance-of-a-team/ Let's connect on LinkedIn! www.linkedin.com/in/aleksmitrovic/ Follow me on Medium! medium.com/@mitrovic.aleksandar Questions or suggestions for new videos? Email me! babybea...
RANDOM PICK WITH BLACKLIST (Leetcode) - Code & Whiteboard
Переглядів 2 тис.3 роки тому
Leetcode 710 is a really tricky problem being asked by Two Sigma that requires a rather unique algorithm to solve efficiently! Let me know if you have any questions below! :) leetcode.com/problems/random-pick-with-blacklist/ Let's connect on LinkedIn! www.linkedin.com/in/aleksmitrovic/ Follow me on Medium! medium.com/@mitrovic.aleksandar Questions or suggestions for new videos? Email me! babybe...
COUNT UNHAPPY FRIENDS (Leetcode) - Code & Whiteboard
Переглядів 3,7 тис.3 роки тому
A tricky Bloomberg favourite! Thankfully, the solution itself is quite succinct :) Time Complexity: O(n^2). For each of the n people, we will walk through (up to) n-1 entries in their preferences. Therefore, n * (n-1) approaches O(n^2). Space Complexity: O(n^2). We will have n entries in the dictionary we create, and each one of those can have (up to) n-1 entries in the set. Therefore, n * (n-1...
FIND MEDIAN FROM DATA STREAM (Leetcode) - Code & Whiteboard
Переглядів 1,6 тис.3 роки тому
A great problem (with an even better solution) that's being asked by all of the FAANG companies! Let me know if you have any questions down below :) Let's connect on LinkedIn! www.linkedin.com/in/aleksmitrovic/ Follow me on Medium! medium.com/@mitrovic.aleksandar Questions or suggestions for new videos? Email me! babybear4812@gmail.com
SHOPPER'S DELIGHT - IBM INTERVIEW QUESTION - Code & Whiteboard
Переглядів 4,7 тис.3 роки тому
TYPO @ 19:38 - should be greater than, not less than!! This is my absolute favourite problem and solution to any Data Structure & Algorithm problem I've come across to date. The question is being asked by IBM in their Online Assessment (at least) for backend developers. Let me know if you'd like to see more of these kinds of questions! :) Let's connect on LinkedIn! www.linkedin.com/in/aleksmitr...
BINARY TREE RIGHT SIDE VIEW (Leetcode) - Code & Whiteboard
Переглядів 4483 роки тому
A cheeky O(N) solution to Facebook's popular Binary Tree Right Side View problem Leetcode #199. Let me know if you have any questions down below! :) leetcode.com/problems/binary-tree-right-side-view/ Let's connect on LinkedIn! www.linkedin.com/in/aleksmitrovic/ Follow me on Medium! medium.com/@mitrovic.aleksandar Questions or suggestions for new videos? Email me! babybear4812@gmail.com
LEFTMOST COLUMN WITH AT LEAST A ONE (Leetcode) - Code & Whiteboard
Переглядів 5533 роки тому
One of Facebook's most popular problems in the past 6 months! Let me know if you have any questions down below :) leetcode.com/problems/leftmost-column-with-at-least-a-one/ Let's connect on LinkedIn! www.linkedin.com/in/aleksmitrovic/ Follow me on Medium! medium.com/@mitrovic.aleksandar Questions or suggestions for new videos? Email me! babybear4812@gmail.com
CONVERT BINARY SEARCH TREE TO SORTED DOUBLY LINKED LIST (Leetcode) - Code & Whiteboard
Переглядів 6 тис.3 роки тому
One of Facebook's most recently asked questions of the past 6 months! Let me know down below if you have any questions :) leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ Let's connect on LinkedIn! www.linkedin.com/in/aleksmitrovic/ Follow me on Medium! medium.com/@mitrovic.aleksandar Questions or suggestions for new videos? Email me! babybear4812@gmail.com
REGULAR EXPRESSION MATCHING (Leetcode) - Code & Whiteboard
Переглядів 4,2 тис.3 роки тому
SUPER TOUGH PROBLEM that's currently being asked by Amazon and Facebook! This one's a real headache but I hope this video helps clarify things :) Here's a FREE, IN-DEPTH article I wrote about solving Dynamic Programming problems. It talks about how to approach and evolve your DP solutions over the course of an interview, from soup to nuts! I hope you find it useful: medium.com/@mitrovic.aleksan...
NUMBER OF PROVINCES (Leetcode) - Code & Whiteboard
Переглядів 20 тис.3 роки тому
CORRECTION: The time complexity is actually O(N). See the discussion in the pinned comments for an explanation :) Leetcode 547 has been super popular with Amazon and Goldman Sachs recently! Have a look and let me know if you have any questions or comments :) leetcode.com/problems/number-of-provinces/ Let's connect on LinkedIn! www.linkedin.com/in/aleksmitrovic/ Follow me on Medium! medium.com/@...
REORGANIZE STRING (Leetcode) - Code & Whiteboard
Переглядів 1,9 тис.3 роки тому
SEE PINNED COMMENT: I made a mistake in my understanding of how the `charCountsIdx` integer works around the ~28 min mark. @Martial Studiôs was kind enough to point out the mistake and explain the correctly how the code actually worked in this small segment. Thank you! Also, I have no damn idea why my beard came out looking so uneven here. I promise I know how to trim. This problem has been pop...
LFU CACHE (Leetcode) - Code & Whiteboard
Переглядів 6 тис.3 роки тому
This one's tough! We break down the constant time solution on implementing a Least Frequently Used Cache. Let me know down below if you have any questions or comments :) leetcode.com/problems/lfu-cache/ Let's connect on LinkedIn! www.linkedin.com/in/aleksmitrovic/ Follow me on Medium! medium.com/@mitrovic.aleksandar Questions or suggestions for new videos? Email me! babybear4812@gmail.com
UNIQUE PATHS (Leetcode) - Code & Whiteboard
Переглядів 8734 роки тому
An O(n*m) solution to Unique Paths, along with a bonus O(1) solution at the end of the video :) Let me know what you guys think, and if you have any suggestions about what to answer next! leetcode.com/problems/unique-paths/ Let's connect on LinkedIn! www.linkedin.com/in/aleksmitrovic/ Follow me on Medium! medium.com/@mitrovic.aleksandar Questions or suggestions for new videos? Email me! babybea...
NUMBER OF SHIPS IN A RECTANGLE (Leetcode) - Code & Whiteboard
Переглядів 5 тис.4 роки тому
Check the pinned comment to have a look at a more detailed explanation of why this solution satisfies the limit of at most 400 API calls; I briefly brushed it off in the video as I wasn't entirely sure, but one of our viewers seems to have gotten it! :) A logarithmic solution to Leetcode 1274 - Number of Ships in a Rectangle. It's a tricky problem! Let me know if you have any questions about it...
GROUP ANAGRAMS (Leetcode) - Code & Whiteboard
Переглядів 3064 роки тому
GROUP ANAGRAMS (Leetcode) - Code & Whiteboard
LONGEST INCREASING PATH IN A MATRIX (Leetcode) - Code & Whiteboard
Переглядів 1 тис.4 роки тому
LONGEST INCREASING PATH IN A MATRIX (Leetcode) - Code & Whiteboard
DESIGN SEARCH AUTOCOMPLETE SYSTEM (Leetcode) - Code & Whiteboard
Переглядів 6 тис.4 роки тому
DESIGN SEARCH AUTOCOMPLETE SYSTEM (Leetcode) - Code & Whiteboard
RESTORE IP ADDRESSES (Leetcode) - Code & Whiteboard
Переглядів 3,1 тис.4 роки тому
RESTORE IP ADDRESSES (Leetcode) - Code & Whiteboard
LARGEST BST SUBTREE (Leetcode) - Code & Whiteboard
Переглядів 2,2 тис.4 роки тому
LARGEST BST SUBTREE (Leetcode) - Code & Whiteboard
LOWEST COMMON ANCESTOR OF DEEPEST LEAVES (Leetcode) - Code & Whiteboard
Переглядів 8434 роки тому
LOWEST COMMON ANCESTOR OF DEEPEST LEAVES (Leetcode) - Code & Whiteboard
POW(X, N) (Leetcode) - Code & Whiteboard
Переглядів 8814 роки тому
POW(X, N) (Leetcode) - Code & Whiteboard
MAXIMUM WIDTH OF BINARY TREE (Leetcode) - Code & Whiteboard
Переглядів 1,6 тис.4 роки тому
MAXIMUM WIDTH OF BINARY TREE (Leetcode) - Code & Whiteboard
FLATTEN A MULTILEVEL DOUBLY LINKED LIST (Leetcode) - Code & Whiteboard
Переглядів 3,7 тис.4 роки тому
FLATTEN A MULTILEVEL DOUBLY LINKED LIST (Leetcode) - Code & Whiteboard
TWO CITY SCHEDULING (Leetcode) - Code & Whiteboard
Переглядів 1,1 тис.4 роки тому
TWO CITY SCHEDULING (Leetcode) - Code & Whiteboard
BEST TIME TO BUY AND SELL STOCK IV (Leetcode) - Code & Whiteboard
Переглядів 8294 роки тому
BEST TIME TO BUY AND SELL STOCK IV (Leetcode) - Code & Whiteboard
WORD SQUARES (Leetcode) - Code & Whiteboard
Переглядів 3 тис.4 роки тому
WORD SQUARES (Leetcode) - Code & Whiteboard
NON-DECREASING ARRAY (Leetcode) - Code & Whiteboard
Переглядів 1,3 тис.4 роки тому
NON-DECREASING ARRAY (Leetcode) - Code & Whiteboard
COUNT COMPLETE TREE NODES (Leetcode) - Code & Whiteboard
Переглядів 1,2 тис.4 роки тому
COUNT COMPLETE TREE NODES (Leetcode) - Code & Whiteboard
SUM OF MUTATED ARRAY CLOSEST TO TARGET (Leetcode) - Code & Whiteboard
Переглядів 1,8 тис.4 роки тому
SUM OF MUTATED ARRAY CLOSEST TO TARGET (Leetcode) - Code & Whiteboard

КОМЕНТАРІ

  • @huongpham3580
    @huongpham3580 11 днів тому

    very clear explanation!!! thanks

  • @techpiano7010
    @techpiano7010 17 днів тому

    Thank you sir.I needed this video at this time!😄

  • @anoops7974
    @anoops7974 Місяць тому

    Great explanation. Thank youuu !!

  • @MattieRamos
    @MattieRamos 2 місяці тому

    Dude, thank you for this. Embarrassed to say that I've been coming back to this problem for a week while working on Binary Search. LeetCode has this listed as an "easy" problem. I feel as if you just do it recursively then yes, it's easy to solve. But using Binary Search to optimize it turned into a lot of googling and youtubing that made no sense until I came across this video. Thanks again!

  • @sumitvaise5452
    @sumitvaise5452 2 місяці тому

    explanation is just amazing, thank you!

  • @bag_of_pixels
    @bag_of_pixels 3 місяці тому

    For those, who like me still didn't understand solution after video, here code in python: ```python from typing import List import logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) def findCircleNum(isConnected: List[List[int]]) -> int: cnt, visited, l = 0, set(), len(isConnected) def dfs(start: int): visited.add(start) for end in range(l): if isConnected[start][end] and end not in visited: dfs(end) for start in range(l): if start not in visited: cnt += 1 dfs(start) return cnt if __name__ == "__main__": test_cases = [ [[1, 1, 0], [1, 1, 0], [0, 0, 1]], [[1, 0, 0], [0, 1, 0], [0, 0, 1]], [[1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 1, 1], [1, 0, 1, 1]], ] expected = [2, 3, 1] for index, item in enumerate(test_cases): res = findCircleNum(item) logger.debug(f"res == {res}, expected == {expected[index]}") ``` Simply speaking, dfs marks as `visited` all the linked to current item nodes, therefore in the main loop `cnt += 1` called only for remaining nodes

  • @tindo0038
    @tindo0038 3 місяці тому

    I understand the implementation part, but I am not sure why we are interested in the earliest end time :(. greedy is tough, man.

    • @babybear-hq9yd
      @babybear-hq9yd 3 місяці тому

      I haven't looked at this problem or any Leetcode in almost 3 years, but from what I recall, that's roughly covered around 6:30. Conceptually, if I need a meeting room & all current rooms are being used/are booked, I am going to look for the one that ends soonest because that is the first one that I will occupy if it is available at my time.

    • @tindo0038
      @tindo0038 3 місяці тому

      @@babybear-hq9yd thank you!

  • @ani_jonah
    @ani_jonah 3 місяці тому

    what pen and setup do you use to draw diagrams?

    • @babybear-hq9yd
      @babybear-hq9yd 3 місяці тому

      I used sketchpad for the online whiteboard, and used a HUION 420 Drawing Tablet for the pen/tablet setup.

    • @ani_jonah
      @ani_jonah 3 місяці тому

      @@babybear-hq9yd thanks!

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

    you are great <3

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

    it took me 45 minutes to use the trie version of this, and I found yours, I am using this version from now on. Thanks!

    • @babybear-hq9yd
      @babybear-hq9yd 4 місяці тому

      love to hear it, glad it helped man!

  • @htchannel123
    @htchannel123 5 місяців тому

    Thank you so much for giving a great explanation.

  • @asimzuhaib2451
    @asimzuhaib2451 5 місяців тому

    bc angrezo thoda dheere bol liya kro banda smaghne aaya hai

  • @ANUSHAKRISHNA-m1k
    @ANUSHAKRISHNA-m1k 7 місяців тому

    This is the simplest version I've come across, can't thank you enough

  • @inderbindra87
    @inderbindra87 7 місяців тому

    @babybear4812 - The time complexity of the code will O(N^2) as in the worst case when none of the cities is connected, in that case, the DFS method will called N times, and the outer loop will also be executed n times making it O(N^2).

  • @AlexBlack-xz8hp
    @AlexBlack-xz8hp 8 місяців тому

    Your video took this from being pretty incomprehensible to me to being super trivial in 20 minuts. Thank you sooooo much for your very clear explanation. It was incredibly helpful.

  • @saitmesutcanilhaner8850
    @saitmesutcanilhaner8850 8 місяців тому

    Best explanation I have seen. Thank you very much! The solution seems simple but it is not easy to come up with the solution on the spot especially given the limited amount of time in the interviews. I don't think I would be able to fully solve this myself in 20 minutes.

  • @DenysGarbuz
    @DenysGarbuz 8 місяців тому

    trash solution with comprehensive amount of code even using queue, when problem states that you should use constant space. The coolest approach I have seen, is "needle" approach, when we use each lvl of tree as linked list nodes. it uses O(1)

  • @sayeedchowdhury11
    @sayeedchowdhury11 8 місяців тому

    Excellent video, subbed, keep it up

  • @shadowhawk5511
    @shadowhawk5511 8 місяців тому

    Thank you for mentioning about aliasing on curPath array when we directly add to result array!

  • @nithinsastrytellapuri291
    @nithinsastrytellapuri291 9 місяців тому

    Great explanation !

  • @JarvisBirmingham-b8q
    @JarvisBirmingham-b8q 9 місяців тому

    Request for toonblast leetcode

  • @iiilllilllliiiiiillllliiii5613
    @iiilllilllliiiiiillllliiii5613 11 місяців тому

    Amazing!

  • @shivangrathore
    @shivangrathore 11 місяців тому

    This problem was so confusing for me to understand, thanks for explaining

  • @J1MKAKA1N
    @J1MKAKA1N 11 місяців тому

    Much thanks, quite clearly explained!

  • @m4hi2
    @m4hi2 11 місяців тому

    Hey, great explaination and video. But I think leetcode has introduced some new test cases and this approach doesn't work anymore. I've followed this and coded up the Go version but it got WA. Directly copied the python version still got WA. 😞

    • @babybear-hq9yd
      @babybear-hq9yd 11 місяців тому

      ah bummer sorry to hear. haven't touched this stuff in a few years so can't help much

    • @m4hi2
      @m4hi2 11 місяців тому

      @@babybear-hq9yd no worries, solved it. just wanted to let you know. thanks for the content.

  • @bhushankhope8949
    @bhushankhope8949 11 місяців тому

    what is the time and space complexity of this code?

    • @babybear-hq9yd
      @babybear-hq9yd 11 місяців тому

      probably O(n*m) if i remember correctly

  • @NextGenAdvancedNerd
    @NextGenAdvancedNerd Рік тому

    yes,bear is right about nonlocal declaration ,we use global var inside function incase we need to update a variable declare/used outside of a function and can be used throughout code .similarly we use non local var inside nested function incase we need to update a variable declared in the outer function.local is when we declare and use variables inside any function.

  • @ManishaSharma-o5t
    @ManishaSharma-o5t Рік тому

    best best best explanation :)

  • @el_m_ghoul
    @el_m_ghoul Рік тому

    Nice one, man. Thanks.

  • @arunimachakraborty1175
    @arunimachakraborty1175 Рік тому

    Its that simple? 😲😲😲 OMG thanks a lot for this

  • @eugeniocastro5118
    @eugeniocastro5118 Рік тому

    buena viejo, explicaste chido

  • @northridgefield
    @northridgefield Рік тому

    cool explanation, thanks :)

  • @malikjavaid3029
    @malikjavaid3029 Рік тому

    goated video

  • @geob7o
    @geob7o Рік тому

    Yesss, the fact that they call top right before bottom left is low key really confusing 😅

  • @rohanchess8332
    @rohanchess8332 Рік тому

    I think there is confusion in the complexity, lets say the graph is only directly connected pairwise (like 1-2, 3-4, 5-6 ... etc) in that case the dfs function will be called N/2 time and therefore time complexity is O(N^2) only. We are supposed to track how many times dfs function will be called, not how many times a node gets visited.

  • @NitirajSinghRathore
    @NitirajSinghRathore Рік тому

    Just remove the ANY word from the title. Because this is all the theory etc and even the intuition is discussed everywhere, books, videos etc. But all that still does not help when trying to solve a difficult problem. So I was expecting that you will also introduce the intuition both for recursion and DP with more complex problems so that practice and thought process will help solve ANY problem. But you just discussed one problem and probably the most easiest one and call the video title as ANY. I can guarantee that just by watching this video even you will not be able to the next difficult problem where DP will "improve" the recursion. Ask yourself.

  • @vortexgamingtv0
    @vortexgamingtv0 Рік тому

    what would be the time complexity with and without using memo ??

  • @KiyotakaAyanokoji1
    @KiyotakaAyanokoji1 Рік тому

    it really gave clarity through that dry run!! , i think i mis-read the question though , . . . . .what i thought was -> , it was necessary that we have exgatly k employees selected ? our(your) code can give (maximum )answer even when we dont consider k people , but less than k people , , here is a test case . . . . eff = [100 , 1 , 1] , speed = [100 , 1 , 1 ] k = 3 , so we need 3 people but taking only the first guy gives us the answer as 100*100 , while taking all three of them reduces our answer to 1*(100+1+1)=102 , ,, , this is could be a variant to the original question , small change to our code like start taking maximum only when the size of heap has reached k should just do the thing!! Happy learning !

  • @vikashmaurya2679
    @vikashmaurya2679 Рік тому

    Subscription added

  • @MuhammadNiaazWahab
    @MuhammadNiaazWahab Рік тому

    How come it’s enough for you to DFS from the curr as START only? How come we don’t have to also consider other STARTS for this (using curr as END) So if for example were iterating on START =2. So yes we check all the [2][0…5]. But how about checking the [0…5][2] ones too?

  • @muditkhanna8164
    @muditkhanna8164 Рік тому

    this is the best explanation out there for word break 2

  • @AiminglowNAccpble
    @AiminglowNAccpble Рік тому

    12:16

  • @rydmerlin
    @rydmerlin Рік тому

    Can you define dfs as a nested function and keep graph outside it? Then you don’t need it as a parameter do you? Why in the official solution do they choose to unmark currNode from visited at the end of dfs?

    • @babybear-hq9yd
      @babybear-hq9yd Рік тому

      You probably could nest it, i don't see why not. And I'm not sure about the official solution I haven't looked at this in years. An initial guess may be because if you don't unmark it as visited, their solution may not know where to continue searching from, but tough to say without having looked at it

  • @robertuptc
    @robertuptc Рік тому

    Very nice explanation, easy to follow. Drawings definetely help a lot. Thanks man

  • @rydmerlin
    @rydmerlin Рік тому

    Did you ever interview at FAANG? And why are you so happy when doing Leetcode ? :-)

    • @babybear-hq9yd
      @babybear-hq9yd Рік тому

      1. i don't work in the SWE space 2. i was young and naive :')

  • @fgtdjkg
    @fgtdjkg Рік тому

    wawaweewa, it's a very nice!

  • @kayehong3615
    @kayehong3615 Рік тому

    It's great. Your idea is more understandable and helpful than leetcode card for me. I'm lucky today to meet your video. Thank you so much.

  • @GauravDG1990
    @GauravDG1990 Рік тому

    Great video. Loved how problem was broken into pieces and every bit was explained throughly. Please keep doing such videos

  • @davithov
    @davithov Рік тому

    Awesome explanation! Thank you!

  • @FernandoDiaz-jk2xg
    @FernandoDiaz-jk2xg Рік тому

    Do you do tutoring on problem solving?

    • @babybear-hq9yd
      @babybear-hq9yd Рік тому

      I don't! Haven't touched this stuff in a couple of years now so definitely too rusty :(

    • @FernandoDiaz-jk2xg
      @FernandoDiaz-jk2xg Рік тому

      @@babybear-hq9yd lol! Man you lucky youbhavtn had to deal with this recently.