Tuesday, December 18, 2018

Find if there is a rectangle in binary matrix with corners as 1

Here is the algorithm to work on.

The brute force solution:

rectangle top left, (rowStart, columnStart), rectangle bottom down (rowEnd, columnEnd), the time complexity is O(Rows * Rows * Columns * Columns).

It is a not good solution for very sparse matrix.

One thing is to brute force all element in matrix with value 1. First spend O(Rows * Columns) to put all nodes with value 1 into hashSet.

Now the time complexity can be simplified to O(M * (M - 1)), M is total number of elements with value 1.

Here is my practice.


No comments:

Post a Comment