Skip to content

Commit 5b3e6af

Browse files
author
jim
committed
update 84 java, progress
1 parent fd2816f commit 5b3e6af

File tree

3 files changed

+154
-118
lines changed

3 files changed

+154
-118
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@
11591159
011| [Container With Most Water](https://leetcode.com/problems/container-with-most-water/)|[Python](./leetcode_python/Greedy/container-with-most-water.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/ContainerWithMostWater.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, good basics, `two pointers`,`fb`, amazon| OK*** (3) (but again !!)
11601160
045| [Jump Game II](https://leetcode.com/problems/jump-game-ii/)|[Python](./leetcode_python/Greedy/jump-game-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame2.java) | _O(n)_ | _O(1)_| Medium|Greedy, google, apple, fb, tesla, `amazon`| AGAIN******** (4) (MUST)
11611161
055| [Jump Game](https://leetcode.com/problems/jump-game/) |[Python](./leetcode_python/Greedy/jump-game.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame.java) | _O(n)_ | _O(1)_| Medium|Curated Top 75, good trick, backtrack, Greedy, DP, `amazon`| AGAIN******** (8)
1162-
84| [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)|[Python](./leetcode_python/Greedy/largest-rectangle-in-histogram.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/LargestRectangleInHistogram.java)| _O(n)_ | _O(1)_| Hard |top 100 like, brute force, divide and conquer, good trick, `mono stack`, LC 085, amazon, fb| AGAIN**** (2)
1162+
84| [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)|[Python](./leetcode_python/Greedy/largest-rectangle-in-histogram.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/LargestRectangleInHistogram.java)| _O(n)_ | _O(1)_| Hard |top 100 like, brute force, good trick, `mono stack`, LC 085, amazon, fb| AGAIN******** (3)
11631163
122| [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [Python](./leetcode_python/Greedy/best-time-to-buy-and-sell-stock-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/BestTimeToBuyAndSellStock2.java) | _O(n)_ | _O(1)_ | Easy |compare with `#309 Best Time to Buy and Sell Stock with Cooldown `, `#714 Best Time to Buy and Sell Stock with Transaction Fee`, `amazon`| again* (2)
11641164
134| [Gas Station](https://leetcode.com/problems/gas-station/)| [Python](./leetcode_python/Greedy/gas-station.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/GasStation.java) | _O(n)_ | _O(1)_ | Medium|trick, greedy,`amazon`| AGAIN****** (5)
11651165
135| [Candy](https://leetcode.com/problems/candy/)| [Python](./leetcode_python/Greedy/candy.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/Candy.java) | _O(n)_ | _O(1)_ | Hard|LC 123, greedy, boundary, array, `amazon`| AGAIN (2)

leetcode_java/src/main/java/LeetCodeJava/Stack/LargestRectangleInHistogram.java

Lines changed: 118 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,123 @@ public class LargestRectangleInHistogram {
4747
// }
4848

4949
// V0-0-1
50-
// IDEA: BRUTE FORCE (TLE)
50+
// IDEA: MONO STACK (GPT)
51+
// -> STACK (monotonic stack) + pop when (cur_height < last+height)
52+
/**
53+
* IDEA:
54+
*
55+
* 🔑 Takeaways:
56+
* • Stack stores indices, not heights — needed to compute width.
57+
* • Height comes from the popped index.
58+
* • Width is computed from current index minus previous index.
59+
* • The dummy 0 is a useful trick to flush the stack at the end.
60+
*/
61+
/**
62+
*
63+
🧠 Why This Works:
64+
• As long as the current bar is taller or equal, we push its index — since it may be part of a future larger rectangle.
65+
• When the current bar is shorter, we know:
66+
• All previous taller bars can’t extend past this point,
67+
• So we pop them, calculate area, and move on.
68+
*
69+
*/
5170
public int largestRectangleArea_0_0_1(int[] heights) {
71+
int n = heights.length;
72+
73+
/**
74+
* NOTE !!!!
75+
*
76+
* - A monotonic increasing stack to store the `indices` of bars.
77+
* - We’ll use this to track the left boundaries of rectangles.
78+
*
79+
*
80+
* NOTE !!!
81+
*
82+
* the stack stores the `index` of bars (small -> big)
83+
*
84+
* -> The stack stores indices of elements in non-decreasing height order
85+
* (i.e., from shortest to tallest) as we move left to right.
86+
*/
87+
Stack<Integer> stack = new Stack<>();
88+
int maxArea = 0;
89+
90+
// Append a 0 to flush out remaining stack at end
91+
/**
92+
*
93+
* - We’re looping from i = 0 to i = n inclusive.
94+
*
95+
* - At i = n, we use a dummy bar of height 0 to
96+
* flush the stack — this ensures we process
97+
* all remaining bars.
98+
*/
99+
for (int i = 0; i <= n; i++) {
100+
101+
/**
102+
* NOTE !!!!
103+
*
104+
* - If we’re at i == n, we use height 0 (virtual bar).
105+
* - Otherwise, we get the real bar height.
106+
*
107+
* - This trick helps to pop all remaining bars in the
108+
* stack by triggering the while condition.
109+
*
110+
*/
111+
int currHeight = (i == n) ? 0 : heights[i];
112+
113+
/**
114+
* - While the current height is `less` than the height at
115+
* the `top` of the stack:
116+
117+
* - We’ve found the right boundary for a rectangle with
118+
* `height heights[stack.peek()]`.
119+
*
120+
* - Now compute area using that height.
121+
*
122+
*/
123+
while (!stack.isEmpty() && currHeight < heights[stack.peek()]) {
124+
125+
/**
126+
* NOTE !!!
127+
*
128+
* - Pop the top index from the stack, and get its height.
129+
* - This is the `height` of the rectangle we’re evaluating.
130+
*/
131+
int height = heights[stack.pop()]; // NOTE !!! we pop the element from stack
132+
133+
/**
134+
* NOTE !!!
135+
*
136+
* - Compute the width of the rectangle:
137+
*
138+
* - If the stack is empty after popping,
139+
* it means the popped bar is the smallest so far,
140+
* so its width spans from index 0 to i - 1 → width = i.
141+
*
142+
* - Otherwise, the left boundary is the new stack.peek(),
143+
* so width is from stack.peek() + 1 to i - 1.
144+
*
145+
*/
146+
int width = stack.isEmpty() ? i : i - stack.peek() - 1;
147+
148+
// Compute area and update maxArea if it’s larger.
149+
maxArea = Math.max(maxArea, height * width);
150+
}
151+
152+
/**
153+
*
154+
* - Push the current index onto the stack.
155+
* - This keeps the indices of bars in `non-decreasing` height order.
156+
*/
157+
stack.push(i);
158+
}
159+
160+
return maxArea;
161+
}
162+
163+
164+
// V0-0-2
165+
// IDEA: BRUTE FORCE (TLE)
166+
public int largestRectangleArea_0_0_2(int[] heights) {
52167
// edge
53168
if (heights == null || heights.length == 0) {
54169
return 0;
@@ -73,7 +188,8 @@ public int largestRectangleArea_0_0_1(int[] heights) {
73188
}
74189

75190
// V0-1
76-
// IDEA : STACK (monotonic stack)
191+
// IDEA : STACK (monotonic stack) + pop when (cur_height < last+height)
192+
// mono increasing stack, but the popping condition is current height < height at stack top.
77193
// https://leetcode.com/problems/largest-rectangle-in-histogram/editorial/
78194
/**
79195
*
@@ -189,119 +305,6 @@ public int largestRectangleArea_1(int[] heights) {
189305
return maxArea;
190306
}
191307

192-
// V0-2
193-
// IDEA: MONO STACK (GPT)
194-
/**
195-
* IDEA:
196-
*
197-
* 🔑 Takeaways:
198-
* • Stack stores indices, not heights — needed to compute width.
199-
* • Height comes from the popped index.
200-
* • Width is computed from current index minus previous index.
201-
* • The dummy 0 is a useful trick to flush the stack at the end.
202-
*/
203-
/**
204-
*
205-
🧠 Why This Works:
206-
• As long as the current bar is taller or equal, we push its index — since it may be part of a future larger rectangle.
207-
• When the current bar is shorter, we know:
208-
• All previous taller bars can’t extend past this point,
209-
• So we pop them, calculate area, and move on.
210-
*
211-
*/
212-
public int largestRectangleArea_0_2(int[] heights) {
213-
int n = heights.length;
214-
215-
/**
216-
* NOTE !!!!
217-
*
218-
* - A monotonic increasing stack to store the `indices` of bars.
219-
* - We’ll use this to track the left boundaries of rectangles.
220-
*
221-
*
222-
* NOTE !!!
223-
*
224-
* the stack stores the `index` of bars (small -> big)
225-
*
226-
* -> The stack stores indices of elements in non-decreasing height order
227-
* (i.e., from shortest to tallest) as we move left to right.
228-
*/
229-
Stack<Integer> stack = new Stack<>();
230-
int maxArea = 0;
231-
232-
// Append a 0 to flush out remaining stack at end
233-
/**
234-
*
235-
* - We’re looping from i = 0 to i = n inclusive.
236-
*
237-
* - At i = n, we use a dummy bar of height 0 to
238-
* flush the stack — this ensures we process
239-
* all remaining bars.
240-
*/
241-
for (int i = 0; i <= n; i++) {
242-
243-
/**
244-
* NOTE !!!!
245-
*
246-
* - If we’re at i == n, we use height 0 (virtual bar).
247-
* - Otherwise, we get the real bar height.
248-
*
249-
* - This trick helps to pop all remaining bars in the
250-
* stack by triggering the while condition.
251-
*
252-
*/
253-
int currHeight = (i == n) ? 0 : heights[i];
254-
255-
/**
256-
* - While the current height is `less` than the height at
257-
* the `top` of the stack:
258-
259-
* - We’ve found the right boundary for a rectangle with
260-
* `height heights[stack.peek()]`.
261-
*
262-
* - Now compute area using that height.
263-
*
264-
*/
265-
while (!stack.isEmpty() && currHeight < heights[stack.peek()]) {
266-
267-
/**
268-
* NOTE !!!
269-
*
270-
* - Pop the top index from the stack, and get its height.
271-
* - This is the `height` of the rectangle we’re evaluating.
272-
*/
273-
int height = heights[stack.pop()]; // NOTE !!! we pop the element from stack
274-
275-
/**
276-
* NOTE !!!
277-
*
278-
* - Compute the width of the rectangle:
279-
*
280-
* - If the stack is empty after popping,
281-
* it means the popped bar is the smallest so far,
282-
* so its width spans from index 0 to i - 1 → width = i.
283-
*
284-
* - Otherwise, the left boundary is the new stack.peek(),
285-
* so width is from stack.peek() + 1 to i - 1.
286-
*
287-
*/
288-
int width = stack.isEmpty() ? i : i - stack.peek() - 1;
289-
290-
// Compute area and update maxArea if it’s larger.
291-
maxArea = Math.max(maxArea, height * width);
292-
}
293-
294-
/**
295-
*
296-
* - Push the current index onto the stack.
297-
* - This keeps the indices of bars in `non-decreasing` height order.
298-
*/
299-
stack.push(i);
300-
}
301-
302-
return maxArea;
303-
}
304-
305308
// V0-3
306309
// IDEA: BRUTE FORCE (TLE)
307310
public int largestRectangleArea_0_3(int[] heights) {

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,9 +2538,42 @@ else if (val < l_height){
25382538
*
25392539
* IDEA 2) BRUTE FORCE
25402540
*/
2541-
// IDEA 1) MONO STACK ???
2541+
// IDEA 1) MONO INCREASING STACK ???
25422542
public int largestRectangleArea(int[] heights) {
2543-
return 0;
2543+
// edge
2544+
if(heights.length == 0){
2545+
return 0;
2546+
}
2547+
if(heights.length == 1){
2548+
return heights[0];
2549+
}
2550+
2551+
int ans = 0;
2552+
2553+
// mono increasing stack
2554+
Stack<Integer> st = new Stack();
2555+
2556+
for(int i = 0; i < heights.length; i++){
2557+
int val = heights[i];
2558+
/**
2559+
* if cur val > `stack top val`
2560+
* -> pop val from stack
2561+
* -> get `area`
2562+
*
2563+
*/
2564+
ans = Math.max(ans, val);
2565+
2566+
while(!st.isEmpty() && st.peek() < val){
2567+
int last_idx = st.pop();
2568+
int tmp = Math.min(heights[last_idx], val) * (i - last_idx + 1);
2569+
System.out.println(">>> i = " + i + ", last_idx = " + last_idx + ", st = " + st + ", tmp = " + tmp);
2570+
ans = Math.max(ans, tmp);
2571+
}
2572+
2573+
st.add(i); // add idx to stack
2574+
}
2575+
2576+
return ans;
25442577
}
25452578

25462579

0 commit comments

Comments
 (0)