Longest Increasing Path in a Matrix - Leetcode 329

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

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

  • @rommeltito123
    @rommeltito123 2 ปีที่แล้ว +19

    10+ years coding and I still enjoy watching ur videos.

  • @premraja55
    @premraja55 3 ปีที่แล้ว +42

    The way you make hard problems look so easy is amazing, thanks!

  • @dj1984x
    @dj1984x ปีที่แล้ว +9

    the first hard problem i've gotten without first viewing the solution ... feels good & hard work + Neetcode videos pays off :)

  • @freshlyminted6999
    @freshlyminted6999 3 ปีที่แล้ว +9

    My code was kinda the same, but yours is a lot cleaner. That's something I really appreciate. Thanks for the great content. :)

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

    dude i got an approach before but i wasn't sure of its implementation when i saw urs it's not totally same but the iteration part was missing in my approach now i got that thx dude appreciated it

  • @bhuvneshsingh3347
    @bhuvneshsingh3347 3 ปีที่แล้ว +1

    Thanks!

    • @NeetCode
      @NeetCode  3 ปีที่แล้ว

      Thank you so much!

  • @mingchenma4679
    @mingchenma4679 3 ปีที่แล้ว +8

    Thanks for all your videos, they helped me so much. I landed my dream job. Please keep up ur channel! Merry Christmas!

  • @girirajrdx7277
    @girirajrdx7277 3 ปีที่แล้ว +4

    Pls keep uploading....I am learning a lot.
    You are my guide

  • @NeetCode
    @NeetCode  3 ปีที่แล้ว +5

    Python Code: github.com/neetcode-gh/leetcode/blob/main/329-Longest-Increasing-Path-in-a-Matrix.py
    Java Code (courtesy of a viewer - ndesai15): github.com/ndesai15/coding-java/blob/master/src/com/coding/patterns/dynamicprogramming/memoize/LongestIncreasingPath.java

  • @ngneerin
    @ngneerin 3 ปีที่แล้ว +5

    Beautiful solution. Looking forward to more such videos

  • @rezajeffrey7970
    @rezajeffrey7970 2 ปีที่แล้ว +3

    I literally search all the youtube I didnt find a guy explaining better than you
    Keep on..

  • @minciNashu
    @minciNashu 2 ปีที่แล้ว +1

    this one made me feel good about myself, cuz I knew right away it's a grid DFS with memoized storage

  • @sauravchandra10
    @sauravchandra10 ปีที่แล้ว

    When you said it is a doable hard, I figured lets try this till I get it accepted and after a bit of back and forth, I was able to get my code accepted.
    This is my C++ implementation:
    class Solution {
    public:
    int n,m;
    int drow[4]={-1,0,1,0}, dcol[4]={0,1,0,-1};
    bool isValid(int &row, int &col){
    return row>=0 && row=0 && col

  • @hgk7027
    @hgk7027 3 ปีที่แล้ว +1

    Best Leetcode TH-cam channel!

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

    i think we can directly write this statement right.
    res = 1+max(dfs(r+1,c,matrix[r][c]),dfs(r-1,c,matrix[r][c]),dfs(r,c+1,matrix[r][c]),dfs(r,c-1,matrix[r][c]))
    instead of those 5 lines of code??

    • @buttofthejoke
      @buttofthejoke 11 หลายเดือนก่อน

      I think so too. we are calculating max of res again and again, but we can do max ((left, right, top, bottom) + 1)

    • @sakethkumarpeddi2089
      @sakethkumarpeddi2089 11 หลายเดือนก่อน

      @@buttofthejoke yes.. exactly

    • @hwang1607
      @hwang1607 8 หลายเดือนก่อน

      yes but its more difficult to read

  • @dk20can86
    @dk20can86 2 ปีที่แล้ว +1

    If you want a bottom up DP solution, you can sort the values of the matrix in descending order (i used a heap, but a sorted array would work too) and process them in that order. You'll be guaranteed to have already calculated the LIPs of your larger neighbours as you visit each element, so you don't need DFS/recursion. Same time and space complexity of course.

    • @mubarizsaeed187
      @mubarizsaeed187 2 ปีที่แล้ว

      what do u mean u dont need dfs? how would u find the path tho

    • @xiaohuisun
      @xiaohuisun ปีที่แล้ว

      that's my first thought as well. (m*n)(log(m*n)) for sorting I guess

  • @andrewyuan7881
    @andrewyuan7881 3 ปีที่แล้ว +2

    Great problem and explanation

  • @jamespolemeni5475
    @jamespolemeni5475 2 ปีที่แล้ว

    The Gold Standard of Leetcode tutorials

  • @Septix
    @Septix 3 ปีที่แล้ว +3

    Neet what are you thoughts on spaced repetition? Not writing the entire solution and memorizing it... instead, it would be cards in front: statement of the problem, back: Pattern (two pointers, DFS, etc) and any comments, maybe even drawings. Ideally this would help with just pattern recognition over time but wondering if you think it would be useless/ too much memorizing. Would use Anki for reference, thoughts?

    • @dj1984x
      @dj1984x ปีที่แล้ว

      i think it's a good idea, spaced repetition is shown to be very effective for memorization (that's if you're trying to memorize the blind 75/neetcode150, etc.)
      even better though is simple practice problem solving, so you can solve new problems the first time working through them. spaced repetition could probably help in learning patterns for this

  • @BeingAsh22
    @BeingAsh22 2 ปีที่แล้ว

    This was the best explanation, you made the problem so easy. Thank you!

  • @vincentlius1569
    @vincentlius1569 3 ปีที่แล้ว +1

    very clean code !! like the way you use dictionary as cache

  • @jitbanerjee6875
    @jitbanerjee6875 ปีที่แล้ว

    Small addition, implement backtracking to slightly improve the running time. P.S. Solved this problem on my own in just 20 minutes. Can't stop thanking you.

  • @AryanSingh-zv5ch
    @AryanSingh-zv5ch ปีที่แล้ว

    Thanks man any tips to approach problem like u do?

  • @JR_GameDev
    @JR_GameDev 27 วันที่ผ่านมา

    Thank you my bro your so good

  • @aryanyadav3926
    @aryanyadav3926 2 ปีที่แล้ว +1

    Brilliant explanation!

  • @ronniey4231
    @ronniey4231 ปีที่แล้ว

    Awesome as always ❤❤❤❤

  • @nishantingle1438
    @nishantingle1438 2 ปีที่แล้ว +2

    Graph with Dynamic Programming

    • @henryCcc8614
      @henryCcc8614 2 ปีที่แล้ว

      = Weapon of Mass Destruction

  • @nuamaaniqbal6373
    @nuamaaniqbal6373 2 ปีที่แล้ว

    The problem was nicely broken down!

  • @KnuthMorris
    @KnuthMorris 5 หลายเดือนก่อน

    Brilliant solution.

  • @NhanSleeptight
    @NhanSleeptight 3 ปีที่แล้ว

    Merry Christmas, let do some neetcode today

  • @hanjiang9888
    @hanjiang9888 2 ปีที่แล้ว

    thank you you made dp problems no longer unapproachable to me 😭

  • @刘叶叶
    @刘叶叶 3 ปีที่แล้ว

    Won’t the Error: List index out of range matrix[r][c]

  • @minato6536
    @minato6536 2 ปีที่แล้ว

    How to calculate the number of longest path?

  • @Ragdoll333-q5l
    @Ragdoll333-q5l 3 ปีที่แล้ว

    Hi Neetcode! Could you upload something in terms of bit masking? like 1915?

  • @vadimkokielov2173
    @vadimkokielov2173 2 ปีที่แล้ว

    A bit of trivia here. I just read the section on DFS in Intro to Algorithms (en.wikipedia.org/wiki/Introduction_to_Algorithms ) The theoretical kind of DFS they define there is actually always run on all the vertices of the graph. So anyone who read that book and was paying attention -- I wasn't :) :D -- would have this idea cold.

  • @peeckle1657
    @peeckle1657 2 ปีที่แล้ว

    Great Solution!!

  • @Eggleweb97
    @Eggleweb97 3 ปีที่แล้ว

    please make a video on LeetCode 1499. Max Value of Equation

  • @matthewzhang7330
    @matthewzhang7330 3 ปีที่แล้ว

    Thanks for the solution!

    • @matthewzhang7330
      @matthewzhang7330 3 ปีที่แล้ว

      Would you mind making a video for 68: Text Justification?

  • @romo119
    @romo119 ปีที่แล้ว +1

    Seems like this shouldn't really be labelled "hard". Some of the graph dfs medium problems are much harder

    • @dj1984x
      @dj1984x ปีที่แล้ว +1

      agreed, this was relatively simple for a graph problem, just need to implement cache

  • @ram_pande8039
    @ram_pande8039 3 ปีที่แล้ว

    Very clean code ❤️

  • @paraskumar693
    @paraskumar693 2 ปีที่แล้ว +1

    it should have easy tag

  • @HalfJoked
    @HalfJoked ปีที่แล้ว

    Amazing, thanks so much. I want to just add, I think these few lines look a bit cleaner than the 4 repeated lines of reassigning the 'res' variable.
    directions = [[1,0], [0,1], [-1,0], [0,-1]]
    for rowDir, colDir in directions:
    res = max(res, 1 + backtrack(row+rowDir, col+colDir, cur))

  • @tshotblue22
    @tshotblue22 3 ปีที่แล้ว

    how come backtracking isn’t used here? from each point, isn’t it possible to have several valid paths, where one path is the longest?

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

      My question as well. Seems similar to the word search problem so I don’t get why it’s treated differently. Do you know 2 years later? Lol

  • @amualtheshitaye
    @amualtheshitaye 10 หลายเดือนก่อน

    If this matrix increased by 20% what is new marix

  • @georgegeorge581
    @georgegeorge581 2 ปีที่แล้ว

    Really helpful!!

  • @priyanshudeep1008
    @priyanshudeep1008 2 ปีที่แล้ว

    Isn't this just dp??

  • @shavvasatya7131
    @shavvasatya7131 ปีที่แล้ว +1

    Is'nt not a hard.🤩

  • @senthilkumar5
    @senthilkumar5 2 ปีที่แล้ว

    Best!!!

  • @EduarteBDO
    @EduarteBDO 10 หลายเดือนก่อน

    This is more like a medium problem. It's not hard

  • @AseshShrestha
    @AseshShrestha 3 ปีที่แล้ว

    👏

  • @lakhbawa
    @lakhbawa 3 ปีที่แล้ว

    For god’s sake, pls start using simpler python(without syntax suger) or start using some other language such as Java, we are not here to learn python or see your language skills but program solving, i am referring your other video, cannot dare to watch this one bcoz I don’t wanna waste time learning python syntax, i am sorry if it looks rude

    • @NeetCode
      @NeetCode  3 ปีที่แล้ว +22

      Not sure which video you're talking about, but there's nothing fancy in this code. The general ideas most of the time be easily translated between languages, and if you can't do that it's most likely that you don't understand the solution. Nothing related to the language.
      If anything, python is written like psuedo code, Java has so much boiler plate it makes things more complicated.

    • @mk17173n
      @mk17173n 3 ปีที่แล้ว +4

      Typical Indian thinking he is smarter than people. If you dont like it in Python watch other videos. My primary language is JavaScript but i appreciate these videos.

    • @alokesh985
      @alokesh985 2 ปีที่แล้ว +6

      @@NeetCode Please try and ignore these outlier comments. Most people love your Python solutions and I personally don't think your code can get any simpler! Please continue the great work!

    • @Ak-um1yg
      @Ak-um1yg 2 ปีที่แล้ว +1

      @@mk17173n was there any need to point out the nationality ?? Indians have the best brains .... I think you have some hatred / jealousy for Indian people

    • @Kezbeh
      @Kezbeh 2 ปีที่แล้ว

      @@mk17173n yeh i am also using js and python can be read like English language and we can convert into any language we want. There is no fancy coding here