File tree 1 file changed +9
-7
lines changed
src/main/java/io/github/spannm/leetcode/lc1/lc1400
1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change 8
8
class Problem1438 extends LeetcodeProblem {
9
9
10
10
int longestSubarray (int [] _nums , int _limit ) {
11
- int ans = 0 ;
11
+ int len = _nums .length ;
12
+ int result = 0 ;
12
13
Queue <Integer > maxHeap = new PriorityQueue <>((a , b ) -> b - a );
13
14
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 ;
17
18
}
18
19
maxHeap .offer (_nums [right ]);
19
20
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 );
22
24
} else {
23
25
maxHeap .remove (_nums [left ]);
24
26
minHeap .remove (_nums [left ]);
25
27
left ++;
26
28
}
27
29
}
28
- return ans ;
30
+ return result ;
29
31
}
30
32
31
33
}
You can’t perform that action at this time.
0 commit comments