diff --git a/java/0084-largest-rectangle-in-histogram.java b/java/0084-largest-rectangle-in-histogram.java index 6ba5e0303..54445895d 100644 --- a/java/0084-largest-rectangle-in-histogram.java +++ b/java/0084-largest-rectangle-in-histogram.java @@ -1,3 +1,58 @@ +//Please check this video which has best explaination which is understandable. +//https://youtu.be/lJLcqDsmYfg + +class Solution2 { + public int largestRectangleArea(int[] heights) { + + int[] nextSmallerIndices = findNextSmallerIndex(heights); + int[] prevSmallerIndices = findPrevSmallerIndex(heights); + int maxArea = 0; + for (int i = 0; i < heights.length; i++){ + + maxArea = Math.max(maxArea, ((nextSmallerIndices[i] == - 1 + ? heights.length : nextSmallerIndices[i]) - prevSmallerIndices[i] - 1) * heights[i]); + } + return maxArea; + } + + + private int[] findNextSmallerIndex(int[] heights){ + Stack stack = new Stack<>(); + int[] result = new int[heights.length]; + stack.push(-1); + + for (int i = heights.length - 1; i >=0 ; i--){ + while (stack.peek() != -1 && heights[stack.peek()] >= heights[i]){ + stack.pop(); + } + + result[i] = stack.peek(); + stack.push(i); + + + } + return result; + + } + + private int[] findPrevSmallerIndex(int[] heights){ + Stack stack = new Stack<>(); + int[] result = new int[heights.length]; + stack.push(-1); + + for (int i = 0; i < heights.length ; i++){ + while (stack.peek() != -1 && heights[stack.peek()] >= heights[i]){ + stack.pop(); + } + result[i] = stack.peek(); + stack.push(i); + + + } + return result; + } +} + class Solution { public int largestRectangleArea(int[] heights) { int area = 0, n = heights.length; diff --git a/java/0239-sliding-window-maximum.java b/java/0239-sliding-window-maximum.java index f4acef438..42bd654ab 100644 --- a/java/0239-sliding-window-maximum.java +++ b/java/0239-sliding-window-maximum.java @@ -1,11 +1,13 @@ class Solution { - + //It also has nice explainasion. public int[] maxSlidingWindow(int[] nums, int k) { int[] ans = new int[nums.length - k + 1]; int j = 0; Deque q = new LinkedList<>(); for (int i = 0; i < nums.length; i++) { + //here we are checking if this is in current bound if not removing it. if (!q.isEmpty() && q.peekFirst() < i - k + 1) q.pollFirst(); + //here we are checking if current element is greater than value in queue.. then keep polling it. while (!q.isEmpty() && nums[i] > nums[q.peekLast()]) q.pollLast(); q.offer(i); if (i >= k - 1) ans[j++] = nums[q.peekFirst()]; diff --git a/java/0424-longest-repeating-character-replacement.java b/java/0424-longest-repeating-character-replacement.java index 1ce9988b4..4845307a0 100644 --- a/java/0424-longest-repeating-character-replacement.java +++ b/java/0424-longest-repeating-character-replacement.java @@ -1,5 +1,8 @@ class Solution { + + //Please see the way sliding window is implemented. + // This is goood as if you go for other way.. will be bit confusing and time consuming. public int characterReplacement(String s, int k) { int[] arr = new int[26]; int ans = 0;