Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4b0f447

Browse files
committed
042 (3) cleanup solution
1 parentc14c3ac commit4b0f447

File tree

3 files changed

+33
-52
lines changed

3 files changed

+33
-52
lines changed

‎src/_042_TrappingRainWater/Practice.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
*************************************************************************
33
* Description:
44
*
5-
* Given an unsorted integer array, find the first missing positive integer.
5+
* Given n non-negative integers representing an elevation map where the
6+
* width of each bar is 1, compute how much water it is able to trap after raining.
67
*
7-
* For example, Given [1,2,0] return 3,
8-
* and [3,4,-1,1] return 2.
9-
*
10-
* Your algorithm should run in O(n) time and uses constant space.
8+
* For example,
9+
* Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
1110
*
1211
*************************************************************************
1312
* @tag : Array; Stack; Two Pointers

‎src/_042_TrappingRainWater/Solution.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,21 @@
33
* @tag : Array; Stack; Two Pointers
44
* @by : Steven Cooks
55
* @date: Jul 15, 2015
6-
*************************************************************************
6+
**************************************************************************
77
* Description:
88
*
9-
* Given an unsorted integer array, find the first missing positive integer.
10-
*
11-
* For example, Given [1,2,0] return 3,
12-
* and [3,4,-1,1] return 2.
9+
* Given n non-negative integers representing an elevation map where the
10+
* width of each bar is 1, compute how much water it is able to trap after raining.
1311
*
14-
* Your algorithm should run in O(n) time and uses constant space.
12+
* For example,
13+
* Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
1514
*
1615
*************************************************************************
1716
* {@link https://leetcode.com/problems/trapping-rain-water/ }
1817
*/
1918
package_042_TrappingRainWater;
2019

21-
/**
22-
* O(1) space solution, and there is another O(N) solution
23-
* {@link _042_TrappingRainWater.SolutionStack }
24-
* see test {@link _042_TrappingRainWater.SolutionTest }
25-
*/
20+
/** * see test {@link _042_TrappingRainWater.SolutionTest } */
2621
publicclassSolution {
2722

2823
publicinttrap(int[]height) {
@@ -42,7 +37,6 @@ public int trap(int[] height) {
4237
// update left barrier
4338
leftBarrier =height[left];
4439
}else {
45-
// trap water (leftBarrier - height[left]) * 1
4640
result +=leftBarrier -height[left];
4741
}
4842
left++;
@@ -51,7 +45,6 @@ public int trap(int[] height) {
5145
// update right barrier
5246
rightBarrier =height[right];
5347
}else {
54-
// trap water (rightBarrier - height[right]) * 1
5548
result +=rightBarrier -height[right];
5649
}
5750
right--;

‎src/_042_TrappingRainWater/SolutionStack.java

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,45 @@
33
* @tag : Array; Stack; Two Pointers
44
* @by : Steven Cooks
55
* @date: Jul 15, 2015
6-
*************************************************************************
6+
**************************************************************************
77
* Description:
88
*
9-
* Given an unsorted integer array, find the first missing positive integer.
10-
*
11-
* For example, Given [1,2,0] return 3,
12-
* and [3,4,-1,1] return 2.
9+
* Given n non-negative integers representing an elevation map where the
10+
* width of each bar is 1, compute how much water it is able to trap after raining.
1311
*
14-
* Your algorithm should run in O(n) time and uses constant space.
12+
* For example,
13+
* Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
1514
*
1615
*************************************************************************
1716
* {@link https://leetcode.com/problems/trapping-rain-water/ }
17+
* @reference {@link https://leetcode.com/discuss/41688/stack-based-solution-inspired-by-largest-histogram-problem }
1818
*/
1919
package_042_TrappingRainWater;
2020

21+
importjava.util.Stack;
22+
2123
/** see test {@link _042_TrappingRainWater.SolutionStackTest } */
2224
publicclassSolutionStack {
2325

26+
// keep a non-ascending stack: element in stack is index, and their height[index]
27+
// is non-ascending order
28+
// because we need a 'valley' to trap water, for 'valley', both left and
29+
// right bar should be higher
2430
publicinttrap(int[]height) {
25-
intleft =0;
26-
intright =height.length -1;
27-
28-
intleftBarrier =0;
29-
intrightBarrier =0;
30-
intresult =0;
31-
32-
while (left <=right) {
33-
if (leftBarrier <=rightBarrier) {
34-
// there could be a basin between leftBarrier and rightBarrier
35-
// and left side is lower one
36-
if (height[left] >leftBarrier) {
37-
// update left barrier
38-
leftBarrier =height[left];
39-
}else {
40-
// trap water (leftBarrier - height[left]) * 1
41-
result +=leftBarrier -height[left];
42-
}
43-
left++;
44-
}else {
45-
if (height[right] >rightBarrier) {
46-
// update right barrier
47-
rightBarrier =height[right];
48-
}else {
49-
// trap water (rightBarrier - height[right]) * 1
50-
result +=rightBarrier -height[right];
31+
Stack<Integer>indices =newStack<>();
32+
intres =0;
33+
for (inti =0;i <height.length;i++) {
34+
while (!indices.isEmpty() &&height[i] >height[indices.peek()]) {
35+
intlow =height[indices.pop()];
36+
if (!indices.empty()) {
37+
intw =i -indices.peek() -1;
38+
inth =Math.min(height[i],height[indices.peek()]) -low;
39+
res +=w *h;
5140
}
52-
right--;
5341
}
42+
indices.push(i);
5443
}
55-
returnresult;
44+
returnres;
5645
}
5746

5847
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp