Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1438
Browse files Browse the repository at this point in the history
No.1438.Longest Continuous Subarray With Absolute Diff Less Than or
Equal to Limit
  • Loading branch information
yanglbme committed Apr 12, 2024
1 parent 2c6cd87 commit 4aa1487
Show file tree
Hide file tree
Showing 11 changed files with 1,128 additions and 87 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
func longestSubarray(nums []int, limit int) (ans int) {
tm := treemap.NewWithIntComparator()
j := 0
for i, v := range nums {
if x, ok := tm.Get(v); ok {
tm.Put(v, x.(int)+1)
merge := func(st *redblacktree.Tree[int, int], x, v int) {
c, _ := st.Get(x)
if c+v == 0 {
st.Remove(x)
} else {
tm.Put(v, 1)
st.Put(x, c+v)
}
for {
a, _ := tm.Min()
b, _ := tm.Max()
if b.(int)-a.(int) > limit {
if x, _ := tm.Get(nums[j]); x.(int) == 1 {
tm.Remove(nums[j])
} else {
tm.Put(nums[j], x.(int)-1)
}
j++
} else {
break
}
}
st := redblacktree.New[int, int]()
j := 0
for i, x := range nums {
merge(st, x, 1)
for ; st.Right().Key-st.Left().Key > limit; j++ {
merge(st, nums[j], -1)
}
ans = max(ans, i-j+1)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
class Solution {
public int longestSubarray(int[] nums, int limit) {
TreeMap<Integer, Integer> tm = new TreeMap<>();
int ans = 0, j = 0;
for (int i = 0; i < nums.length; ++i) {
tm.put(nums[i], tm.getOrDefault(nums[i], 0) + 1);
while (tm.lastKey() - tm.firstKey() > limit) {
tm.put(nums[j], tm.get(nums[j]) - 1);
if (tm.get(nums[j]) == 0) {
int ans = 0;
for (int i = 0, j = 0; i < nums.length; ++i) {
tm.merge(nums[i], 1, Integer::sum);
for (; tm.lastKey() - tm.firstKey() > limit; ++j) {
if (tm.merge(nums[j], -1, Integer::sum) == 0) {
tm.remove(nums[j]);
}
++j;
}
ans = Math.max(ans, i - j + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Solution:
def longestSubarray(self, nums: List[int], limit: int) -> int:
sl = SortedList()
ans = j = 0
for i, v in enumerate(nums):
sl.add(v)
for i, x in enumerate(nums):
sl.add(x)
while sl[-1] - sl[0] > limit:
sl.remove(nums[j])
j += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function longestSubarray(nums: number[], limit: number): number {
let j = 0;
for (let i = 0; i < nums.length; ++i) {
ts.add(nums[i]);
while (ts.last() - ts.first() > limit) {
while (ts.last()! - ts.first()! > limit) {
ts.delete(nums[j++]);
}
ans = Math.max(ans, i - j + 1);
Expand Down
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;
}
};
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)]
}
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;
}
}
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
Loading

0 comments on commit 4aa1487

Please sign in to comment.