File tree Expand file tree Collapse file tree 1 file changed +9
-7
lines changed
src/main/java/io/github/spannm/leetcode/lc1/lc1400 Expand file tree Collapse file tree 1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change 88class 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}
You can’t perform that action at this time.
0 commit comments