## Sunday, 13 June 2021

### Java Program To Solve Maximum Size Rectangle In Binary Matrix

Given a binary matrix, find max rectangle with all 1's.

``````A : [  1 1 1 0
0 1 1 0
1 1 1 0
]

Output : 6``````

Dynamic programming can help us here. For this, we can use maximum size rectangle in a histogram as a sub-problem. The idea is for each row we will calculate the histogram. If current [row][column] value is 0, then current histogram [column] value will become 0. If current [row][column] value is 1, then histogram [column] value will be incremented by 1. Then we will apply max rectangle histogram sub-problem & check for maximum area within the histogram.

The time complexity of this Java solution will be O(n * m) where n is the size of rows & m is the size of columns. We have a nested loop. For each traversal of outer loop, we do m traversal to calculate the current histogram & then we find the largest rectangle in the histogram in O(m) time complexity. So basically it is n * (m + m) which is O(n*m).

Space complexity is O(m) as we need m space for tracking current histogram & another m space used by stack for calculating largest rectangle within a histogram.

Here is the Java implementation to find the largest area of a binary matrix with all ones.

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59``` ```import java.util.*; import java.lang.*; import java.io.*; class Solution { public int maximalRectangle(ArrayList> A) { List result = new ArrayList(); for(int i=0; i currentRow = A.get(i); for(int j=0; j result, int maxArea) { Stack stack = new Stack(); int i = 0; for(; i maxArea){ maxArea = currentArea; } } } while(!stack.isEmpty()){ int index = stack.pop(); int leftBound = !stack.empty() ? stack.peek() : -1; int rightBound = i; int currentArea = (rightBound - leftBound - 1) * result.get(index); if(currentArea > maxArea){ maxArea = currentArea; } } return maxArea; } } ```

## Saturday, 5 June 2021

### Java Program To Solve Word Break Problem

Given a string A and a dictionary of words B, find out if A can be segmented into a space-separated sequence of dictionary words.

``````Input:
A = "myinterviewtrainer",
B = ["trainer", "my", "interview"]

Output:
1 ("myinterviewtrainer" can be segmented as "my interview trainer")

Input:
A = "cc"
B = ["ccc"]

Output:
0 ("cc" can't be segmented as "ccc")``````
Word break problem can be solved using dynamic programming. We solved the problem in bottom up manner. The result array contains n+1 elements where n is length of A. The base case of empty string ("") is true. Then we begin with the first letter of A & loop till end. There is one inner loop, so time complexity is O(n^2). I am not considering time complexity of finding the substring in List B. It can be converted to Map & find Operation can be done in O(1).

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22``` ```import java.util.*; import java.lang.*; import java.io.*; class Solution { public int wordBreak(String A, ArrayList B) { int[] result = new int[A.length() + 1]; //for empty string wordbreak is true result = 1; for(int i=0; i

### Java Program To Solve Maximum Size Rectangle In Binary Matrix

Given a binary matrix, find max rectangle with all 1's. A : [ 1 1 1 0 0 1 1 0 1 1 1 0 ] Output : 6 Dynamic programmi...