Skip to content

Commit 0f941e2

Browse files
committed
Solution to today's problem
1 parent 5f00a9f commit 0f941e2

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/main/java/io/github/spannm/leetcode/lc1/lc1400/Problem1438.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,26 @@
88
class Problem1438 extends LeetcodeProblem {
99

1010
int longestSubarray(int[] _nums, int _limit) {
11-
int ans = 0;
11+
int len = _nums.length;
12+
int result = 0;
1213
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
1314
Queue<Integer> minHeap = new PriorityQueue<>();
14-
for (int left = 0, right = 0; left < _nums.length && right < _nums.length; right++) {
15-
if (ans == 0) {
16-
ans = 1;
15+
for (int left = 0, right = 0; left < len && right < len; right++) {
16+
if (result == 0) {
17+
result = 1;
1718
}
1819
maxHeap.offer(_nums[right]);
1920
minHeap.offer(_nums[right]);
20-
if (!maxHeap.isEmpty() && !minHeap.isEmpty() && maxHeap.peek() - minHeap.peek() <= _limit) {
21-
ans = Math.max(ans, right - left + 1);
21+
if (!maxHeap.isEmpty() && !minHeap.isEmpty()
22+
&& maxHeap.peek() - minHeap.peek() <= _limit) {
23+
result = Math.max(result, right - left + 1);
2224
} else {
2325
maxHeap.remove(_nums[left]);
2426
minHeap.remove(_nums[left]);
2527
left++;
2628
}
2729
}
28-
return ans;
30+
return result;
2931
}
3032

3133
}

0 commit comments

Comments
 (0)