-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
91 lines (84 loc) · 2.94 KB
/
Solution.java
File metadata and controls
91 lines (84 loc) · 2.94 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.q0042;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
/**
* @author xjn
* @since 2020-05-28
* https://leetcode-cn.com/problems/trapping-rain-water/
* 42. 接雨水
* 时间复杂度O(n)
* 空间复杂度O(n)
*/
public class Solution {
public int trap(int[] height) {
if (height == null) {
return 0;
}
Deque<Integer> stack = new ArrayDeque<>();
int ans = 0;
for (int i = 0; i < height.length; i++) {
//如果当前值小于栈顶值,则直接push
while (!stack.isEmpty() && height[stack.peek()] < height[i]) {
//pop出来的值是比当前值小的元素
int cur = stack.pop();
System.out.println("i:" + i + " height[i]:" + height[i] + " cur:" + cur + " height[cur]:" + height[cur]);
if (stack.isEmpty()) {
break;
}
//距离差
int dis = i - stack.peek() - 1;
//面积
int h = Math.min(height[stack.peek()], height[i]) - height[cur];
ans += h * dis;
}
//当前值小于等于栈顶值
stack.push(i);
}
return ans;
}
public static void main(String[] args) {
Solution solution = new Solution();
// int[] array = new int[]{2, 1, 2, 4, 3};
// solution.test1(array);
// solution.test2(array);
//6
// System.out.println(solution.trap(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
// 1
System.out.println(solution.trap(new int[]{4, 2, 3}));
}
//https://labuladong.gitbook.io/algo/shu-ju-jie-gou-xi-lie/dan-tiao-zhan
public void test1(int[] array) {
//存放结果数组
int[] tempArray = new int[array.length];
Deque<Integer> stack = new ArrayDeque<>();
for (int i = array.length - 1; i >= 0; i--) {
//判定大小
while (!stack.isEmpty() && stack.peek() <= array[i]) {
//小的直接pop丢弃
stack.pop();
}
tempArray[i] = stack.isEmpty() ? -1 : stack.peek();
System.out.println(tempArray[i]);
stack.push(array[i]);
}
System.out.println(Arrays.toString(tempArray));
}
public void test2(int[] array) {
//存放结果数组
int[] tempArray = new int[array.length];
Deque<Integer> stack = new ArrayDeque<>();
for (int i = 0; i < array.length; i++) {
//判定大小
while (!stack.isEmpty() && stack.peek() <= array[i]) {
//小的直接pop丢弃
stack.pop();
// System.out.println(pop);
}
tempArray[i] = stack.isEmpty() ? -1 : stack.peek();
System.out.println(tempArray[i]);
stack.push(array[i]);
}
System.out.println(Arrays.toString(tempArray));
}
}