Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8a78e8b

Browse files
add 1438
1 parent10875dd commit8a78e8b

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.PriorityQueue;
4+
5+
publicclass_1438 {
6+
publicstaticclassSolution1 {
7+
/**
8+
* My completely original solution on 1/19/2022.
9+
*/
10+
publicintlongestSubarray(int[]nums,intlimit) {
11+
intans =0;
12+
PriorityQueue<Integer>maxHeap =newPriorityQueue<>((a,b) ->b -a);
13+
PriorityQueue<Integer>minHeap =newPriorityQueue<>();
14+
for (intleft =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+
returnans;
29+
}
30+
}
31+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1438;
4+
importcom.fishercoder.solutions._3;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_1438Test {
11+
privatestatic_1438.Solution1solution1;
12+
privatestaticint[]nums;
13+
privatestaticintlimit;
14+
privatestaticintexpected;
15+
16+
@BeforeClass
17+
publicstaticvoidsetup() {
18+
solution1 =new_1438.Solution1();
19+
}
20+
21+
@Test
22+
publicvoidtest1() {
23+
expected =2;
24+
nums =newint[]{8,2,4,7};
25+
limit =4;
26+
assertEquals(expected,solution1.longestSubarray(nums,limit));
27+
}
28+
29+
@Test
30+
publicvoidtest2() {
31+
expected =4;
32+
nums =newint[]{10,1,2,4,7,2};
33+
limit =5;
34+
assertEquals(expected,solution1.longestSubarray(nums,limit));
35+
}
36+
37+
@Test
38+
publicvoidtest3() {
39+
expected =3;
40+
nums =newint[]{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)

[8]ページ先頭

©2009-2025 Movatter.jp