Skip to content

Commit fd2816f

Browse files
author
jim
committed
update 84 java
1 parent 81a5744 commit fd2816f

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,57 @@ public int largestRectangleArea_0_2(int[] heights) {
302302
return maxArea;
303303
}
304304

305+
// V0-3
306+
// IDEA: BRUTE FORCE (TLE)
307+
public int largestRectangleArea_0_3(int[] heights) {
308+
// edge
309+
if (heights.length == 0) {
310+
return 0;
311+
}
312+
if (heights.length == 1) {
313+
return heights[0];
314+
}
315+
// if(heights.length == 2){
316+
// return Math.min(heights[0], heights[1]);
317+
// }
318+
int ans = 0;
319+
320+
// brute force
321+
for (int i = 0; i < heights.length; i++) {
322+
int min_val = heights[i];
323+
ans = Math.max(heights[i], ans);
324+
for (int j = i + 1; j < heights.length; j++) {
325+
min_val = Math.min(min_val, heights[j]);
326+
int tmp = min_val * (j - i + 1);
327+
System.out.println(
328+
">>> i = " + i + ", j = " + j + ", tmp = " + tmp + ", ans = " + ans + ", min_val = " + min_val);
329+
ans = Math.max(tmp, ans);
330+
}
331+
}
332+
333+
return ans;
334+
}
335+
336+
// V0-4
337+
// IDEA: monotonic increasing stack (GPT)
338+
public int largestRectangleArea_0_4(int[] heights) {
339+
int n = heights.length;
340+
Stack<Integer> st = new Stack<>();
341+
int ans = 0;
342+
343+
for (int i = 0; i <= n; i++) {
344+
int h = (i == n) ? 0 : heights[i]; // sentinel at the end
345+
while (!st.isEmpty() && h < heights[st.peek()]) {
346+
int height = heights[st.pop()];
347+
int width = st.isEmpty() ? i : i - st.peek() - 1;
348+
ans = Math.max(ans, height * width);
349+
}
350+
st.push(i);
351+
}
352+
353+
return ans;
354+
}
355+
305356
// V2
306357
// IDEA : BRUTE FORCE
307358
// https://leetcode.com/problems/largest-rectangle-in-histogram/editorial/

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,5 +2523,56 @@ else if (val < l_height){
25232523
return ans;
25242524
}
25252525

2526+
// LC 84
2527+
// 10.29 - 10.39 AM
2528+
/**
2529+
* -> return the` area `of the `largest rectangle` in the `histogram`.
2530+
*
2531+
* rectangle: `left height` == `right height`
2532+
*
2533+
* -> max rec: find the case that left height and right height are the same and BOTH exist
2534+
*
2535+
*
2536+
* IDEA 1) STACK: right, left st
2537+
* ->
2538+
*
2539+
* IDEA 2) BRUTE FORCE
2540+
*/
2541+
// IDEA 1) MONO STACK ???
2542+
public int largestRectangleArea(int[] heights) {
2543+
return 0;
2544+
}
2545+
2546+
2547+
2548+
// IDEA 2) BRUTE FORCE
2549+
public int largestRectangleArea_1(int[] heights) {
2550+
// edge
2551+
if(heights.length == 0){
2552+
return 0;
2553+
}
2554+
if(heights.length == 1){
2555+
return heights[0];
2556+
}
2557+
// if(heights.length == 2){
2558+
// return Math.min(heights[0], heights[1]);
2559+
// }
2560+
int ans = 0;
2561+
2562+
// brute force
2563+
for(int i = 0; i < heights.length; i++){
2564+
int min_val = heights[i];
2565+
ans = Math.max(heights[i], ans);
2566+
for(int j = i+1; j < heights.length; j++){
2567+
min_val = Math.min(min_val, heights[j]);
2568+
int tmp = min_val * (j - i + 1);
2569+
System.out.println(">>> i = " + i + ", j = " + j + ", tmp = " + tmp + ", ans = " + ans + ", min_val = " + min_val);
2570+
ans = Math.max(tmp, ans);
2571+
}
2572+
}
2573+
2574+
return ans;
2575+
}
2576+
25262577

25272578
}

0 commit comments

Comments
 (0)