L13. Maximal Rectangle | Stack and Queue Playlist

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

КОМЕНТАРІ •

  • @KeerthiReddyKolan
    @KeerthiReddyKolan 6 місяців тому +17

    Most of the times(mostly when I see a tough question), I see your video and I exclaim "WOWWW!" and hit the like button.
    Great work!
    Thanks a ton for your videos.

  • @amanpreetsinghbawa1600
    @amanpreetsinghbawa1600 Місяць тому +2

    The visualisation of this question was the key cracking point here, I couldn't think about it

  • @AkshitChaudhary-vx8iw
    @AkshitChaudhary-vx8iw Місяць тому +3

    if you don't want to use Prefix Sum for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[0].length; j++) {

    if (matrix[i][j] == '1') {
    arr[j]++;
    } else {
    arr[j] = 0;
    }
    }

    maxArea = Math.max(maxArea, largestRectangleArea(arr));
    }

  • @srilekhaig2442
    @srilekhaig2442 29 днів тому +2

    great work sir, thanks a tonn❤, i watch your videos on a regular basis along with my homie, honestly she has such a huge ass crush on you lmao.

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

    00:04 Maximal Rectangle problem
    01:39 Concept of largest rectangle in histogram
    03:09 Understanding maximal rectangle in histograms through example
    04:40 Optimizing complexity using prefix sum concept
    06:04 Understanding and visualizing histogram bar heights
    07:33 Prefix sum computation for histogram bar preparation
    08:59 Compute maximal rectangle area using stack and queue
    10:34 Time complexity analysis for computing maximal rectangle area.

  • @kunal5077
    @kunal5077 3 місяці тому +2

    Very nice explanation Striver. Eagerly waiting for strings

  • @hashcodez757
    @hashcodez757 6 місяців тому +7

    Understood Bhaiya!!

  • @Rahul-jy9wg
    @Rahul-jy9wg 5 місяців тому +3

    Sometimes you just need a hint to solve a question, in this question as soon as strive said that largest rectangle in the histogram, I paused the video and solve the question myself.

  • @LightYagami406
    @LightYagami406 Місяць тому

    finally got the question thanks for the help

  • @shwetachoudhary9003
    @shwetachoudhary9003 День тому

    superb 🎉

  • @aadfasdf2
    @aadfasdf2 5 місяців тому +4

    class Solution {
    public:
    int maxInHistogram(vector& arr) {
    int n = arr.size();
    int maxi = 0;
    stack st;
    arr.push_back(0);
    for (int i = 0; i < arr.size(); ++i) {
    while (!st.empty() && arr[st.top()] >= arr[i]) {
    int height = arr[st.top()];
    st.pop();
    int width = st.empty() ? i : i - st.top() - 1;
    maxi = max(maxi, height * width);
    }
    st.push(i);
    }
    return maxi;
    }
    int maximalRectangle(vector& matrix) {
    int r = matrix.size();
    if(r == 0) return 0;
    int c = matrix[0].size();
    vector ps(c,0);
    int maxi = 0;
    for(int i = 0; i < r; ++i){
    for(int j = 0; j < c; ++j){
    if(matrix[i][j] == '1') ps[j]++;
    if(matrix[i][j] == '0') ps[j] = 0;
    }
    maxi = max(maxi, maxInHistogram(ps));
    }
    return maxi;
    }
    };
    //arr[i] * (nse - pse - 1)

  • @iamnoob7593
    @iamnoob7593 Місяць тому

    superb , Thank you

  • @AdityaJain-ed9my
    @AdityaJain-ed9my 27 днів тому

    Thank You sirr

  • @prajwalkambale8812
    @prajwalkambale8812 Місяць тому +2

    I think the space complexity should be O(n*m) + O(n*m) as we are using the stack of size m, n times.... Correct me if I am wrong.

  • @pooranik5342
    @pooranik5342 3 місяці тому

    Thank you so much.Understood Bhaiya

  • @subee128
    @subee128 6 місяців тому +1

    Thanks

  • @Kshitijsingh-n7f
    @Kshitijsingh-n7f 6 місяців тому +4

    space should be O(n*m) +O(m) na ? why O(n) can anyone explain?

    • @maverick_8707
      @maverick_8707 5 місяців тому +1

      Right 👍

    • @oyeshxrme
      @oyeshxrme 3 місяці тому +1

      yup

    • @dailymix05
      @dailymix05 2 місяці тому

      coz, running a loop for n elements (row) that's why O(N)

    • @srilekhaig2442
      @srilekhaig2442 29 днів тому +1

      O(n) for using stack in Largesthistogram function. okay?

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

    UNDERSTOOD;

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

    Thanks a lot

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

    Understood

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

    wowwwwwww Understood

  • @SubhashB22CH036
    @SubhashB22CH036 5 місяців тому +3

    cpp solution
    class Solution {
    public:
    int maximalRectangle(vector& matrix) {
    int n = matrix.size();
    int m = matrix[0].size();
    int maxiarea = 0;
    vector presum(n, vector(m, 0));
    for (int j = 0; j < m; j++) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
    if (matrix[i][j] == '1') {
    sum += 1;
    } else {
    sum = 0;
    }
    presum[i][j] = sum;
    }
    }
    for (int i = 0; i < n; i++) {
    maxiarea = max(maxiarea, lhist(presum[i]));
    }
    return maxiarea;
    }
    int lhist(vector& heights) {
    stack st;
    int max_area = 0;
    int n = heights.size();

    for (int i = 0; i

  • @valendradangi1822
    @valendradangi1822 5 місяців тому +1

    We don't actually need to create the sum variable it can also be done as
    for (int c = 0; c < m; c++)
    {
    for (int r = 0; r < n; r++)
    {
    if (matrix[r][c] == '1')
    {
    if (r != 0)
    dp[r][c] = dp[r - 1][c] + 1;
    else
    dp[r][c] = 1;
    }
    else
    dp[r][c] = 0;
    }
    }

  • @ToonDubberDuo
    @ToonDubberDuo 3 місяці тому +1

    very hard

  • @tanujasharma1262
    @tanujasharma1262 3 місяці тому

    What is the song in the end

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

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

  • @abhinavabhi3568
    @abhinavabhi3568 Місяць тому

    Understood

  • @anupkhismatrao9280
    @anupkhismatrao9280 2 місяці тому