-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1438 (#2575)
No.1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
- Loading branch information
Showing
11 changed files
with
1,128 additions
and
87 deletions.
There are no files selected for viewing
414 changes: 385 additions & 29 deletions
414
...st Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
414 changes: 385 additions & 29 deletions
414
...Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README_EN.md
Large diffs are not rendered by default.
Oops, something went wrong.
31 changes: 12 additions & 19 deletions
31
...38.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 5 additions & 7 deletions
12
....Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
....Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/Solution2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Solution { | ||
public: | ||
int longestSubarray(vector<int>& nums, int limit) { | ||
auto check = [&](int k) { | ||
deque<int> min_q; | ||
deque<int> max_q; | ||
for (int i = 0; i < nums.size(); ++i) { | ||
if (!min_q.empty() && i - min_q.front() + 1 > k) { | ||
min_q.pop_front(); | ||
} | ||
if (!max_q.empty() && i - max_q.front() + 1 > k) { | ||
max_q.pop_front(); | ||
} | ||
while (!min_q.empty() && nums[min_q.back()] >= nums[i]) { | ||
min_q.pop_back(); | ||
} | ||
while (!max_q.empty() && nums[max_q.back()] <= nums[i]) { | ||
max_q.pop_back(); | ||
} | ||
min_q.push_back(i); | ||
max_q.push_back(i); | ||
if (i >= k - 1 && nums[max_q.front()] - nums[min_q.front()] <= limit) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
int l = 1, r = nums.size(); | ||
while (l < r) { | ||
int mid = (l + r + 1) >> 1; | ||
if (check(mid)) { | ||
l = mid; | ||
} else { | ||
r = mid - 1; | ||
} | ||
} | ||
return l; | ||
} | ||
}; |
94 changes: 94 additions & 0 deletions
94
...8.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/Solution2.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
func longestSubarray(nums []int, limit int) int { | ||
l, r := 0, len(nums) | ||
check := func(k int) bool { | ||
minq := Deque{} | ||
maxq := Deque{} | ||
for i, x := range nums { | ||
for !minq.Empty() && i-minq.Front()+1 > k { | ||
minq.PopFront() | ||
} | ||
for !maxq.Empty() && i-maxq.Front()+1 > k { | ||
maxq.PopFront() | ||
} | ||
for !minq.Empty() && nums[minq.Back()] >= x { | ||
minq.PopBack() | ||
} | ||
for !maxq.Empty() && nums[maxq.Back()] <= x { | ||
maxq.PopBack() | ||
} | ||
minq.PushBack(i) | ||
maxq.PushBack(i) | ||
if i >= k-1 && nums[maxq.Front()]-nums[minq.Front()] <= limit { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
for l < r { | ||
mid := (l + r + 1) >> 1 | ||
if check(mid) { | ||
l = mid | ||
} else { | ||
r = mid - 1 | ||
} | ||
} | ||
return l | ||
} | ||
|
||
// template | ||
type Deque struct{ l, r []int } | ||
|
||
func (q Deque) Empty() bool { | ||
return len(q.l) == 0 && len(q.r) == 0 | ||
} | ||
|
||
func (q Deque) Size() int { | ||
return len(q.l) + len(q.r) | ||
} | ||
|
||
func (q *Deque) PushFront(v int) { | ||
q.l = append(q.l, v) | ||
} | ||
|
||
func (q *Deque) PushBack(v int) { | ||
q.r = append(q.r, v) | ||
} | ||
|
||
func (q *Deque) PopFront() (v int) { | ||
if len(q.l) > 0 { | ||
q.l, v = q.l[:len(q.l)-1], q.l[len(q.l)-1] | ||
} else { | ||
v, q.r = q.r[0], q.r[1:] | ||
} | ||
return | ||
} | ||
|
||
func (q *Deque) PopBack() (v int) { | ||
if len(q.r) > 0 { | ||
q.r, v = q.r[:len(q.r)-1], q.r[len(q.r)-1] | ||
} else { | ||
v, q.l = q.l[0], q.l[1:] | ||
} | ||
return | ||
} | ||
|
||
func (q Deque) Front() int { | ||
if len(q.l) > 0 { | ||
return q.l[len(q.l)-1] | ||
} | ||
return q.r[0] | ||
} | ||
|
||
func (q Deque) Back() int { | ||
if len(q.r) > 0 { | ||
return q.r[len(q.r)-1] | ||
} | ||
return q.l[0] | ||
} | ||
|
||
func (q Deque) Get(i int) int { | ||
if i < len(q.l) { | ||
return q.l[len(q.l)-1-i] | ||
} | ||
return q.r[i-len(q.l)] | ||
} |
44 changes: 44 additions & 0 deletions
44
...Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/Solution2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class Solution { | ||
private int[] nums; | ||
private int limit; | ||
|
||
public int longestSubarray(int[] nums, int limit) { | ||
this.nums = nums; | ||
this.limit = limit; | ||
int l = 1, r = nums.length; | ||
while (l < r) { | ||
int mid = (l + r + 1) >> 1; | ||
if (check(mid)) { | ||
l = mid; | ||
} else { | ||
r = mid - 1; | ||
} | ||
} | ||
return l; | ||
} | ||
|
||
private boolean check(int k) { | ||
Deque<Integer> minQ = new ArrayDeque<>(); | ||
Deque<Integer> maxQ = new ArrayDeque<>(); | ||
for (int i = 0; i < nums.length; ++i) { | ||
if (!minQ.isEmpty() && i - minQ.peekFirst() + 1 > k) { | ||
minQ.pollFirst(); | ||
} | ||
if (!maxQ.isEmpty() && i - maxQ.peekFirst() + 1 > k) { | ||
maxQ.pollFirst(); | ||
} | ||
while (!minQ.isEmpty() && nums[minQ.peekLast()] >= nums[i]) { | ||
minQ.pollLast(); | ||
} | ||
while (!maxQ.isEmpty() && nums[maxQ.peekLast()] <= nums[i]) { | ||
maxQ.pollLast(); | ||
} | ||
minQ.offer(i); | ||
maxQ.offer(i); | ||
if (i >= k - 1 && nums[maxQ.peekFirst()] - nums[minQ.peekFirst()] <= limit) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...8.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/Solution2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution: | ||
def longestSubarray(self, nums: List[int], limit: int) -> int: | ||
def check(k: int) -> bool: | ||
min_q = deque() | ||
max_q = deque() | ||
for i, x in enumerate(nums): | ||
if min_q and i - min_q[0] + 1 > k: | ||
min_q.popleft() | ||
if max_q and i - max_q[0] + 1 > k: | ||
max_q.popleft() | ||
while min_q and nums[min_q[-1]] >= x: | ||
min_q.pop() | ||
while max_q and nums[max_q[-1]] <= x: | ||
max_q.pop() | ||
min_q.append(i) | ||
max_q.append(i) | ||
if i >= k - 1 and nums[max_q[0]] - nums[min_q[0]] <= limit: | ||
return True | ||
return False | ||
|
||
l, r = 1, len(nums) | ||
while l < r: | ||
mid = (l + r + 1) >> 1 | ||
if check(mid): | ||
l = mid | ||
else: | ||
r = mid - 1 | ||
return l |
Oops, something went wrong.