Hard-题目30:85. Maximal Rectangle

本文介绍了解决LeetCode上第85题“最大矩形”的算法思路,通过将问题转化为直方图最大面积问题,并使用动态规划方法求解。提供了一段高效的Java代码实现。

题目原文:
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area.
题目大意:
给出一个0-1矩阵,求出最大的全1矩阵。
题目分析:
(这也是原创的)朴素解法就是dfs,复杂度貌似为 O(4n) 就不谈了。
然后考虑DP,注意本题在Leetcode的题号是85题,好眼熟啊。刚才做到Hard-题目27:84. Largest Rectangle in Histogram是求直方图的最大面积。好像找到了什么关联对不对?没错,我们可以把这个0-1矩阵的第i行以上的子矩阵看做一个直方图,那么求每个子矩阵的直方图的面积最大值的最大值(好绕,看懂了么)就是题目的要求啦。
那么就好办了,直接调用Hard第27题的代码就行了。
源码:(language:java)
public class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;

    int[] height = new int[matrix[0].length];
    for(int i = 0; i < matrix[0].length; i ++){
        if(matrix[0][i] == '1') height[i] = 1;
    }
    int result = largestInLine(height);
    for(int i = 1; i < matrix.length; i ++){
        resetHeight(matrix, height, i);
        result = Math.max(result, largestInLine(height));
    }

    return result;
}

private void resetHeight(char[][] matrix, int[] height, int idx){
    for(int i = 0; i < matrix[0].length; i ++){
        if(matrix[idx][i] == '1') height[i] += 1;
        else height[i] = 0;
    }
}    

public int largestInLine(int[] height) {
    if(height == null || height.length == 0) return 0;
    int len = height.length;
    Stack<Integer> s = new Stack<Integer>();
    int maxArea = 0;
    for(int i = 0; i <= len; i++){
        int h = (i == len ? 0 : height[i]);
        if(s.isEmpty() || h >= height[s.peek()]){
            s.push(i);
        }else{
            int tp = s.pop();
            maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
            i--;
        }
    }
    return maxArea;
}

}
成绩:
27ms,64.32%,32ms,8.41%

{ "creatingLibrary":{"name":"MediaInfoLib","version":"25.03","url":"https://mediaarea.net/MediaInfo"}, "media":{"@ref":"C:\\Users\\Administrator\\Desktop\\施工方.mp4","track":[{"@type":"General","VideoCount":"1", "AudioCount":"1", "FileExtension":"mp4", "Format":"MPEG-4", "Format_Profile":"Base Media", "CodecID":"isom", "CodecID_Compatible":"isom/iso2/avc1/mp41", "FileSize":"109103515", "Duration":"15420.321", "OverallBitRate":"56602", "FrameRate":"30.000", "HeaderSize":"628191", "DataSize":"108475316", "FooterSize":"8", "IsStreamable":"Yes", "Encoded_Date":"2025-08-26 09:50:28 UTC", "Tagged_Date":"2025-08-26 09:48:50 UTC", "File_Created_Date":"2025-09-08 12:11:59.812 UTC", "File_Created_Date_Local":"2025-09-08 20:11:59.812", "File_Modified_Date":"2025-08-28 14:05:15.347 UTC", "File_Modified_Date_Local":"2025-08-28 22:05:15.347", "extra":{"IsTruncated":"Yes","ConformanceErrors":[{"moov":[{"trak":[{"mdia":[{"minf":[{"GeneralCompliance":"Element size 277283 is more than maximal permitted size 277253 (offset 0x185) / Element size 350209 is more than maximal permitted size 350179 (offset 0x43D99)","stbl":[{"stco":[{"GeneralCompliance":"Element size 69308 is more than maximal permitted size 69248 (offset 0x32DEC) / Element size 69308 is more than maximal permitted size 69302 (offset 0x886A8)"}]}],"_0x067FEE3D":[{"GeneralCompliance":"Element size 109046617 is more than maximal permitted size 22 (offset 0x43C74)"}],"Unknown":[{"GeneralCompliance":"Element size 3313632888 is more than maximal permitted size 22 (offset 0x99566)"}]}]}],"_0x9DAD0680_":[{"GeneralCompliance":"Element size 1750861432 is more than maximal permitted size 22 (offset 0x43C92)"}],"_0x001C7362_":[{"GeneralCompliance":"Element size 4294901752 is more than maximal permitted size 22 (offset 0x99584)"}]}]}],"_0x01010101_":[{"GeneralCompliance":"Element size 16843001 is more than maximal permitted size 0 (offset 0x680C99B)"}],"MPEG4_":[{"GeneralCompliance":"File size 109103515 is less than expected size 125946516 (offset 0x680C99B)"}]}]}},{"@type":"Video","StreamOrder":"0", "ID":"1", "Format":"AVC", "Format_Profile":"High", "Format_Level":"3.2", "Format_Settings_CABAC":"Yes", "Format_Settings_RefFrames":"4", "CodecID":"avc1", "Duration":"15420.321", "Source_Duration":"577.500", "BitRate_Maximum":"1374687", "Width":"720", "Height":"1372", "Stored_Height":"1376", "Sampled_Width":"720", "Sampled_Height":"1372", "PixelAspectRatio":"1.000", "DisplayAspectRatio":"0.525", "Rotation":"0.000", "FrameRate_Mode":"CFR", "FrameRate_Mode_Original":"VFR", "FrameRate":"30.000", "FrameRate_Num":"30", "FrameRate_Den":"1", "FrameCount":"462610", "ColorSpace":"YUV", "ChromaSubsampling":"4:2:0", "BitDepth":"8", "ScanType":"Progressive", "Source_StreamSize":"99235221", "Encoded_Library":"bvc0ot v2.2.1.3-20250220", "Encoded_Library_Name":"bvc0ot v2.2.1.3-20250220", "Default":"No", "Encoded_Date":"2025-08-26 09:50:28 UTC", "Tagged_Date":"2025-08-26 09:48:50 UTC", "colour_description_present":"Yes", "colour_description_present_Source":"Container / Stream", "colour_range":"Limited", "colour_range_Source":"Container / Stream", "colour_primaries":"BT.709", "colour_primaries_Source":"Container / Stream", "transfer_characteristics":"BT.709", "transfer_characteristics_Source":"Container / Stream", "matrix_coefficients":"BT.709", "matrix_coefficients_Source":"Container / Stream", "extra":{"mdhd_Duration":"15420322","CodecConfigurationBox":"avcC"}},{"@type":"Audio","StreamOrder":"1", "ID":"2", "Format":"AAC", "Format_AdditionalFeatures":"LC", "CodecID":"mp4a-40-2", "Duration":"15420.321", "Source_Duration":"577.503", "Source_Duration_LastFrame":"-0.000", "BitRate_Mode":"CBR", "BitRate_Nominal":"128000", "BitRate_Maximum":"128010", "Channels":"2", "ChannelPositions":"Front: L R", "ChannelLayout":"L R", "SamplesPerFrame":"1024", "SamplingRate":"44100", "SamplingCount":"680036156", "FrameRate":"43.066", "FrameCount":"664098", "Source_FrameCount":"24871", "Compression_Mode":"Lossy", "Source_StreamSize":"9240087", "Default":"No", "AlternateGroup":"1", "Encoded_Date":"2025-08-26 09:50:28 UTC", "Tagged_Date":"2025-08-26 09:48:50 UTC", "extra":{"mdhd_Duration":"15420321"}}]} } 一致性错误 : 10 moov : Yes trak : Yes mdia : Yes minf : Yes 一般合规性 : Element size 277283 is more than maximal permitted size 277253 (offset 0x185) / Element size 350209 is more than maximal permitted size 350179 (offset 0x43D99) stbl : Yes stco : Yes 一般合规性 : Element size 69308 is more than maximal permitted size 69248 (offset 0x32DEC) / Element size 69308 is more than maximal permitted size 69302 (offset 0x886A8) 0x067FEE3D : Yes 一般合规性 : Element size 109046617 is more than maximal permitted size 22 (offset 0x43C74) ÇÛ : Yes 一般合规性 : Element size 3313632888 is more than maximal permitted size 22 (offset 0x99566) 0x9DAD0680 : Yes 一般合规性 : Element size 1750861432 is more than maximal permitted size 22 (offset 0x43C92) 0x001C7362 : Yes 一般合规性 : Element size 4294901752 is more than maximal permitted size 22 (offset 0x99584) 0x01010101 : Yes 一般合规性 : Element size 16843001 is more than maximal permitted size 0 (offset 0x680C99B) MPEG-4 : Yes 一般合规性 : File size 109103515 is less than expected size 125946516 (offset 0x680C99B) 怎么做到 的 我要造成这样错误 给个详细的方法
09-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值