Form 2 for DP Problems | Ending Form | Day 3 Part 1 | Dynamic Programming workshop | Vivek Gupta

Поділитися
Вставка
  • Опубліковано 15 січ 2025

КОМЕНТАРІ • 38

  • @sainathmandavilli6538
    @sainathmandavilli6538 2 роки тому +11

    Vivek please do take sessions for every other concept(strings,greedy,tries,segment trees,tries) just like you took for dp

  • @Sandeep-zd6dq
    @Sandeep-zd6dq 2 роки тому +3

    In my opinion LIS makes sense more with form 1 by keeping the previous index in state and decide/choose for the current index but ultimately both forms are giving same TC so both are good 😊

    • @TheNishant30
      @TheNishant30 Рік тому +1

      The optimal substructure in LIS is: for any element at index i, LIS starting with this particular element is going to be Max of all the LIS'es on its right hand side that start with an element larger than the element at index i, incremented by 1. If we find LIS starting with every element in the sequence, one of them ought to be the LIS of the entire string.
      Optimal substructure in 0/1 knapsack (form 1) is: The optimal solution to the problem is going to be a combination of elements of the input sequence whose elements sum up to

  • @rv__info7876
    @rv__info7876 2 роки тому

    🔥 thanks

  • @santhosh7042
    @santhosh7042 11 місяців тому +3

    for 2nd ques i thought this way but later i came to know. how stupid i'm
    rec(r,c){
    // base case
    if(r

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

    Nice Video Sir ji

  • @vineettanwar7880
    @vineettanwar7880 2 роки тому

    best DP resource

  • @siddharthsaurav3657
    @siddharthsaurav3657 2 роки тому +1

    If we see dp(some state) depends on prev state it can be form 2 or ending at this form.

  • @theuntoldtree
    @theuntoldtree 2 роки тому

    great : )

  • @virenderkumar951
    @virenderkumar951 2 роки тому

    Nice🔥

  • @tarunmali6228
    @tarunmali6228 2 роки тому +1

    21:00

  • @ShubhamGhuleCodes
    @ShubhamGhuleCodes 2 роки тому +1

    6:45 Shouldn't it be arr[L] < last?
    because DP(L, last) is LIS from L to N

    • @mvssguptajagadesh3269
      @mvssguptajagadesh3269 2 роки тому

      no here , the last represents the last element taken , so it should be last

    • @ShubhamGhuleCodes
      @ShubhamGhuleCodes 2 роки тому

      @@mvssguptajagadesh3269 Yeah got it. Thanks!
      I thought from L to N means we're moving from right to left 🙂

  • @AKASHKUMAR-li7li
    @AKASHKUMAR-li7li 6 днів тому

    on 2nd question can we apply dijkstra algorithm...?

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

    the max path sum question is some what like prefix sum

  • @bangali773
    @bangali773 2 роки тому

    Present. Also i solved the last problem in 2 ways. One using the sorting the a[i][j] in decresing order.i keep a vector of pair of pair int int. I store the a[i][j] ,i,j. The sorted in decreasing order of a[i][j]. Then keep intialise dp to -1. Then chevk if it is -1 go for rec else move on. Then if the cell is not visited then go for recurrenec else dont go. And another one as you give hint that the dirty water is coming for upper three cells so the first cells will always be clean . Then we check the maxtime the dirty water will come down to this cell and check if the a[i][j] of the cell is greater than that time it will be clean.

  • @Sandeep-zd6dq
    @Sandeep-zd6dq 2 роки тому

    In case of sun problems if we written -inf then in some cases it overflows and get convert to positive value and vice versa and we don’t want to use long then what should we do although in this question you are only making valid calls which helps in avoiding that return value but in some problems it’s hard to make only valid calls.

  • @BharatiSubramanian99217
    @BharatiSubramanian99217 2 роки тому

    If you're doing this in Java, make sure to use long instead of int. Since the values are at most 10^7

  • @manavpatel-w1x
    @manavpatel-w1x 6 місяців тому

    if some one who is not good at dp i realy say you shoud continu this playlist

  • @mayankmewar5049
    @mayankmewar5049 2 роки тому

    1. avoid using dp array for answer. In this LIS it works but might not work in other problems.
    2. to check if dp state is visited earlier or not use another done[][] array because the return value could go beyond -1(more negative in second problem) for dp and might cause inconsistenty.

  • @itz_me_imraan02
    @itz_me_imraan02 2 роки тому +2

    I feel both form 1 and form 2 are interchangeable at times.. Isn't it??

  • @sachitsankhe5102
    @sachitsankhe5102 2 роки тому

    why not use dp array in the for loop of solve() while finding the best

    • @mvssguptajagadesh3269
      @mvssguptajagadesh3269 2 роки тому

      Yes you are right ,After performing all the rec calls , we can print the last element of dp array(dp[n-1] as our answer ,since it store the maximum LIS value .

    • @sachitsankhe5102
      @sachitsankhe5102 2 роки тому

      @@mvssguptajagadesh3269 no like its not necessary that it would be n-1. We will have to iterate over the dp array to find

    • @vivekgupta3484
      @vivekgupta3484  2 роки тому +3

      I discussed this in the Doubt stream yesterday... in this it will work... but refrain from direct usage of DP array.

    • @mvssguptajagadesh3269
      @mvssguptajagadesh3269 2 роки тому +1

      @@sachitsankhe5102 yes , you are right . We have to iterate over the DP array to get the Max value.

    • @rachakondaeshwar4129
      @rachakondaeshwar4129 6 місяців тому

      @@mvssguptajagadesh3269 no some of states might not be computed as required call function only

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

    can someone share the code for form 1pls

  • @snehilsinha4689
    @snehilsinha4689 2 роки тому +1

    I tried to solve the LIS problem using Form 1 itself but realised that while calling the function, i.e., lengthOfLIS(level = 0,lastTaken = 0), we can't really take the initial lastTaken as 0 since it means that we are starting from the first element and taking the first element as lastTaken itself. But the catch is that we can't even take -1 as the lastTaken for the initial call as it would try to access dp[0][-1]. So do we need to keep another visited bool array as you had suggested in case of the DP returning negatives.
    How to proceed on this one ? Or did I miss something completely ? Please help.
    int n;
    int arr[10001];
    int dp[10001][10001];
    int lengthOfLIS(int level, int lastTaken) {
    //base case
    if(level == n) return 0;
    //cache check
    if(dp[level][lastTaken] != -1) return dp[level][lastTaken];
    //choice 1 : Not Take
    int ans = lengthOfLIS(level+1, lastTaken);
    //check
    if(arr[level] > arr[lastTaken])
    //choice 2 : Take
    ans = max(ans,1+lengthOfLIS(level+1, level));
    //save and return
    return dp[level][lastTaken] = ans;
    }
    int main() {
    cin>>n;
    f(i,n)cin>>arr[i];
    memset(dp,-1,sizeof(dp));
    cout

    • @vivekgupta3484
      @vivekgupta3484  2 роки тому +4

      That's a very good issue... we will learn this in a session today.

    • @teji644
      @teji644 2 роки тому

      just cache it in dp[level][last_taken+1] :)
      Also add another condition if(last_taken==-1 || nums[level] > nums[last_taken]).. while picking since you need to explore the path even if u havent picked any element previously

    • @abhisheksingh-rz8nj
      @abhisheksingh-rz8nj 3 місяці тому

      Or you can save the items dp[leve] [ lastTaken+1] 1 based indexing for dp arraay

  • @SatyamKumar-yz5gk
    @SatyamKumar-yz5gk 2 роки тому +1

    Hey Vivek. I tried solving CSES "Coin Combination 2" for the whole day and I'm stuck on it. Like I'm not getting an idea to cache only distinct ordered coins.
    It will take only 5 mins to explain the approach.
    Please Please Please. Do explain, please.
    I am solving it with your approach only. Please do explain that problem in your taught approach.
    Thank you.

  • @balubhaskar1915
    @balubhaskar1915 6 місяців тому

    Vivek in Lis problem of form 2, in solve function {
    For() {
    Rec call;
    }
    }
    Rec call's Time complexity is n^2;
    That we are calling in for loop of n;
    So, is it over all time complexity is O(n^3)?
    Please clarify @vivekgupta3484

    • @dank7044
      @dank7044 6 місяців тому

      DP use kia hai na bhai,toh amortized TC o(n^2) hi hai; Per state:max transition possible n hai(due to loop),islie TC: num(states)*(num transitions+1)