858. Mirror Reflection
There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered 0, 1, and 2.
The square room has walls of length p and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.
Given the two integers p and q, return the number of the receptor that the ray meets first.
The test cases are guaranteed so that the ray will meet a receptor eventually.
Example 1:
Input: p = 2, q = 1
Output: 2
Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.
Solution:
class Solution {
public int mirrorReflection(int p, int q) {
while (p % 2 == 0 && q % 2 == 0){
p = p >> 1;
q = q >> 1;
}
if (p % 2 == 0) return 2;
if (q % 2 == 0) return 0;
return 1;
}
}
84. Largest Rectangle in Histogram
Given an array of integers heights representing the histogram’s bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
Example 1:
Input: heights = [2,1,5,6,2,3]
Output: 10
Explanation: The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.
Solution:
class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> stack = new Stack<>();
int max = 0, last = -1, pop = 0, n = heights.length;
int[] res = new int[n];
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && heights[stack.peek()] > heights[i]) {
pop = stack.pop();
if (last == -1) {
res[pop] += heights[pop];
last = pop;
} else {
res[pop] += heights[pop] * (last - pop + 1);
}
}
last = -1;
stack.push(i);
}
while (!stack.isEmpty()){
pop = stack.pop();
res[pop] += heights[pop] * (n - pop);
}
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && heights[stack.peek()] > heights[i]) {
pop = stack.pop();
if (last != -1) {
res[pop] += heights[pop] * (pop - last);
}else{
last = pop;
}
}
last = -1;
stack.push(i);
}
while (!stack.isEmpty()){
pop = stack.pop();
res[pop] += heights[pop] * pop;
}
for (int r: res){
max = Math.max(r, max);
}
return max;
}
}
85. Maximal Rectangle
Given a rows x cols binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.
Example 1:
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 6
Explanation: The maximal rectangle is shown in the above picture.
Solution:
class Solution {
public int maximalRectangle(char[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[][] heights = new int[m][n];
int res = 0, max = 0;
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
if (i == 0) heights[i][j] = matrix[i][j] - '0';
else heights[i][j] = (matrix[i][j] == '0' ? 0 : (heights[i - 1][j] + 1));
}
}
for (int i = 0; i < m; i++){
res = largestRectangleArea(heights[i]);
max = Math.max(res, max);
}
return max;
}
public int largestRectangleArea(int[] heights) {
Stack<Integer> stack = new Stack<>();
int max = 0, last = -1, pop = 0, n = heights.length;
int[] res = new int[n];
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && heights[stack.peek()] > heights[i]) {
pop = stack.pop();
if (last == -1) {
res[pop] += heights[pop];
last = pop;
} else {
res[pop] += heights[pop] * (last - pop + 1);
}
}
last = -1;
stack.push(i);
}
while (!stack.isEmpty()){
pop = stack.pop();
res[pop] += heights[pop] * (n - pop);
}
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && heights[stack.peek()] > heights[i]) {
pop = stack.pop();
if (last != -1) {
res[pop] += heights[pop] * (pop - last);
}else{
last = pop;
}
}
last = -1;
stack.push(i);
}
while (!stack.isEmpty()){
pop = stack.pop();
res[pop] += heights[pop] * pop;
}
for (int r: res){
max = Math.max(r, max);
}
return max;
}
}
Think:
Based on the LC 84. Largest Rectangle in Histogram
. We can consider this problem as the extension of it. Transform the matrix to 2-D Histogram, then use the function in LC 84.
Based on the example above, we get a matrix like:
['1', '0', '1', '0', '0']
['1', '0', '1', '1', '1']
['1', '1', '1', '1', '1']
['1', '0', '0', '1', '0']
Transform to:
[1, 0, 1, 0, 0]
[2, 0, 2, 1, 1]
[3, 1, 3, 2, 2]
[4, 0, 0, 3, 0]
Each of row can be the histogram, therefore the answer is the maximum.
91. Decode Ways
A message containing letters from A-Z can be encoded into numbers using the following mapping:
'A' -> "1"
'B' -> "2"
...
'Z' -> "26"
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, “11106” can be mapped into:
"AAJF"
with the grouping (1 1 10 6)
"KJF"
with the grouping (11 10 6)
Note that the grouping (1 11 06) is invalid because "06"
cannot be mapped into 'F'
since "6"
is different from "06"
.
Given a string s containing only digits, return the number of ways to decode it.
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: s = "12"
Output: 2
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
Solution:
class Solution {
public int numDecodings(String s) {
int n = s.length();
int[] res = new int[n];
HashSet<String> valid = new HashSet<>();
for (int i = 1; i <= 26; i++){
valid.add(Integer.toString(i));
}
// base case
if (n < 2){
return valid.contains(s) ? 1 : 0;
}
res[0] = valid.contains(s.substring(0, 1)) ? 1 : 0;
res[1] = valid.contains(s.substring(1, 2)) ? 1 : 0;
if (valid.contains(s.substring(0, 2))) res[1]++;
if (res[0] == 0 || res[1] == 0) return 0;
for (int i = 2; i < n; i++){
if (valid.contains(s.substring(i, i + 1)) || valid.contains(s.substring(i - 1, i + 1))) {
if (valid.contains(s.substring(i, i + 1))) res[i] += res[i - 1];
if (valid.contains(s.substring(i - 1, i + 1))) res[i] += res[i - 2];
}else{
return 0;
}
}
return res[n - 1];
}
}
95. Unique Binary Search Trees II
Given an integer n, return all the structurally unique BST’s (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order.
Example 1:
Input: n = 3
Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
Solution:
class Solution {
public List<TreeNode> generateTrees(int n) {
return subTrees(1, n);
}
private List<TreeNode> subTrees(int start, int end){
List<TreeNode> res = new ArrayList();
if(start>end) {
res.add(null);
return res;
}
for(int i=start; i<=end; i++){
List<TreeNode> left = subTrees(start, i-1);
List<TreeNode> right = subTrees(i+1, end);
for(TreeNode l: left){
for(TreeNode r: right){
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
res.add(root);
}
}
}
return res;
}
}