Skip to content

Commit

Permalink
Solution to today's problem
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Jun 23, 2024
1 parent 5f00a9f commit 0f941e2
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@
class Problem1438 extends LeetcodeProblem {

int longestSubarray(int[] _nums, int _limit) {
int ans = 0;
int len = _nums.length;
int result = 0;
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
Queue<Integer> minHeap = new PriorityQueue<>();
for (int left = 0, right = 0; left < _nums.length && right < _nums.length; right++) {
if (ans == 0) {
ans = 1;
for (int left = 0, right = 0; left < len && right < len; right++) {
if (result == 0) {
result = 1;
}
maxHeap.offer(_nums[right]);
minHeap.offer(_nums[right]);
if (!maxHeap.isEmpty() && !minHeap.isEmpty() && maxHeap.peek() - minHeap.peek() <= _limit) {
ans = Math.max(ans, right - left + 1);
if (!maxHeap.isEmpty() && !minHeap.isEmpty()
&& maxHeap.peek() - minHeap.peek() <= _limit) {
result = Math.max(result, right - left + 1);
} else {
maxHeap.remove(_nums[left]);
minHeap.remove(_nums[left]);
left++;
}
}
return ans;
return result;
}

}

0 comments on commit 0f941e2

Please sign in to comment.