Skip to content

Commit bbd7445

Browse files
author
jim
committed
update 42 java, readme
1 parent 72073f6 commit bbd7445

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@
812812
| # | Title | Solution | Time | Space | Difficulty | Note | Status|
813813
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
814814
019| [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| [Python](./leetcode_python/Two_Pointers/remove-nth-node-from-end-of-list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/RemoveNthNodeFromEndOfList.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, good basic, MUST, `linked list`,`two pointers`,`fb`| AGAIN********** (4) (MUST)
815-
042 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [Python](./leetcode_python/Two_Pointers/trapping-rain-water.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/TwoPointer/TrappingRainWater.java) || | Hard |scan, dp, trick, two pointer, `amazon`, `apple`| AGAIN*** (2) (not start)
815+
042 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [Python](./leetcode_python/Two_Pointers/trapping-rain-water.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/TwoPointer/TrappingRainWater.java) || | Hard |stack, prefix-suffix array, brute force, two pointer, `amazon`, `apple`| AGAIN******** (3)
816816
086| [Partition List](https://leetcode.com/problems/partition-list/)| [Python](./leetcode_python/Two_Pointers/partition-list.py) | _O(n)_ | _O(1)_ | Medium || OK*
817817
141| [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)| [Python](./leetcode_python/Two_Pointers/linked-list-cycle.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/TwoPointer/LinkedListCycle.java) | _O(n)_ | _O(1)_ | Easy |Curated Top 75, `basic`,`amazon`, `fb`, linked list, 2 pointers, check #142| OK** (5)
818818
142| [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)| [Python](./leetcode_python/Two_Pointers/linked-list-cycle-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/TwoPointer/LinkedListCycle2.java) | _O(n)_ | _O(1)_ | Medium |linked list, 2 pointers, check #141| AGAIN****** (4) (again!)

leetcode_java/src/main/java/LeetCodeJava/TwoPointer/TrappingRainWater.java

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,86 @@ public class TrappingRainWater {
4545

4646
// V0-0-1
4747
// IDEA: STACK (fixed by gpt)
48+
/**
49+
* Example:
50+
*
51+
* height = [0,1,0,2]
52+
*
53+
* Steps:
54+
* 1. i=0 → push 0.
55+
* 2. i=1 → height[1]=1 > height[0]=0 → pop valley 0.
56+
* • Left boundary doesn’t exist (stack empty) → break.
57+
* • Push 1.
58+
* 3. i=2 → height[2]=0 not > height[1]=1 → push 2.
59+
* 4. i=3 → height[3]=2 > height[2]=0.
60+
* • Pop valley 2. Left boundary = 1.
61+
* • Width = 3-1-1=1.
62+
* • Height = min(2,1)-0=1.
63+
* • Water = 1*1=1.
64+
* • Continue: height[3]=2 > height[1]=1.
65+
* • Pop valley 1. Stack empty → break.
66+
* • Push 3.
67+
*
68+
*
69+
* -> Result = 1. ✅
70+
*
71+
*/
4872
public int trap_0_0_1(int[] height) {
4973
int n = height.length;
5074
if (n <= 2)
5175
return 0;
5276

77+
/**
78+
* - ans = result (total trapped water).
79+
*
80+
* - st = stack to store indices (not heights directly).
81+
* - Storing indices allows us to compute both height and width.
82+
*/
5383
int ans = 0;
5484
Stack<Integer> st = new Stack<>(); // store indices
5585

5686
for (int i = 0; i < n; i++) {
5787
// Process while current height is greater than the stack top
88+
/**
89+
* NOTE !!
90+
*
91+
* - While stack is not empty and the current bar (height[i])
92+
* is taller than the bar at the top index (height[st.peek()]):
93+
*
94+
* - We found a right boundary for a trapped water “basin.” (盆地)
95+
*
96+
*/
5897
while (!st.isEmpty() && height[i] > height[st.peek()]) {
98+
/**
99+
*
100+
* - Pop the index at the top.
101+
* - This represents the `valley` bottom between two walls.
102+
*/
59103
int bottom = st.pop(); // the "valley"
60104
if (st.isEmpty())
61105
break; // no left boundary
62106

107+
/**
108+
* - The new top of stack is the left boundary index.
109+
*/
63110
int left = st.peek(); // left boundary index
64111
int width = i - left - 1; // distance between left and right walls
65-
int h = Math.min(height[left], height[i]) - height[bottom]; // bounded height
112+
/**
113+
* NOTE !!!
114+
*
115+
* - Effective water height =
116+
* shorter of the `two walls` (left and right) minus the valley’s height.
117+
*
118+
* - This ensures we don’t over-count if one wall is shorter.
119+
*
120+
*
121+
* -> `eff` height = min (left_wall_height, right_wall_height)
122+
*
123+
* e.g. Math.min( height[left], height[i] ) - height[bottom]
124+
*
125+
* (NOTE !!! need to SUBTRACT the `bottom` height)
126+
*/
127+
int h = Math.min( height[left], height[i] ) - height[bottom]; // bounded height
66128

67129
ans += width * h;
68130
}
@@ -153,7 +215,7 @@ public int trap_0_1(int[] height) {
153215

154216
// V1-1
155217
// https://neetcode.io/problems/trapping-rain-water
156-
// IDEA: BRUTE FORCE
218+
// IDEA: BRUTE FORCE - `leftMax, rightMax array`
157219
public int trap_1_1(int[] height) {
158220
if (height == null || height.length == 0) {
159221
return 0;

leetcode_java/src/main/java/dev/workspace14.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,46 +2482,41 @@ public int trap(int[] height) {
24822482
if(height.length <= 2){
24832483
return 0;
24842484
}
2485-
// // ??
2486-
// if(height.length == 3){
2487-
// return 0;
2488-
// }
2489-
24902485
int ans = 0;
24912486
Stack<Integer> st = new Stack<>();
24922487
//int l = 0;
24932488
//int r = 0;
24942489

2495-
//int l_height = Integer.MAX_VALUE; // ???
2496-
Stack<Integer> st_left_height = new Stack<>();
2490+
int l_height = Integer.MAX_VALUE; // ???
2491+
//Stack<Integer> st_left_height = new Stack<>();
24972492

24982493
// NOTE !!! idx starts from 1
24992494
for(int r = 1; r < height.length; r++){
25002495
int val = height[r];
25012496
// case 1) st is empty
25022497
if(st.isEmpty()){
2503-
//l_height = val;
2504-
st_left_height.add(val);
2498+
l_height = val;
2499+
//st_left_height.add(val);
25052500
st.add(val);
25062501
}
25072502
// case 2) cur val < l_height
2508-
else if (val < st_left_height.peek()){
2509-
st_left_height.add(val);
2503+
else if (val < l_height){
2504+
//st_left_height.add(val);
25102505
st.add(val);
25112506
}
25122507
// case 3) cur val > l_height
25132508
else{
25142509
// get `area`
25152510
while(!st.isEmpty()){
25162511
// pop `1st element` from st_left_height
2517-
int l_height = st_left_height.pop();
2512+
//int l_height = st_left_height.pop();
25182513
//int tmp = st.pop() * l_height;
25192514
ans += (st.pop() * l_height);
25202515
}
25212516
// add cur val to st
25222517
st.add(val);
2523-
// l_height = val;
2524-
st_left_height.add(val);
2518+
l_height = val;
2519+
//st_left_height.add(val);
25252520
}
25262521
}
25272522

0 commit comments

Comments
 (0)