Last question in just 11 lines! LIVE Leetcode Weekly 402 | All Problems solved

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

КОМЕНТАРІ • 15

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

    Prefix sum and binary search in the homework question

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

    I solved 3rd problem using dp and binary search

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

    Finally coded last problem after learning about ordered set . Loved the approach !!

  • @noobchess2516
    @noobchess2516 7 місяців тому +1

    hey folks ,,(3rd question Maximum Total Damage With Spell Casting) whats the prob in code logic . we maintain prev take only if curr-prev>=3 . int solve2(const vector&p,int i,int prev, map&mp,vector&dp){
    if(i==p.size())return 0;
    if(dp[i]!=-1)return dp[i];
    int take=0;
    if(abs(prev-p[i])>=3 || prev==-1){
    take = solve2(p,i+1,p[i],mp,dp)+mp[p[i]]*p[i];
    }
    int nottake = solve2(p,i+1,prev,mp,dp);
    return dp[i]= max(take,nottake);

    }
    long long maximumTotalDamage(vector& power) {
    mapmp;
    int n = power.size();
    for(int i=0;i

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

      u forget to memorize the prev because index,prev varies according to states.

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

      @@rahulyela ya true ,, but then range of prev would be max of element(1e9) so will get mle . so this approach will fail. anything else can be done?

    • @blitztour5924
      @blitztour5924 7 місяців тому +1

      @@noobchess2516 You don't have to include the previous element as dp state. Take it as a function variable

    • @blitztour5924
      @blitztour5924 7 місяців тому +2

      I just read your code. The problem here is that you have defined your Dp[level] as the maximum power you can make from this level to the end.
      But lets take a example like 1,6,7. Here your code will take 1 , 6 and when it will go to level 3(which is power 7). Take condition will not work and it will process to not take this 7 power element. But notice again your dp[level=3] will store 0 as the maximum spell it can create from this level. Which is wrong.
      So to prevent this from happening we will skip all the states in which take will not work and goes to the state in which take will work (if no exist we will return 0). We are skipping all the states because there is no point in doing so. Even if we go in those states we cant take so nothing will be added in our solution

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

      @@blitztour5924 got it 👍 crazy explaination thanks

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

    Solved last problem using Segment Tree, was difficult implementing custom segment tree node considering edge cases but made it

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

      Why custom? wasn't it just maintaining 0/1 and range sum.

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

      @@vivekgupta3484 actually the way I implemented my each range representative node contain below info
      peakCount, leftEle, secondLeftEle, rightEle, secondRightEle
      using these I implemented :p, may sound dumb/way too complicated but in this direction only I got the intuition

  • @JIGARSINGTHAKOR-yy6qp
    @JIGARSINGTHAKOR-yy6qp 7 місяців тому

    Bhaiya is "cracking the coding interview " book is worth to buy in 2024???

  • @trineshreddybs842
    @trineshreddybs842 7 місяців тому +1

    hi