DP 55. Maximum Rectangle Area with all 1's | DP on Rectangles

Поделиться
HTML-код
  • Опубликовано: 3 фев 2025

Комментарии • 153

  • @sunavaroy5672
    @sunavaroy5672 2 года назад +254

    I am not sure whether you would be seeing this or not but I want to thank you for all the playlists (dp, graph, tree, recursion, placement series and the sde sheet) every content I have covered and just in 2 weeks literally just after giving proper 2 weeks to all the problems you have created I was able to clear all the amazon coding interviews. Not sure whether I would be able to clear the manegerial round but I have come so far because of the content you created. Not only amazon , Qualcomm, Vmware every where I am able to clear the rounds because solely of your content. Waiting for more content. Keep the great work going.

    • @binay_krishn
      @binay_krishn 2 года назад +7

      @@chinmaysharma1867 bhai thoda logical socho....koi tumhe aankh band karke bharosa karne ko nahi bolega, although these videos are very helpful but feel free to check other channels as well.

    • @yt_shubham_bgp
      @yt_shubham_bgp 2 года назад +17

      It is impossible to crack within 2 weeks. You would have probably practiced before watching these videos.

    • @rishav144
      @rishav144 2 года назад +6

      only 2 weeks ?? U must be an Expert even before watching playlist

    • @rishav144
      @rishav144 2 года назад +5

      @Ayush Negi haan , bhai 2 weeks mei impossible hai noob se pro bnna .... But, Ye Banda Sunava Roy bol rha hai ..ki isne 2 week mei "every content " pdha hai , which is impossible ...maybe he is a liar ....mjhe to doubt hai ki isne Amazon bhi crack kra hai ki nhi ...ya phir likes paane ke liye jhooth likh dia

    • @scripter7676
      @scripter7676 2 года назад +20

      striver apni asli id se aaaa

  • @varungurnani4989
    @varungurnani4989 Год назад +15

    Special thanks to you for making a clarification on why this problem is in DP playlist.

  • @devbhattdev1607
    @devbhattdev1607 2 года назад +43

    man am feeling proud of myself i was so consitent in this series of the dp. i watched i code every single question. thank you striver bhaiya...

  • @naveensaicremsiyadlapalli3769
    @naveensaicremsiyadlapalli3769 Год назад +26

    Those who are getting wrong answer in Leetcode , In the function they gave the 2d character vector(matrix) not integer vector , so instead of writing matrix[i][j]==1 write matrix[i][j]=='1' then everything will be fine

    • @imPriyansh77
      @imPriyansh77 6 месяцев назад

      Thanks!! Didn't notice it though 😅😅

  • @salmanahmedkhan3979
    @salmanahmedkhan3979 2 года назад +10

    This problem came in my Internship test. Unfortunately, i was unable to solve it and scared from this question. My bad, i haven't look youtube at that time. Gladly, i found a mentor to teach me this.

    • @tushar7305
      @tushar7305 11 месяцев назад

      In the OA ? and How did you apply ?

    • @salmanahmedkhan3979
      @salmanahmedkhan3979 11 месяцев назад

      @@tushar7305OA? What do you mean? Could you explain a bit?

    • @alexlogan7330
      @alexlogan7330 10 месяцев назад +1

      Same here, got this question in Google Interview, i thought this was some Graph problem

    • @AshutoshKumar-es8xy
      @AshutoshKumar-es8xy 9 месяцев назад

      @@alexlogan7330 I too thought it was a graph problem when I first saw it

  • @RamyasriMedagam
    @RamyasriMedagam 7 месяцев назад +1

    Understood!! Thank you Striver for this amazing playlist and kudos to all the hard work you've put in to bring us this amazing content!!

  • @stith_pragya
    @stith_pragya Год назад +1

    UNDERSTOOD..........Thank You So Much for this wonderful video......🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @dashsights4258
    @dashsights4258 Год назад +2

    Thanks striver for giving line of thinking that's the most difficult thing for me! Leap of faith🎉

  • @sauravchandra10
    @sauravchandra10 Год назад +2

    Oh nice building up on previous concepts. Understood, thanks!

  • @ChewyPopcorn69
    @ChewyPopcorn69 7 месяцев назад

    this blew my mind , on god , hats off man!

  • @prabhpreetsingh8665
    @prabhpreetsingh8665 6 месяцев назад +6

    at 6:06 shouldn’t it be 3? there are 3 1’s together so 3*1=3

  • @vishalsinghrajpurohit6037
    @vishalsinghrajpurohit6037 Год назад +5

    The time complexity will be O(n* (m+m)) instead of O(n*(m+n)). because the row size=m and size of height vector=m

    • @HarishSumant
      @HarishSumant Год назад

      Exactly what I was Thinking!

    • @DR-mq1le
      @DR-mq1le Год назад

      didnt get the time complexity someone please clear a doubt , im using the part 1 code for the largest area function
      int largestRectangleArea(vector& heights) {

      int n = heights.size();
      stack st;
      vector PSE(n);
      vector NSE(n);
      for(int i = 0 ; i < n ; i++){
      while(!st.empty() && heights[st.top()] >= heights[i]){
      st.pop();
      }
      PSE[i] = st.empty() ? -1 : st.top();
      st.push(i);

      }
      while(!st.empty()) st.pop();
      for(int i = n-1 ; i >= 0 ; i--){
      while(!st.empty() && heights[st.top()] >= heights[i]){
      st.pop();
      }
      NSE[i] = st.empty() ? n : st.top();
      st.push(i);

      }
      int maxarea = 0;
      for(int i = 0 ; i

    • @gugulothsrilakshmi8729
      @gugulothsrilakshmi8729 9 месяцев назад

      @@DR-mq1le hello friend!!! there nesting not occuring thrice.see the loops correctly in first for loop inside 2 for loops written separately(i mean to say that they are not nested i.e., 2 &3 for loop) .

  • @ronakslibrary8635
    @ronakslibrary8635 Год назад +1

    You have explained it very well sir, thanks for such a wonderful video

  • @cinime
    @cinime 2 года назад +1

    Understood! Thank you sooo much for your great work!!

  • @ayushkiledar4730
    @ayushkiledar4730 2 года назад +1

    Absolutely brilliant!! Great Job!!

  • @gandhijainamgunvantkumar6783
    @gandhijainamgunvantkumar6783 2 года назад +12

    I think the time complexity would be O(n*m) + O(n*O(m))
    O(n*m) because there are 2 loops and O(n* m) is for finding max area in histogram for each row and there are total n rows.

    • @AshutoshKumar-es8xy
      @AshutoshKumar-es8xy 9 месяцев назад

      No the max area in histogram is O(n) every element in height array is pushed and popped from stack only once

  • @ntgrn-pr5yx
    @ntgrn-pr5yx 3 месяца назад

    Thank you so much Striver

  • @DevashishJose
    @DevashishJose Год назад +1

    thank you striver.

  • @shubhamkumarsingh8818
    @shubhamkumarsingh8818 5 месяцев назад

    beautifully explained

  • @UECAshutoshKumar
    @UECAshutoshKumar 7 месяцев назад +1

    Thank you!
    Understood!!!

  • @prabhakaran5542
    @prabhakaran5542 7 месяцев назад +1

    Understood ❤❤❤

  • @aizad786iqbal
    @aizad786iqbal 9 месяцев назад +5

    for max area of row 2...why won't it be 1x3 ??

  • @hrushikesh-1914
    @hrushikesh-1914 Год назад +1

    Thankyou Sir. Understood

  • @ujjalsaha428
    @ujjalsaha428 2 года назад +3

    As always "understood" ❤️

  • @sameersahu4569
    @sameersahu4569 2 года назад +1

    Understood!!!Thank you

  • @narayanpradhan1931
    @narayanpradhan1931 9 месяцев назад

    thanks a lot

  • @yeswanthh5068
    @yeswanthh5068 2 года назад +1

    Thankyou very much💚💚🙂

  • @sparshbarolia1556
    @sparshbarolia1556 Год назад

  • @vinayjangra1401
    @vinayjangra1401 Год назад +2

    In case, if you are curious to solve using Recursive Solution:: Here is the memoized recursive solution(in JAVA):-
    (I HAVE ABSTRACT OUT THE MAX HISTOGRAM FUNCTION) :-)
    public int maximalRectangle(char[][] matrix) {


    int[]dp = new int[matrix.length];
    for(int i = 0; i < dp.length; i++)
    {
    dp[i] = -1;
    }
    int[]heights = new int[matrix[0].length];
    return maxRec(0, matrix, heights, dp);
    }
    public int maxRec(int i, char[][] matrix, int[]heights, int[]dp)
    {
    if(i == matrix.length)
    {

    return 0;
    }
    if(dp[i] != -1)
    {
    return dp[i];
    }
    int[] temp = new int[heights.length];
    for(int j = 0; j < heights.length; j++)
    {
    if(matrix[i][j] == '1')
    {
    heights[j]++;
    // System.out.println("+1");
    }
    else
    {
    heights[j] = 0;
    }
    }
    for(int k = 0; k < temp.length; k++)
    {
    temp[k] = heights[k];
    }

    int rec = maxRec(i + 1, matrix, heights, dp);
    for(int k = 0; k < temp.length; k++)
    {
    heights[k] = temp[k];
    }
    int height = largestRectangleArea(heights);
    int ans = Math.max(height, rec);
    dp[i] = ans;
    return ans;
    }

  • @om3478
    @om3478 Месяц назад

    understood!!!!

  • @yashashwinisingh2733
    @yashashwinisingh2733 9 месяцев назад

    Brilliant.

  • @punkstAr32
    @punkstAr32 Год назад +2

    max area of second row rect should be 3 not 2

  • @rishabhagarwal8049
    @rishabhagarwal8049 2 года назад

    understood sir, thank you very much

  • @subee128
    @subee128 Год назад

    Thanks

  • @verma_jay
    @verma_jay Год назад

    understood

  • @aditithakur6226
    @aditithakur6226 Год назад +1

    Understood Sir :)

  • @rohitraj2321
    @rohitraj2321 Год назад

    love u striver bhaiya

  • @GeniuslyTensai
    @GeniuslyTensai 2 года назад +2

    The same code is giving wrong answer in leetcode. could someone help

  • @tasneemayham974
    @tasneemayham974 Год назад

    BEST BEST BESTTTTT

  • @premduvvapu476
    @premduvvapu476 Год назад

    Understood

  • @mamunahmed4832
    @mamunahmed4832 2 года назад

    You are best of the best

  • @Hello-ro6hr
    @Hello-ro6hr 2 года назад

    UNDERSTOOD ...

  • @aryanyadav3926
    @aryanyadav3926 2 года назад

    Brilliant!

  • @prateekupadhya4875
    @prateekupadhya4875 2 года назад +2

    Brother , how many more videos will be uploaded on the dynamic programming topic ?

    • @takeUforward
      @takeUforward  2 года назад +5

      Done

    • @bishakhdutta8427
      @bishakhdutta8427 2 года назад +2

      @@takeUforward it woud be great if you even cover dp on trees

    • @bishakhdutta8427
      @bishakhdutta8427 2 года назад

      @@takeUforward thanks for teaching me dp. mainly the thought process.

    • @ganeshkamath89
      @ganeshkamath89 2 года назад +1

      @@takeUforward , can you please add some videos on DP on Graph & intervals please. I have followed your series, it is fantastic. But when I see a new problem, still I get stuck.

    • @ganeshkamath89
      @ganeshkamath89 2 года назад

      @@takeUforward . Can you please do one video on how to convert a back tracking problem like Coin change (from Recursion Play list), from Memoization to Tabulation DP please.

  • @abhishek__anand__
    @abhishek__anand__ Год назад

    Great Video

  • @jatinjindgar86
    @jatinjindgar86 2 года назад +4

    Hi Bhaiya, I am a 2022 graduate and i am currently working as a SRE intern at Cisco. I have an offer from Cisco for Site Reliability Engineer profile at a package of 15lpa and an offer from Fis Global for software engineer1 profile at a package of 11 lpa. Can you please advise me on what should I join. Also can I negotiate with Fis Global on the basis on Cisco offer as I am a new Graduate. I want to switch to Sde in the next one year in a top Mnc.I am worried that if join Cisco as Sre, it might hamper my chances when it will come to applying as Sde in companies like Amazon, Microsoft etc. I know in a long run Sre tag will lead to an Sre carrier, but I am not sure whether this really matter much if I want to switch to Sde in next one year itself.

  • @lazymethisside
    @lazymethisside 3 месяца назад

    Today I came to know why all my seniors and batchmates, say about Striver . Such a beautiful explanation 🫡

  • @mayi-h1g
    @mayi-h1g 2 года назад

    Thankx

  • @thelegend5248
    @thelegend5248 2 года назад

    I feel this answer is there in leetcode discuss section

  • @venkateshn9884
    @venkateshn9884 2 года назад +1

    Understood❤

  • @Ubaiish
    @Ubaiish Год назад +1

    why this code giving tle on gfg

  • @priyanshvatsal9791
    @priyanshvatsal9791 Год назад

    Understood 🥰

  • @bunnypubg3475
    @bunnypubg3475 9 месяцев назад +1

    This is today’s lc problem 😅

  • @anshumangupta1001
    @anshumangupta1001 2 года назад

    Understood.

  • @enigma2777
    @enigma2777 2 года назад

    Understood 🖤

  • @parshchoradia9909
    @parshchoradia9909 2 года назад

    Understood Sir!

  • @mriduljain6809
    @mriduljain6809 2 года назад

    Understood Bhaiya

  • @prakashtiwari1455
    @prakashtiwari1455 2 года назад

    Understood..

  • @satyampande684
    @satyampande684 2 года назад

    understood!!

  • @tikshavarshney213
    @tikshavarshney213 2 года назад

    Understood !

  • @DR-mq1le
    @DR-mq1le Год назад

    didnt get the time complexity someone please clear a doubt , im using the part 1 code for the largest area function
    int largestRectangleArea(vector& heights) {

    int n = heights.size();
    stack st;
    vector PSE(n);
    vector NSE(n);
    for(int i = 0 ; i < n ; i++){
    while(!st.empty() && heights[st.top()] >= heights[i]){
    st.pop();
    }
    PSE[i] = st.empty() ? -1 : st.top();
    st.push(i);

    }
    while(!st.empty()) st.pop();
    for(int i = n-1 ; i >= 0 ; i--){
    while(!st.empty() && heights[st.top()] >= heights[i]){
    st.pop();
    }
    NSE[i] = st.empty() ? n : st.top();
    st.push(i);

    }
    int maxarea = 0;
    for(int i = 0 ; i

  • @AB-tp9eg
    @AB-tp9eg 2 года назад

    understood

  • @anonymousabc850
    @anonymousabc850 2 года назад +1

    Can't we solve this using simple DFS/BFS ??

    • @aj9706
      @aj9706 Год назад

      It won't work , Dry run it

  • @utkrishtsingh5520
    @utkrishtsingh5520 2 года назад

    Understood

  • @GandharvDalal
    @GandharvDalal 11 месяцев назад

    done

  • @coding8000
    @coding8000 Год назад +1

    bhai 2 months ka samay milega interview opportunity milne ke baad toh me pakka tera google interview crack kar lunga, ya sirf referral dila de WFH/WFO konsa bhi roles chalenge, Thank You brother in JEE advanced, please bhai

    • @skullcrusher33
      @skullcrusher33 Год назад +1

      lmao striver se referral maang rha h comments mai... aur thanks in jee advanced kya hota hai beh

    • @teachPeace-lc9cj
      @teachPeace-lc9cj Год назад +1

      Bhai pata nhi exam ke din apne aap sick ho gaya tha, mujhe pata hai Medically and Anatomecally, kya hua tha per yaha bol nhi sakta hu yaar 😆😅😉😉😉, per mene gaand ghis ke din raat mehnat kiya tha bhai/behen @@skullcrusher33 , JAI HIND 😁. Salute and Respect to everybody who are running me,Thank You again

    • @teachPeace-lc9cj
      @teachPeace-lc9cj Год назад +1

      Ab mera graduation Speech, jo aaplog banaenge, pura World Dekhega, again JAI HIND, per kitne baar bolna hai "JAI HIND"?

    • @iamnoob7593
      @iamnoob7593 Год назад

      @@skullcrusher33 I think he is giving jee advanced exam , And that same time preparing for placement which is after 4 years.

  • @manasranjanmahapatra3729
    @manasranjanmahapatra3729 2 года назад

    UnderStood

  • @punkstAr32
    @punkstAr32 Год назад

    6:06

  • @adityasaxena6971
    @adityasaxena6971 Год назад

    💯💯💯

  • @codingp110
    @codingp110 8 месяцев назад

    US!

  • @prithvigupta8215
    @prithvigupta8215 Год назад

    8:13

  • @godzilla5111
    @godzilla5111 Год назад

    US

  • @Harshit126
    @Harshit126 2 года назад

    Understood, thanks

  • @Lucifer0872
    @Lucifer0872 2 года назад

    ❤️❤️❤️

  • @mamunahmed4832
    @mamunahmed4832 2 года назад

    ❤️❤️❤️❤️❤️❤️❤️❤️

  • @suryakiran2970
    @suryakiran2970 Год назад

    US❤

  • @AshishKumar-ny4cq
    @AshishKumar-ny4cq Год назад

    usus

  • @dixitnakhva6181
    @dixitnakhva6181 Год назад

    Us

  • @roushansingh2945
    @roushansingh2945 Год назад

    us

  • @munagalasaiprasadreddy8581
    @munagalasaiprasadreddy8581 2 года назад

    This is not a dp problem/solution. We are not calculating solution using previous solutions of sub problems

    • @bharathkumar5870
      @bharathkumar5870 2 года назад +2

      heights of rectangles are caluclated from previous row heights,hence dp

    • @gamerversez5372
      @gamerversez5372 Год назад

      Space optimised dp you can say

  • @rishivalaparla7757
    @rishivalaparla7757 2 года назад

    us

  • @mdnayab5839
    @mdnayab5839 Год назад

    WRONG APPROACH
    the approach will fail for below input, its correct output should be 2 but is will give 1
    [1,0]
    [0,1]

    • @vishious14
      @vishious14 Год назад +1

      Would you please explain how maximum area of rectangle is 2 ?

  • @aniketbhura6131
    @aniketbhura6131 2 года назад

    this is my solution which is giving wrong ans .. anyone please correct
    class Solution {
    public:

    int f(vector& heights) {
    int n = heights.size();
    stack st;
    int maxa = 0;
    for(int i=0;i= heights[i]))
    {
    int h = heights[st.top()];
    st.pop();
    int w ;
    if(st.empty() == true) w = i;
    else w = i - st.top() - 1;

    maxa = max(maxa,h*w);

    }
    st.push(i);
    }
    return maxa;
    }

    int maximalRectangle(vector& matrix) {
    int n = matrix.size();
    int m = matrix[0].size();
    vector heights(m,0);
    int ans = 0;
    for(int i=0;i

    • @jaipurite.k_lalit
      @jaipurite.k_lalit 2 года назад +3

      matrix is of char type , just a subtle change if(matrix[i][j]=='1') h++;
      class Solution {
      public:
      int area(vectorheights,int n){
      stackst;
      int maxi=0;
      for(int i=0;i=heights[i])){
      int h=heights[st.top()];
      st.pop();
      int w;
      if(st.empty()){
      w=i;
      }
      else
      w=i-st.top()-1;
      maxi=max(maxi,w*h);
      }
      st.push(i);
      }
      return maxi;
      }
      int maximalRectangle(vector& matrix) {
      int n=matrix.size();

      int m=matrix[0].size();
      int maxi=0;
      vectorstore(m,0);
      for(int i=0;i

    • @Saurav_Kumar514
      @Saurav_Kumar514 2 года назад +1

      here you have to check if(matrix[i][j] == '1') as character 👍

    • @aniketbhura6131
      @aniketbhura6131 2 года назад

      @@Saurav_Kumar514 thanks man

    • @aniketbhura6131
      @aniketbhura6131 2 года назад

      its so good to see people reviewing each other's code.

  • @GeniuslyTensai
    @GeniuslyTensai 2 года назад +2

    The same code is giving wrong answer in leetcode. could someone help
    class Solution {
    public:
    int largestRectangleArea(vector &heights) {
    int n = heights.size();
    stack < int > st;
    int leftsmall[n], rightsmall[n];
    for (int i = 0; i < n; i++) {
    while (!st.empty() && heights[st.top()] >= heights[i]) {
    st.pop();
    }
    if (st.empty())
    leftsmall[i] = 0;
    else
    leftsmall[i] = st.top() + 1;
    st.push(i);
    }
    // clear the stack to be re-used
    while (!st.empty())
    st.pop();
    for (int i = n - 1; i >= 0; i--) {
    while (!st.empty() && heights[st.top()] >= heights[i])
    st.pop();
    if (st.empty())
    rightsmall[i] = n - 1;
    else
    rightsmall[i] = st.top() - 1;
    st.push(i);
    }
    int maxA = 0;
    for (int i = 0; i < n; i++) {
    maxA = max(maxA, heights[i] * (rightsmall[i] - leftsmall[i] + 1));
    }
    return maxA;
    }
    int maximalRectangle(vector& matrix) {
    int n = matrix.size();
    int m = matrix[0].size();
    vector h(m, 0);
    int maxArea = 0;
    for(int i=0; i

  • @VikashYadav-px8ei
    @VikashYadav-px8ei Год назад +1

    Understood 🎉

  • @chiragbansod8252
    @chiragbansod8252 Год назад

    understood

  • @sanjitvyas3043
    @sanjitvyas3043 Год назад

    Understood

  • @tusharrustagi5729
    @tusharrustagi5729 2 года назад

    understood

  • @vivekpawar311
    @vivekpawar311 2 года назад

    Understood!

  • @amankrsingh
    @amankrsingh 2 года назад

    Understood

  • @arnabroy2995
    @arnabroy2995 Год назад

    US❤️

  • @DeepSinghC
    @DeepSinghC Год назад

    us

  • @abhinanda7049
    @abhinanda7049 10 месяцев назад

    understood

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx Год назад

    Understood

  • @robertocamberos3969
    @robertocamberos3969 2 года назад

    Understood!

  • @adarshmishra7113
    @adarshmishra7113 2 года назад

    understood

  • @arunsadhasivam
    @arunsadhasivam 2 года назад

    Understood

  • @harshitjaiswal9439
    @harshitjaiswal9439 10 месяцев назад

    understood

  • @srinathv1412
    @srinathv1412 Год назад

    Understood

  • @rishabhgupta9846
    @rishabhgupta9846 2 года назад

    understood