for (inti=0; i < s.length(); i++) { if (s.charAt(i) == '(') { stack.push(i); } else { if (!stack.isEmpty()) { // find a match if (s.charAt(stack.peek()) == '(') { stack.pop(); } else { stack.push(i); } } else { stack.push(i); } } }
// now stack contains the indices of characters which cannot be matched
if (stack.isEmpty()) { max = s.length(); } else { intright= s.length();
while (!stack.isEmpty()) { intleft= stack.pop();
// [left+1, right) is matched max = Math.max(max, right - (left + 1));
right = left; }
// [0, right) is matched max = Math.max(max, right); } return max; } }
2 Question-84[★★★★★]
Largest Rectangle in Histogram
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.