From 4635eeef27a3e69990f8f950030c47ca19a3056c Mon Sep 17 00:00:00 2001 From: vivek-kumar-gupta Date: Wed, 26 Apr 2023 03:04:50 +0530 Subject: [PATCH 1/4] Update 0084-largest-rectangle-in-histogram.java --- java/0084-largest-rectangle-in-histogram.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/java/0084-largest-rectangle-in-histogram.java b/java/0084-largest-rectangle-in-histogram.java index 6ba5e0303..d2aeb3bbf 100644 --- a/java/0084-largest-rectangle-in-histogram.java +++ b/java/0084-largest-rectangle-in-histogram.java @@ -1,3 +1,6 @@ +//Please check this video which has best explaination which is understandable. +//https://youtu.be/lJLcqDsmYfg + class Solution { public int largestRectangleArea(int[] heights) { int area = 0, n = heights.length; From b00a0df82ad9839e65753afbb1d8247eeb5b43f7 Mon Sep 17 00:00:00 2001 From: vivek-kumar-gupta Date: Wed, 26 Apr 2023 03:43:11 +0530 Subject: [PATCH 2/4] Update 0084-largest-rectangle-in-histogram.java --- java/0084-largest-rectangle-in-histogram.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/java/0084-largest-rectangle-in-histogram.java b/java/0084-largest-rectangle-in-histogram.java index d2aeb3bbf..54445895d 100644 --- a/java/0084-largest-rectangle-in-histogram.java +++ b/java/0084-largest-rectangle-in-histogram.java @@ -1,6 +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; From f31756c0e5b9fd97feb11e79288f5296e1888cb4 Mon Sep 17 00:00:00 2001 From: vivek-kumar-gupta Date: Thu, 27 Apr 2023 01:59:05 +0530 Subject: [PATCH 3/4] Update 0424-longest-repeating-character-replacement.java --- java/0424-longest-repeating-character-replacement.java | 3 +++ 1 file changed, 3 insertions(+) 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; From 8afa89059e07ce8f580bfcf783e684246ff85af6 Mon Sep 17 00:00:00 2001 From: vivek-kumar-gupta Date: Thu, 27 Apr 2023 03:09:43 +0530 Subject: [PATCH 4/4] Update 0239-sliding-window-maximum.java --- java/0239-sliding-window-maximum.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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()];