-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path42.java
More file actions
145 lines (136 loc) · 4.87 KB
/
42.java
File metadata and controls
145 lines (136 loc) · 4.87 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
__________________________________________________________________________________________________
0ms
class Solution {
public int trap(int[] height) {
int result = 0;
int start = 0;
int end = height.length - 1;
while (start < end) {
if (height[start] <= height[end]) {
int current = height[start];
while (height[++start] < current) {
result += current - height[start];
}
} else {
int current = height[end];
while(height[--end] < current) {
result += current - height[end];
}
}
}
return result;
}
}
__________________________________________________________________________________________________
1ms
class Solution {
public int trap(int[] height) {
if(height == null || height.length == 0)
return 0;
int[] leftHighest = new int[height.length];
for(int i = 1; i < height.length; i++)
{
leftHighest[i] = Math.max(leftHighest[i-1], height[i-1]);
}
int rightHighest = height[height.length-1], result = 0;
for(int i = height.length-2; i > 0; i--)
{
int smaller = Math.min(rightHighest, leftHighest[i]);
if(smaller > height[i])
result += smaller-height[i];
rightHighest = Math.max(rightHighest, height[i]);
}
return result;
}
}
__________________________________________________________________________________________________
2ms
class Solution {
public int trap(int[] height) {
List<Integer> left = new ArrayList<>();
List<Integer> right = new ArrayList<>();
int max = Integer.MIN_VALUE;
int ans = 0;
for(int i = 0; i<height.length;i++)
{
left.add(Math.max(max,height[i]));
max = Math.max(max,height[i]);
}
max = Integer.MIN_VALUE;
for(int i = height.length - 1; i>=0;i--)
{
right.add(Math.max(max,height[i]));
max = Math.max(max,height[i]);
}
int l = 0, r = height.length - 1;
for(int i = 0; i<height.length;i++)
{
ans = ans + Math.min(left.get(l++),right.get(r--)) - height[i];
}
return ans;
}
}
__________________________________________________________________________________________________
35508 kb
class Solution {
public int trap(int[] height) {
int[] maxSoFarLeft = new int[height.length];
int[] maxSoFarRight = new int[height.length];
int left = 0;
int right = 0;
for(int i = 0; i < height.length; i++){
maxSoFarLeft[i] = Math.max(left, height[i]);
left = maxSoFarLeft[i];
}
for(int i = height.length -1 ; i >=0; i--){
maxSoFarRight[i] = Math.max(height[i], right);
right = maxSoFarRight[i];
}
int area = 0;
for(int i= 0; i < height.length; i++){
area += Math.min(maxSoFarLeft[i], maxSoFarRight[i]) - height[i];
}
return area;
}
}
__________________________________________________________________________________________________
35552 kb
class Solution {
public int trap(int[] A) {
if (A.length < 3)
return 0;
int ans = 0;
int l = 0;
int r = A.length - 1;
// find the left and right edge which can hold water
// 找到存储水的左右边界
while ((l < r) && (A[l] <= A[l + 1]))// 找到下降的位置
l++;
while ((l < r) && (A[r] <= A[r - 1]))
r--;
while (l < r) {
int left = A[l];
int right = A[r];
if (left <= right) {
// add volum until an edge larger than the left edge
// 加上水的体积直到大于左边界的数出现
while ((l < r) && (left >= A[++l]))
// left=A[1]=1, A[++l]=A[2]=0, +=1-0
// A[3]=2>left, l=3, continue
ans += (left - A[l]);
}
else {
// add volum until an edge larger than the right volum
// 加上水的体积直到大于右边界的数出现
while ((l < r) && (A[--r] <= right))
ans += (right - A[r]);
}
// while ((l < r) && (A[l] <= A[l + 1]))// 找到下降的位置
// l++;
// while ((l < r) && (A[r] <= A[r - 1]))
// r--;
}
return ans;
}
}
__________________________________________________________________________________________________