Maximum Number of Points with Cost - Leetcode 1937 - Python

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

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

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

    at this point leetcode is testing my willpower rather than my coding knowledge

  • @chien-yucode992
    @chien-yucode992 2 หลายเดือนก่อน +37

    The idea for optimizing from O(m*n^2) to O(m*n) is so smart....

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

    Memoization gave me TLE on 144th testcase and simple DP gave TLE on 153rd testcase. I was confident of solving this myself but this problem humbled me. The optimization is so smart!

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

    At this point its not dynamic programming anymore, its double penetration

  • @deep.space.12
    @deep.space.12 2 หลายเดือนก่อน +2

    My intuition for the left/right optimization:
    Let's say the previous row is [A, B, C, D].
    We only consider elements from left-to-right for now.
    The maximum value for the first element in the current row is: max(A) == A
    The maximum value for the second element in the current row is: max(A - 1, B) == max(A- 1, B)
    The maximum value for the third element in the current row is: max(A - 2, B - 1, C) == max(max(A - 1, B) - 1, C)
    The maximum value for the forth element in the current row is: max(A - 3, B - 2, C - 1, D) == max(max(max(A - 1, B) - 1, C) - 1, D)
    So it's a rolling max(prev_max - 1, element_right_above).
    And similarly do right-to-left for the second half.

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

    small nitpick on 10:25 dp[2] is 7 not 8

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

    this dp monster seems to have no bounds to its power😢😢

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

    This it tough with that double DP aspect to it, feels like a Hard problem IMO. Thanks for the awesome explanation as always man

  • @AbdulWahab-jl4un
    @AbdulWahab-jl4un 2 หลายเดือนก่อน +2

    Genius solution as always😮

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

    Everything up to left/right was quite intuitive. I understand what you did and why. I don't understand the intuition. That's the frustrating part.

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

    took a two week break and came back to this never quitting lc again :( skill gapped

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

      I've been doing leetcode and I still can't reason this shit.

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

      ​@@leshius7230deadass, feels like i got hit with the men in black flash

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

    wow. didn't know that solution so simple! incredible!

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

    we nesting dp now

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

    10:22 Should be 1+max(6,4)=7

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

    Damn, looked at various solutions, but yours is very easy to understand

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

    How do u come out with this? Is it try and error ? Or just experience , cause i would never would have think of computing it left and right.

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

    will solving enough dp problems help me come up with the left and right array intuitively? I was able to do the brute force dp but not thsi one

    • @VishalKumar-lw3yh
      @VishalKumar-lw3yh 2 หลายเดือนก่อน

      NO😑

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

      Probably not, but now when you get to a similar problem you'll know how to solve and that's what matter. What we can get from this problem is the idea from left- right and that some problems have double nested dp

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

    it would have been be good if you could share your thought process/intuition.

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

    It's a bit of a stretch to call the solution DP. It's more of a clever precalculation. The features of the solution lacks the usual features of DP like exploring combinations. I get that is encoded in the precalculation hence why I think it shouldn't be tagged as dynamic programming.

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

    very smart technique

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

    Thanks for the consistent videos mate.!❤I appreciate your efforts

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

    i knew it was a DP problem the second I read the problem and constraints but I really got humbled when Memoization failed, tried fixing it but did not work, came straight here, Thanks neetcode!

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

    Beautiful explanation. Thank you

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

    thank you for sharing this!

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

    Thanks, great explanation.

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

    Memoization solution gave me the tle is horryfying.

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

      How much testcases u passed with memoization?
      I got TLE on 144th testcase

    • @AdityaSharma-eg4do
      @AdityaSharma-eg4do 2 หลายเดือนก่อน

      @@069_Souvik same

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

      @@069_Souvik same

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

      @@069_Souvik i got stuck in this too

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

      @@069_Souvik 152

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

    Thank you for such a great explanation. ❤
    I couldnt able to identify that this can be solved using dp 😢
    How to identity dp can be used to problems
    I used different approach (i think its greedy) but it was wrong, my solution is.. taking max val in a row and keeping track of max id and using this to find max val in next row and summing up.. got failed bcoz elements in a row are not always unique.

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

    Thanks for this video. This will be my first time solving DP problem.

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

    very nice explanation

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

    This is the first video of neetcode where I am unable to understand what he is explaining

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

    The fact I can't get such intuition on the spot makes me hopeless for interviews

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

      do such people normally exist?

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

      @@LifeZone-j3w well Neetcode is one 😅, and probably many more

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

    you using a new keyboard or what, a lot of typos today :D, keep up the good work though!

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

    I am here just to hear the description read out :)

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

    Leetcode's difficulty algorithm is broken! No way this is a Medium and not a Hard.

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

    Can you say your intuition about it

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

    can anyone explain me how left and right arrays work to give us the max value? I am unable to understand how its working.

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

      I'm not sure if my take is correct, but here's how i understood it,
      left and right feels like a greedy solution more than a dp solution where you take the max between the previous and current utility (val - cost) where the current col has a cost of 0 and the relative cost is the dist from the current col, this is because you cant really reuse the calculations for any of the cols because each cols despite having the same utility have a relative cost

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

      @@Elias_90 thanks bro

    • @deep.space.12
      @deep.space.12 2 หลายเดือนก่อน

      Let's say the previous row is [A, B, C, D].
      We only consider elements from left to right for now.
      The maximum value for the first element in the current row is: max(A) == A
      The maximum value for the second element in the current row is: max(A - 1, B) == max(A- 1, B)
      The maximum value for the third element in the current row is: max(A - 2, B - 1, C) == max(max(A - 1, B) - 1, C)
      The maximum value for the forth element in the current row is: max(A - 3, B - 2, C - 1, D) == max(max(max(A - 1, B) - 1, C) - 1, D)
      So it's rolling max(prev_max - 1, element right above).
      And similarly do right to left for the second half.

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

    At this point i think i should just give up

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

      keep grinding and it will be worth it

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

      This kind of problems is the problems where if your mind don't randomly send a hint you just can't rationalize until a solution, but with enough practice, you can increase the chance of the mind sending a hint but it's always not 100%

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

    this problem is not medium difficult, it is hard

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

    why the hell would anyone ask this in an interview bruv😭

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

    isn't it like nqueens problem a pattern like that

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

    🤯🤯

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

    looking for you

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

    I always feel like I am improving until I get a DP question... Is this really a medium? :(

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

      Its ok, its a hard in disguised

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

      Leetcode's difficulty algorithm is broken! No way this is a Medium and not a Hard.

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

    For the life of me, I cant figure it out how to optimize the get max from previous row part. Thank you so much for the explaination.
    2 questions though:
    - At which point does the thought of 6:00 occurred to you that it is impossible? Did it come at you intuitively or you somehow proved it using quick maff? I too thought that it is impossible at first, but the thought of looping all cells in the prev rows to pick one was too "bruteforce" and I thought it would result in TLE, so I discarded that thoughts.
    - How would you know that looping each row twice (thrice to build the actual dp) would not result in TLE? I did come up with the thoughts of check max for each current_element but the thought of looping all the rows made me discarded that approach

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

    why you are add 1 with value at 2:10

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

      Because the value at that cell is 1

  • @ransh-sahu
    @ransh-sahu 2 หลายเดือนก่อน

    If i hadn't done dp can i solve

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

    what a problem.

  • @arijaa.9315
    @arijaa.9315 2 หลายเดือนก่อน

    The video is all about here and that.hahaha

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

    I am tired boss :(

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

    Nah the explanation wasn't it, after the duplicates thingie came in

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

      i got lost after that

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

    i figured out another approach using heaps, which is slower (2460ms), but still passed. Time complexity mlogm * n.
    class Solution:
    def maxPoints(self, points: List[List[int]]) -> int:
    height = len(points)
    width = len(points[0])
    ans = [[0] * width] + [[None] * width for _ in range(height)]
    for r in range(height):
    heap = [(-points[r][i] + ans[r][i], i) for i in range(width)]
    heapq.heapify(heap)
    while heap:
    n, i = heapq.heappop(heap)
    if ans[r+1][i] is None:
    ans[r+1][i] = n
    if i > 0 and ans[r+1][i-1] is None:
    heapq.heappush(heap, (n+1, i-1))
    if i < width -1 and ans[r+1][i+1] is None:
    heapq.heappush(heap, (n+1, i+1))
    return -min(ans[-1])