Convex Hull Trick - Dynamic Programming Optimisation

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ม.ค. 2025

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

  • @AlgorithmsConquered
    @AlgorithmsConquered  4 ปีที่แล้ว +5

    Note: In my line insert function, I should actually be checking if the deque is empty instead of it having a size > 1 since we just need to access the last line. It will still work but it might have one redundant line.

  • @Mohit-fb9nw
    @Mohit-fb9nw 3 ปีที่แล้ว +4

    I highly request you to please make a video on divide and conquer dynamic programming.
    Your videos are really very helpful.

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

      Hey, it's been a while but the video has finally arrived! th-cam.com/video/Ec3fSWk9JOw/w-d-xo.html

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

    Understood CHT really well from this video, thanks!

    • @II_xD_II
      @II_xD_II 4 ปีที่แล้ว

      saarang orz

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

    Could you explain the slope - y.slope - 1 at 21:35?

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

    This video is really awesome, you made it look so simple !! Thank you!!!

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

    TBH, the video is really good, lovely work :)

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

    According to my understanding, for each 'j' we are finding the best 'i', so j is constant for the current part but 'i' is changing, now my doubts are the following:
    I didn't understand how you pick x (variable) for the equation. First of all, we are at 'j' so h_j will be constant currently and you also assumed h_j^2 is constant but in -2h_i * h_j, you said h_j is 'x' (variable). In the intercept part, which must be constant and you said that h_i^2 + dp[i] is constant. Could you explain why this: h_i^2 + dp[i] is constant and h_j as x in -2h_i.h_j?

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

      Yes, for each "j", we are finding the best "i". For every x value, a line has some y value. Consider h_j to be the x value here. Some line will give us the best y value for this x. We consider h_j to be x here because we want to find the minimum y value we can obtain with this x value.
      Note that 'h_i^2 + dp[i]' (variable small 'c') is from the equation 'y = mx + c'.
      Capital 'C' is a fixed cost given to us as input.
      Consider that we are at j and we have computed dp[j]. For any index k > j, this index j will become 'i' for k. After we compute dp[j], we treat it as "i". Now, we need to insert this value into our data structure so that indices to our right can possibly use it. This value is basically some information we have - the slope and the intercept. We are treating -2h_i as the slope here (variable "m") and h_i^2 + dp[i] as the intercept (variable small "c" which is in the equation "y = mx + c").
      Now the only information missing in this line is x, which when we give to the line, it will give us some y value. What we are essentially doing is, when for some j we want to compute dp[j], we plug in h_j as the x value (we query our data structure with h_j as the x value). Since the line corresponding to this x value will be the optimal line (because this is how our data structure is maintained), we receive a y value, which we set to be the answer for this j. Now, we still need to add the fixed cost capital 'C' (given as input) and 'h_j^2' to this answer which has come from the modified dp recurrence.
      So basically, this is what our data structure will do:
      1. It has a function insert(slope, intercept) which inserts a line for us
      2. It has a function query(x) which will give us the minimum y value possible for this x, where x is h_j.
      Side note: While in this particular problem we are querying for the minimum, the same logic applies to query for the maximum.

    • @piyushnaithani7689
      @piyushnaithani7689 4 ปีที่แล้ว +5

      @@AlgorithmsConquered got it bro👍. Initially I was thinking that: you are saying h_j is x (means h_j is independent variable). Now it is clear, h_j is plugin value of x in equation of line to get the best value and Li chao is doing that.

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

    at 7:15 it should be monotonic*

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

    Kind of impossible case, but may be possible: when lines are parallel, then to check which line gives best can be checked by intercept value.

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

      Yes, I think this problem does generate some parallel lines: cses.fi/problemset/task/2084

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

      lines i dont think can be parralel as slopes are -2*h[j] and problem stat mentioned it that all h[i['s are strictly increasing

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

      @@punitkumarojha817 In this particular problem we won't have parallel lines but in some other problems we can.

  • @ajitshiva9193
    @ajitshiva9193 4 ปีที่แล้ว

    Please upload more videos

  • @kushagrashekhawat8227
    @kushagrashekhawat8227 4 ปีที่แล้ว

    Thank you

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

    I M late.... sorry bro. Btw Nice video :) .. GL

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

    amazing video bro, btw the sound is quite small :((

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

      Yeah, sorry about that. This was one of my first videos where I hadn't fully understood what levels to record at. I believe that my more recent videos have decent sound quality. Do check them out!

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

      orz

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

    Nice bedio I liek

  • @manankumawat8364
    @manankumawat8364 4 ปีที่แล้ว

    hj is x, then hj^2 should be x^2 right?

    • @AlgorithmsConquered
      @AlgorithmsConquered  4 ปีที่แล้ว

      I don't get you. Could you please elaborate?

    • @manankumawat8364
      @manankumawat8364 4 ปีที่แล้ว

      @@AlgorithmsConquered Thanks for the reply, 3:15 you are taking "hj^2" as constant but later 3:58 taking "hj" as "x"?

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

      I don’t see what the issue is here. h_j ^2 is a constant value for every j that we can add to the answer after finding dp_j. The reason I’m writing h_j as x is because we will make a function which will give us the smallest y value for some x value.

    • @manankumawat8364
      @manankumawat8364 4 ปีที่แล้ว

      Got it!! Thanks

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

    Nciec1 👍

  • @utkarshjetly668
    @utkarshjetly668 4 ปีที่แล้ว

    gg

  • @thatoneguy908
    @thatoneguy908 4 ปีที่แล้ว

    10th.

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

    Try to speak fast and loud.

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

      Hey, thanks for the feedback. This was one of my first videos when I was still figuring out how to record better audio. My newer videos have much louder and better audio.

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

      @@AlgorithmsConquered Nice :)

  • @RahilMittal
    @RahilMittal 4 ปีที่แล้ว

    Gg

  • @aryanraina3631
    @aryanraina3631 4 ปีที่แล้ว

    I m late

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

    first :)

  • @deepanshgoel6385
    @deepanshgoel6385 4 ปีที่แล้ว

    orz

  • @nazh7664
    @nazh7664 4 ปีที่แล้ว

    Second