Created
August 1, 2017 19:24
-
-
Save jianminchen/9b51ba17cd99f88ae481a9c9e87a0360 to your computer and use it in GitHub Desktop.
Leetcode 85 - maximal rectangle - still work on currentLeft, currentRight's update, think about how to make it more easy to follow
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]; | |
// initialization - understand the tip - why to set as columns? | |
for (int i = 0; i < columns; i++) | |
{ | |
threeMetrics[i, 1] = columns; | |
} | |
for (int row = 0; row < rows; row++) | |
{ | |
var currentLeft = 0; | |
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; | |
// compute left (from left to right) | |
if (isOne) | |
{ | |
threeMetrics[col, 0] = Math.Max(threeMetrics[col, 0], currentLeft); | |
} | |
else | |
{ | |
threeMetrics[col, 0] = 0; | |
currentLeft = col + 1; | |
} | |
} | |
// compute right (from right to left) | |
var currentRight = columns; | |
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