modify code

pull/6/head
左程云 4 years ago
parent 0147f549e1
commit 3e511ede53

@ -3,7 +3,7 @@ package class25;
import java.util.Stack;
// 测试链接https://leetcode.com/problems/largest-rectangle-in-histogram
public class Code04_LargestRectangleInHistogram {
public class Code03_LargestRectangleInHistogram {
public static int largestRectangleArea1(int[] height) {
if (height == null || height.length == 0) {

@ -0,0 +1,48 @@
package class25;
import java.util.Stack;
// 测试链接https://leetcode.com/problems/maximal-rectangle/
public class Code04_MaximalRectangle {
public static int maximalRectangle(char[][] map) {
if (map == null || map.length == 0 || map[0].length == 0) {
return 0;
}
int maxArea = 0;
int[] height = new int[map[0].length];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
height[j] = map[i][j] == '0' ? 0 : height[j] + 1;
}
maxArea = Math.max(maxRecFromBottom(height), maxArea);
}
return maxArea;
}
// height是正方图数组
public static int maxRecFromBottom(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int maxArea = 0;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < height.length; i++) {
while (!stack.isEmpty() && height[i] <= height[stack.peek()]) {
int j = stack.pop();
int k = stack.isEmpty() ? -1 : stack.peek();
int curArea = (i - k - 1) * height[j];
maxArea = Math.max(maxArea, curArea);
}
stack.push(i);
}
while (!stack.isEmpty()) {
int j = stack.pop();
int k = stack.isEmpty() ? -1 : stack.peek();
int curArea = (height.length - k - 1) * height[j];
maxArea = Math.max(maxArea, curArea);
}
return maxArea;
}
}

@ -0,0 +1,56 @@
package class25;
import java.util.Stack;
// 测试链接https://leetcode.com/problems/count-submatrices-with-all-ones
public class Code05_CountSubmatricesWithAllOnes {
public static int numSubmat(int[][] mat) {
if (mat == null || mat.length == 0 || mat[0].length == 0) {
return 0;
}
int nums = 0;
int[] height = new int[mat[0].length];
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
height[j] = mat[i][j] == 0 ? 0 : height[j] + 1;
}
nums += countFromBottom(height);
}
return nums;
}
public static int countFromBottom(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int nums = 0;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < height.length; i++) {
while (!stack.isEmpty() && height[stack.peek()] >= height[i]) {
int cur = stack.pop();
if (height[cur] > height[i]) {
int left = stack.isEmpty() ? -1 : stack.peek();
int n = i - left - 1;
int down = Math.max(left == -1 ? 0 : height[left], height[i]);
nums += (height[cur] - down) * num(n);
}
}
stack.push(i);
}
while (!stack.isEmpty()) {
int cur = stack.pop();
int left = stack.isEmpty() ? -1 : stack.peek();
int n = height.length - left - 1;
int down = left == -1 ? 0 : height[left];
nums += (height[cur] - down) * num(n);
}
return nums;
}
public static int num(int n) {
return ((n * (1 + n)) >> 1);
}
}

@ -7,7 +7,7 @@ import java.util.Stack;
// subArrayMinSum2是最优解的思路
// sumSubarrayMins是最优解思路下的单调栈优化
// Leetcode上只提交sumSubarrayMins方法时间复杂度O(N),可以直接通过
public class Code03_SumOfSubarrayMinimums {
public class Code06_SumOfSubarrayMinimums {
public static int subArrayMinSum1(int[] arr) {
int ans = 0;
Loading…
Cancel
Save