Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8a78e8b

Browse files
add 1438
1 parent 10875dd commit 8a78e8b

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ _If you like this project, please leave me a star._ ★
308308
|1446|[Consecutive Characters](https://leetcode.com/problems/consecutive-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1446.java) | |Easy|String|
309309
|1441|[Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1441.java) | |Easy|Stack|
310310
|1439|[Find the Kth Smallest Sum of a Matrix With Sorted Rows](https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1439.java) | |Hard|Array, Binary Search, PriorityQueue, Matrix|
311+
|1438|[Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1438.java) | |Medium|Array, Queue, Sliding Window, PriorityQueue, Monotonic Queue|
311312
|1437|[Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1437.java) | |Medium|Array|
312313
|1436|[Destination City](https://leetcode.com/problems/destination-city/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1436.java) | |Easy|String|
313314
|1432|[Max Difference You Can Get From Changing an Integer](https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1432.java) | |Medium|String|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class _1438 {
6+
public static class Solution1 {
7+
/**
8+
* My completely original solution on 1/19/2022.
9+
*/
10+
public int longestSubarray(int[] nums, int limit) {
11+
int ans = 0;
12+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
13+
PriorityQueue<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;
17+
}
18+
maxHeap.offer(nums[right]);
19+
minHeap.offer(nums[right]);
20+
if (!maxHeap.isEmpty() && !minHeap.isEmpty() && (maxHeap.peek() - minHeap.peek() <= limit)) {
21+
ans = Math.max(ans, right - left + 1);
22+
} else {
23+
maxHeap.remove(nums[left]);
24+
minHeap.remove(nums[left]);
25+
left++;
26+
}
27+
}
28+
return ans;
29+
}
30+
}
31+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1438;
4+
import com.fishercoder.solutions._3;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1438Test {
11+
private static _1438.Solution1 solution1;
12+
private static int[] nums;
13+
private static int limit;
14+
private static int expected;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _1438.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
expected = 2;
24+
nums = new int[]{8, 2, 4, 7};
25+
limit = 4;
26+
assertEquals(expected, solution1.longestSubarray(nums, limit));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
expected = 4;
32+
nums = new int[]{10, 1, 2, 4, 7, 2};
33+
limit = 5;
34+
assertEquals(expected, solution1.longestSubarray(nums, limit));
35+
}
36+
37+
@Test
38+
public void test3() {
39+
expected = 3;
40+
nums = new int[]{4, 2, 2, 2, 4, 4, 2, 2};
41+
limit = 0;
42+
assertEquals(expected, solution1.longestSubarray(nums, limit));
43+
}
44+
45+
}

0 commit comments

Comments
 (0)