Created
August 1, 2017 18:31
-
-
Save jianminchen/7d8d076b5d6effe1a8684a0cb78d24dc to your computer and use it in GitHub Desktop.
Study Leetcode 85 C# solution, rewrite the code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Leetcode85_MaximumRectangle | |
{ | |
/// <summary> | |
/// code review: | |
/// August 1, 2017 | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var matrix = new char[,] { | |
{'1','0','1','0','0'}, | |
{'1','0','1','1','1'}, | |
{'1','1','1','1','1'}, | |
{'1','0','0','1','0'} | |
}; | |
var result = MaximalRectangle(matrix); | |
Debug.Assert(result == 6); | |
} | |
/// <summary> | |
/// code review on August 1, 2017 | |
/// Try to figure out the idea in the following: | |
/// height counts the number of successive '1's above (plus the current one). | |
/// The value of left & right means the boundaries of the rectangle which | |
/// contains the current point with a height of value height. | |
/// </summary> | |
/// <param name="matrix"></param> | |
/// <returns></returns> | |
public static int MaximalRectangle(char[,] matrix) | |
{ | |
var rows = matrix.GetLength(0); | |
var columns = matrix.GetLength(1); | |
var maximumRectangle = 0; | |
//[i, 0] is left; [i, 1] is right; [i, 2] is height | |
var threeMetrics = new int[columns, 3]; | |
for (int i = 0; i < columns; i++) | |
{ | |
threeMetrics[i, 1] = columns; | |
} | |
for (int row = 0; row < rows; row++) | |
{ | |
var currentLeft = 0; | |
var currentRight = columns; | |
for (int col = 0; col < columns; col++) | |
{ | |
var visit = matrix[row, col]; | |
bool isOne = visit == '1'; | |
// compute height (can do this from either side) | |
threeMetrics[col, 2] = isOne ? (threeMetrics[col, 2] + 1) : 0; | |
if (isOne) // compute left (from left to right) | |
{ | |
threeMetrics[col, 0] = Math.Max(threeMetrics[col, 0], currentLeft); | |
} | |
else | |
{ | |
threeMetrics[col, 0] = 0; | |
currentLeft = col + 1; | |
} | |
} | |
// compute right (from right to left) | |
for (int col = columns - 1; col >= 0; col--) | |
{ | |
var visit = matrix[row, col]; | |
bool isOne = visit == '1'; | |
if (isOne) | |
{ | |
threeMetrics[col, 1] = Math.Min(threeMetrics[col, 1], currentRight); | |
} | |
else | |
{ | |
threeMetrics[col, 1] = columns; | |
currentRight = col; | |
} | |
} | |
// compute the area of rectangle (can do this from either side) | |
for (int column = 0; column < columns; column++) | |
{ | |
var width = threeMetrics[column, 1] - threeMetrics[column, 0]; | |
var height = threeMetrics[column, 2]; | |
maximumRectangle = Math.Max(maximumRectangle, width * height); | |
} | |
} | |
return maximumRectangle; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment