LeetCode simple solution - 221 (Java lets you solve a problem in one minute)

Description of the topic: Given a matrix of two-dimensional 0,1 strings, find the region of the largest square with only one string.
E.g:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

The answer is 4.

Problem-solving ideas: dynamic planning.
Dp[i][j] is the side length of the largest square with (i, j) as the vertex of the lower right corner.

The matrix is ​​scanned from left to right and top to bottom, when found:
1. (i, j), (i-1, j), (i, j-1) When all three positions are 1, the value of (i, j) can be some size above the left side of this value. The regions form a matrix. E.g:

1 0 1 0 0
1 0 1 1 1
1 1 1 1

Next scan (2, 4) and find that (i, j), (i-1, j), (i, j-1) are all three positions, then the value of (i, j) can be combined with five One 1 constitutes a matrix.
But the problem requires the size of the square, so we must find the minimum value min in dp(i-1, j-1), dp(i-1, j), dp(i, j-1), this minimum The value is the minimum side length of the three squares whose vertices are the lower right corner, and the value of (i, j) is scanned and we find that it can be added to a larger matrix, so the smallest square after this complement The side length must be increased by 1, so let dp(i, j) = min + 1.
The reason for finding the minimum value of dp(i-1, j-1), dp(i-1, j), dp(i, j-1) is because the complement is a matrix, and the width of the matrix Limits the side length of the square.
2. (i, j), (i-1, j), (i, j-1) When one of the three positions is 0, it is definitely impossible to make up a rectangle, so dp(i, j) = 0.

During the scan, the variable res records the length of the side of the largest square found.

The final code is as follows:

Class SoluTIon {

Public:

Int maximalSquare(vector"vector"char" & matrix) {

If(matrix.size() == 0) return 0;

Int row = matrix.size(), col = matrix[0].size();

Int res = 0;

Vector "int" dp(col, 0);

//first row

For(int i = 0;i " col;i++) dp[i] = matrix[0][i] - '0';

Res = max(res, *max_element(dp.begin(), dp.end()));

// dp

For(int i = 1;i "row;i++) {

// first col

Dp[0] = matrix[i][0] - '0';

// other cols

For(int j = 1;j "col;j++) {

If(matrix[i][j] == '1') {

Int temp = min(dp[j], dp[j-1]);

Dp[j] = temp + (matrix[i-temp][j-temp] - '0');

} else {

Dp[j] = 0;

}

}

Res = max(res, *max_element(dp.begin(), dp.end()));

}

Return res * res;

}

};

Metal Cable Management

Organize your cabling

  • Organizes all types of cables
  • Ideal for network Patch Panel cabling
  • Finger duct design allows easy cable organization
  • Cable pass-through holes in the back of the duct
  • Only set up 1U rack
  • Work with threaded, round and square hole racks
  • EIA 19" RACK standard compliable
  • Including screws and mounting hardware

Metal Cable Management,1U Metal cable management,stainless steel cable management,metal cable tie mount

NINGBO UONICORE ELECTRONICS CO., LTD , https://www.uonicore.com