Skip to content

Commit 72073f6

Browse files
author
jim
committed
update 42 java
1 parent 907fe55 commit 72073f6

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

data/progress.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
- LC 125 - Valid Palindrome
1818
- 42
1919
- LC 11 - Container With Most Water
20-
- 344
2120
- 42
2221
- 84
2322
- 85

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ public class TrappingRainWater {
4343
//
4444
// }
4545

46+
// V0-0-1
47+
// IDEA: STACK (fixed by gpt)
48+
public int trap_0_0_1(int[] height) {
49+
int n = height.length;
50+
if (n <= 2)
51+
return 0;
52+
53+
int ans = 0;
54+
Stack<Integer> st = new Stack<>(); // store indices
55+
56+
for (int i = 0; i < n; i++) {
57+
// Process while current height is greater than the stack top
58+
while (!st.isEmpty() && height[i] > height[st.peek()]) {
59+
int bottom = st.pop(); // the "valley"
60+
if (st.isEmpty())
61+
break; // no left boundary
62+
63+
int left = st.peek(); // left boundary index
64+
int width = i - left - 1; // distance between left and right walls
65+
int h = Math.min(height[left], height[i]) - height[bottom]; // bounded height
66+
67+
ans += width * h;
68+
}
69+
st.push(i);
70+
}
71+
72+
return ans;
73+
}
74+
4675
// V0-1
4776
// IDEA: 2 POINTER + left_till_array + max_till_array
4877
/**

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,5 +2444,89 @@ public int[] searchRange(int[] nums, int target) {
24442444
return res; // ???
24452445
}
24462446

2447+
// LC 42
2448+
// 9.06 - 9.20 am
2449+
/**
2450+
* IDEA 1) 2 pointers + STACK
2451+
*
2452+
* -> left, right pointer
2453+
* -> get `area` when `right > left`
2454+
* -> sum over all `area` as ans
2455+
* -> slide window ???
2456+
* -> maintain a window while
2457+
* - left <= right
2458+
* - otherwise, get `all areas` within window
2459+
* - NOT get area at `0 idx`
2460+
*
2461+
* exp 1)
2462+
*
2463+
* Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
2464+
* Output: 6
2465+
*
2466+
*
2467+
* [0,1,0,2,1,0,1,3,2,1,2,1] ans = 0
2468+
* x
2469+
*
2470+
* [0,1,0,2,1,0,1,3,2,1,2,1] ans = 0, st = [1], max_l = 1
2471+
* x
2472+
*
2473+
* [0,1,0,2,1,0,1,3,2,1,2,1] ans = 0, st = [1,0], max_l = 1
2474+
* x
2475+
*
2476+
* [0,1,0,2,1,0,1,3,2,1,2,1] ans = 0, 2 > max_l, st = [], ans = (left_l - cur_hight * 1 ) = 1
2477+
* x
2478+
*
2479+
*/
2480+
public int trap(int[] height) {
2481+
// edge
2482+
if(height.length <= 2){
2483+
return 0;
2484+
}
2485+
// // ??
2486+
// if(height.length == 3){
2487+
// return 0;
2488+
// }
2489+
2490+
int ans = 0;
2491+
Stack<Integer> st = new Stack<>();
2492+
//int l = 0;
2493+
//int r = 0;
2494+
2495+
//int l_height = Integer.MAX_VALUE; // ???
2496+
Stack<Integer> st_left_height = new Stack<>();
2497+
2498+
// NOTE !!! idx starts from 1
2499+
for(int r = 1; r < height.length; r++){
2500+
int val = height[r];
2501+
// case 1) st is empty
2502+
if(st.isEmpty()){
2503+
//l_height = val;
2504+
st_left_height.add(val);
2505+
st.add(val);
2506+
}
2507+
// case 2) cur val < l_height
2508+
else if (val < st_left_height.peek()){
2509+
st_left_height.add(val);
2510+
st.add(val);
2511+
}
2512+
// case 3) cur val > l_height
2513+
else{
2514+
// get `area`
2515+
while(!st.isEmpty()){
2516+
// pop `1st element` from st_left_height
2517+
int l_height = st_left_height.pop();
2518+
//int tmp = st.pop() * l_height;
2519+
ans += (st.pop() * l_height);
2520+
}
2521+
// add cur val to st
2522+
st.add(val);
2523+
// l_height = val;
2524+
st_left_height.add(val);
2525+
}
2526+
}
2527+
2528+
return ans;
2529+
}
2530+
24472531

24482532
}

0 commit comments

Comments
 (0)