[LeetCode 毎日の質問] - 85. 最大の長方形

[トピックカテゴリ] 1 つ

  • マトリックス

2【問題難易度】

  • 困難

3 [トピック番号]

  • 85. 最大の長方形

4 【タイトル説明】

  • 0 と 1 のみを含む行 x 列サイズの 2D バイナリ行列が与えられた場合、1 のみを含む最大の長方形を見つけて、その面積を返します。

5 【トピック例】

  • 例 1:

    • ここに画像の説明を挿入
    • 入力: 行列 = [["1","0","1","0","0"],["1","0","1","1","1"],[ "1", "1", "1", "1", "1"], ["1", "0", "0", "1", "0"]]
    • 出力: 6
    • 説明: 最大の長方形が上の図に示されています。
  • 例 2:

    • 入力: 行列 = []
    • 出力: 0
  • 例 3:

    • 入力: 行列 = [["0"]]
    • 出力: 0
  • 例 4:

    • 入力: 行列 = [["1"]]
    • 出力: 1
  • 例 5:

    • 入力: 行列 = [["0","0"]]
    • 出力: 0

6 [トピックプロンプト]

  • 行 = = 行列 。行の長さ == 行列.長さ_ _==マトリックス_ _ _ _ _ _ _ _
  • 列 = = 行列 [0] 。長さの列 == 行列[0].長さコルス_ _==行列[ 0 ] _ _ _ _ _ _ _ _
  • 1 < = 行、列 < = 200 1 <= 行、列 <= 2001<=_コルス_ _<=200
  • 行列 [ i ] [ j ] は ' 0 ' または ' 1 ' です行列 [i][j] は '0' または '1' ですmat r i x [ i ] [ j ]_ 0」'または 1」

7つの【問題解決アイデア】

  • まず、各位置の左側に連続する「1」の数を記録する補助配列 left を作成します。
  • 次に、2次元配列の各点について、この点を右下隅として長方形の面積を計算し、「上方拡張」方法を使用します。行列の幅は、その中で最も短い幅になります。 「上方向への拡張」の処理で、現在の高さが渡されます。 位置からトラバースした位置を減算し、1を加算します(配列は0からカウントし始めるため)
  • 次に、最大値を比較して、最大の長方形の面積を取得します。
  • 最後に結果を返します

8【時間周波数】

  • 時間計算量: O ( m 2 n ) O(m^2n)O ( m2 n)m 、 nm、nmnはそれぞれ、受信する 2 次元配列の行数と列数です。
  • 空間複雑さ: O ( mn ) O(mn)O ( mn )m、nm、nmnはそれぞれ、受信する 2 次元配列の行数と列数です。

9 [コード実装]

  1. Java言語バージョン
class Solution {
    
    
    public int maximalRectangle(char[][] matrix) {
    
    
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] left = new int[m][n];
        for(int i = 0;i < m;i++){
    
    
            for(int j = 0;j < n;j++){
    
    
                if(matrix[i][j] == '1'){
    
    
                    left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
                }
            }
        }
        int res = 0;
        for(int i = 0;i < m;i++){
    
    
            for(int j = 0;j < n;j++){
    
    
                if(matrix[i][j] == '0'){
    
    
                    continue;
                }
                int width = left[i][j];
                int area = width;
                for(int k = i - 1;k >= 0;k--){
    
    
                    width = Math.min(width, left[k][j]);
                    area = Math.max(area,(i - k + 1) * width);
                }
                res = Math.max(res, area);
            }
        }
        return res;
    }
}
  1. C言語版
int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize)
{
    
    
    int m = matrixSize;
    int n = matrixColSize[0];
    int** left = (int **)malloc(sizeof(int*) * m);
    for(int i = 0;i < m;i++)
    {
    
    
        left[i] = (int*)calloc(n, sizeof(int));
    }
    for(int i = 0;i < m;i++)
    {
    
    
        for(int j = 0;j < n;j++)
        {
    
    
            if(matrix[i][j] == '1')
            {
    
    
                left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
            }
        }
    }
    int res = 0;
    for(int i = 0;i < m;i++)
    {
    
    
        for(int j = 0;j < n;j++)
        {
    
    
            if(matrix[i][j] == '0')
            {
    
    
                continue;
            }
            int width = left[i][j];
            int area = width;
            for(int k = i - 1;k >= 0;k--)
            {
    
    
                width = fmin(width, left[k][j]);
                area = fmax(area, (i - k + 1) * width);
            }
            res = fmax(res, area);
        }
    }
    return res;
}
  1. Python言語バージョン
class Solution:
    def maximalRectangle(self, matrix: List[List[str]]) -> int:
        m = len(matrix)
        n = len(matrix[0])
        left = [[0 for _ in range(n)] for _ in range (m)]
        for i in range(0, m):
            for j in range(0, n):
                if matrix[i][j] == '1':
                    left[i][j] = (0 if j == 0 else left[i][j - 1]) + 1
        res = 0
        for i in range(0, m):
            for j in range(0, n):
                if matrix[i][j] == '0':
                    continue
                width = left[i][j]
                area = width
                for k in range(i - 1, -1, -1):
                    width = min(width, left[k][j])
                    area = max(area, (i - k + 1) * width)
                res = max(res, area)
        return res
  1. C++言語バージョン
class Solution {
    
    
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
    
    
        int m = matrix.size();
        int n = matrix[0].size();
        vector<vector<int>> left(m, vector<int>(n, 0));
        for(int i = 0;i < m;i++){
    
    
            for(int j = 0;j < n;j++){
    
    
                if(matrix[i][j] == '1'){
    
    
                    left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
                }
            }
        }
        int res = 0;
        for(int i = 0;i < m;i++){
    
    
            for(int j = 0;j < n;j++){
    
    
                if(matrix[i][j] == '0'){
    
    
                    continue;
                }
                int width = left[i][j];
                int area = width;
                for(int k = i - 1;k >= 0;k--){
    
    
                    width = fmin(width, left[k][j]);
                    area = fmax(area, (i - k + 1) * width);
                }
                res = fmax(res, area);
            }
        }
        return res;
    }
};

10【投稿結果】

  1. Java言語バージョン
    ここに画像の説明を挿入

  2. C言語版
    ここに画像の説明を挿入

  3. Python言語バージョン
    ここに画像の説明を挿入

  4. C++言語バージョン
    ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/IronmanJay/article/details/132143936