几种解法:
按行加
public int trap(int[] height) {
int sum = 0;
int max = getMax(height);//找到最大的高度,以便遍历。
for (int i = 1; i <= max; i++) {
boolean isStart = false; //标记是否开始更新 temp
int temp_sum = 0;
for (int j = 0; j < height.length; j++) {
if (isStart && height[j] < i) {
temp_sum++;
}
if (height[j] >= i) {
sum = sum + temp_sum;
temp_sum = 0;
isStart = true;
}
}
}
return sum;
}
private int getMax(int[] height) {
int max = 0;
for (int i = 0; i < height.length; i++) {
if (height[i] > max) {
max = height[i];
}
}
return max;
}
按列加
public int trap(int[] height){
int sum = 0;
for(int i = 1;i < height.length - 1;i++){
int maxLeft = 0;
for(int j = i - 1;j >= 0;j--){
if(height[j] > height[i]){
maxLeft = j;
}
}
int maxRight = 0;
for(int j = i + 1;j < height.length;j++){
if(height[j] > height[i]){
maxRight = j;
}
}
int min = Math.min(maxLeft,maxRight);
if(height[i] < min){
sum += (min - height[i]);
}
}
return sum;
}
42. 按列加使用左右最大值数组,利用空间换时间。
class Solution {
public int trap(int[] height) {
if(height == null || height.length == 0) return 0;
int len = height.length;
int[] left = new int[len];
left[0] = height[0];
for(int i = 1;i < len;i++) {
left[i] = Math.max(left[i - 1],height[i]);
}
int[] right = new int[len];
right[len - 1] = height[len - 1];
for(int i = len - 2;i >= 0;i--) {
right[i] = Math.max(right[i + 1],height[i]);
}
int max = 0;
for(int i = 1;i < len - 1;i++) {
int min = Math.min(left[i - 1],right[i + 1]);
if(height[i] < min) {
max += (min - height[i]);
}
}
return max;
}
}
42.接雨水(单调栈的实现)
代码来源:
https://leetcode-cn.com/problems/trapping-rain-water/solution/dan-diao-zhan-jie-jue-jie-yu-shui-wen-ti-by-sweeti/
public int trap2(int[] height) {
if (height == null) {
return 0;
}
Stack<Integer> stack = new Stack<>();
int ans = 0;
for (int i = 0; i < height.length; i++) {
while(!stack.isEmpty() && height[stack.peek()] < height[i]) {
int curIdx = stack.pop();
// 如果栈顶元素一直相等,那么全都pop出去,只留第一个。
while (!stack.isEmpty() && height[stack.peek()] == height[curIdx]) {
stack.pop();
}
if (!stack.isEmpty()) {
int stackTop = stack.peek();
// stackTop此时指向的是此次接住的雨水的左边界的位置。右边界是当前的柱体,即i。
// Math.min(height[stackTop], height[i]) 是左右柱子高度的min减去
//height[curIdx]就是雨水的高度。 i - stackTop - 1 是雨水的宽度。
ans += (Math.min(height[stackTop], height[i]) - height[curIdx]) * (i - stackTop - 1);
}
}
stack.add(i);
}
return ans;
}
84.柱状图中最大的矩形
暴力,其实比较容易理解。
自己写的。
public int largestRectangleArea(int[] heights) {
int len = heights.length;
if (len == 0) return 0;
int max = 0;
for (int i = 0; i < len; i++) {
int left = i;
int right = i;
while (left - 1 >= 0 && heights[left - 1] >= heights[i]) left--;
while (right + 1 < len && heights[right + 1] >= heights[i]) right++;
max = Math.max((right - left + 1) * heights[i], max);
}
return max;
}
单调栈:
代码来源:
https://leetcode-cn.com/problems/largest-rectangle-in-histogram/solution/bao-li-jie-fa-zhan-by-liweiwei1419/
public int largestRectangleArea1(int[] heights) {
int len = heights.length;
if (len == 0) {
return 0;
}
if (len == 1) {
return heights[0];
}
int res = 0;
Deque<Integer> stack = new ArrayDeque<>(len);
for (int i = 0; i < len; i++) {
// 这个 while 很关键,因为有可能不止一个柱形的最大宽度可以被计算出来
while (!stack.isEmpty() && heights[i] < heights[stack.peekLast()]) {
int curHeight = heights[stack.pollLast()];
while (!stack.isEmpty() && heights[stack.peekLast()] == curHeight) {
stack.pollLast();
}
int curWidth;
if (stack.isEmpty()) {
curWidth = i;
} else {
curWidth = i - stack.peekLast() - 1;
}
// System.out.println("curIndex = " + curIndex + " " + curHeight * curWidth);
res = Math.max(res, curHeight * curWidth);
}
stack.addLast(i);
}
while (!stack.isEmpty()) {
int curHeight = heights[stack.pollLast()];
while (!stack.isEmpty() && heights[stack.peekLast()] == curHeight) {
stack.pollLast();
}
int curWidth;
if (stack.isEmpty()) {
curWidth = len;
} else {
curWidth = len - stack.peekLast() - 1;
}
res = Math.max(res, curHeight * curWidth);
}
return res;
}
84.单调栈的优化(前后加两个哨兵)
代码来源:
https://leetcode-cn.com/problems/largest-rectangle-in-histogram/solution/bao-li-jie-fa-zhan-by-liweiwei1419/
public int largestRectangleArea2(int[] heights) {
int len = heights.length;
if (len == 0) {
return 0;
}
if (len == 1) {
return heights[0];
}
int res = 0;
int[] newHeights = new int[len + 2];
newHeights[0] = 0;
System.arraycopy(heights, 0, newHeights, 1, len);
newHeights[len + 1] = 0;
len += 2;
heights = newHeights;
Deque<Integer> stack = new ArrayDeque<>(len);
// 先放入哨兵,在循环里就不用做非空判断
stack.addLast(0);
for (int i = 1; i < len; i++) {
while (heights[i] < heights[stack.peekLast()]) {
int curHeight = heights[stack.pollLast()];
int curWidth = i - stack.peekLast() - 1;
res = Math.max(res, curHeight * curWidth);
}
stack.addLast(i);
}
return res;
}
739.每日温度
从后向前跳
public int[] dailyTemperatures1(int[] T) {
if (T == null || T.length == 0) return new int[0];
int len = T.length;
int[] dp = new int[len];
dp[len - 1] = 0;
for (int i = len - 2; i >= 0; i--) {
if (T[i] < T[i + 1]){
dp[i] = 1;
} else {
int temp = i + 1;
while (dp[temp] != 0 && temp < len - 1 && T[temp] <= T[i]){
temp = temp + dp[temp];
}
if (T[temp] > T[i]) {
dp[i] = temp - i;
} else {
dp[i] = 0;
}
}
}
return dp;
}
单调栈的典型应用
public int[] dailyTemperatures(int[] T) {
if (T == null || T.length == 0) return new int[0];
int len = T.length;
int[] res = new int[len];
Deque<Integer> stack = new ArrayDeque<>();
for (int i = 0; i < len; i++) {
while (!stack.isEmpty() && T[i] > T[stack.peekLast()]){
res[stack.peekLast()] = i - stack.pollLast();
}
stack.addLast(i);
}
return res;
}