Advertisement
exmkg

Untitled

Sep 28th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. class Solution {
  2.     public int largestRectangleArea(int[] heights) {
  3.         int n = heights.length;
  4.         if (n == 0) {
  5.             return 0; // Base Condition
  6.         }
  7.  
  8.         int maxArea = 0;
  9.         int left[] = new int[n]; // fill left boundary
  10.         int right[] = new int[n]; // fill right boundary
  11.        
  12.         left[0] = -1;
  13.         right[n - 1] = n;
  14.          
  15.         for (int i = 1; i < n; i++) {
  16.             int prev = i - 1; // previous for comparing the heights
  17.             while (prev >= 0 && heights[prev] >= heights[i]) {
  18.                 prev = left[prev]; // we have done this to minimise the jumps we make to the left
  19.             }
  20.             left[i] = prev;
  21.         }
  22.  
  23.         // Similarly we do for right
  24.         for (int i = n - 2; i >= 0; i--) {
  25.             int prev = i + 1;
  26.             while (prev < n && heights[prev] >= heights[i]) {
  27.                 prev = right[prev];
  28.             }
  29.             right[i] = prev;
  30.         }
  31.  
  32.         // once we have these two arrays fill we need width & area
  33.         for (int i = 0; i < n; i++) {
  34.             int width = right[i] - left[i] - 1;
  35.             maxArea = Math.max(maxArea, heights[i] * width);
  36.         }
  37.         return maxArea;      
  38.     }
  39. }
  40.  
  41. /*
  42. Here's the intuition to understand p = lessFromLeft[p];
  43.  
  44. Consider the test case
  45. indices.......... :  0 1 2 3 4 5 6 7 8 9 10 11  .
  46. heights.......... :  4 9 8 7 6 5 9 8 7 6  5  4  .
  47. lessFromLeft      : -1 0 0 0 0 0 5 5 5 5  .  .  .
  48.  
  49. In this, when we reach 5 at index 10, we start searching for idx=9, i.e. p points at 6.
  50. 6 > 5.
  51. Now, we want something which is smaller than 5, so it should definitely be smaller than 6. So 6 says to 5:
  52.  
  53. I've already done the effort to find the nearest number that's smaller than me and you needn't traverse the array again till that point. My lessFromLeft points at index 5 and all the elements between that and me are greater than me so they are surely greater than you. So just jump to that index and start searching from there.
  54.  
  55. So you next p directly points at idx = 5, at value 5.
  56.  
  57. There, this 5 again says the same statement to current 5 and asks it to jump directly to idx = 0. So in the second iteration itself, our search has reached idx=0 and that's our answer for the current element.
  58.  
  59. Similarly, for the next element 4, it'll take 3 steps.
  60.  
  61. And for all the elements following 4, if they are greater than 4, their search will stop at 4 itself.
  62.  
  63. Bottom line: we are not traversing the array again and again. it's O(n).
  64. */
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement