Skip to content

Commit 00bdace

Browse files
committed
refactor: update 2nd ts solution to lc problem: No.1438
1 parent 6c44148 commit 00bdace

File tree

3 files changed

+48
-369
lines changed

3 files changed

+48
-369
lines changed

solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README.md

Lines changed: 16 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,137 +1050,30 @@ func (q Deque) Get(i int) int {
10501050

10511051
```ts
10521052
function longestSubarray(nums: number[], limit: number): number {
1053-
const n = nums.length;
1054-
let [l, r] = [0, n];
1055-
const check = (k: number): boolean => {
1056-
const minq = new Deque<number>();
1057-
const maxq = new Deque<number>();
1058-
for (let i = 0; i < n; ++i) {
1059-
while (!minq.isEmpty() && i - minq.frontValue()! + 1 > k) {
1060-
minq.popFront();
1061-
}
1062-
while (!maxq.isEmpty() && i - maxq.frontValue()! + 1 > k) {
1063-
maxq.popFront();
1064-
}
1065-
while (!minq.isEmpty() && nums[minq.backValue()!] >= nums[i]) {
1066-
minq.popBack();
1067-
}
1068-
while (!maxq.isEmpty() && nums[maxq.backValue()!] <= nums[i]) {
1069-
maxq.popBack();
1070-
}
1071-
minq.pushBack(i);
1072-
maxq.pushBack(i);
1073-
if (i >= k - 1 && nums[maxq.frontValue()!] - nums[minq.frontValue()!] <= limit) {
1074-
return true;
1075-
}
1076-
}
1077-
return false;
1078-
};
1079-
while (l < r) {
1080-
const mid = (l + r + 1) >> 1;
1081-
if (check(mid)) {
1082-
l = mid;
1083-
} else {
1084-
r = mid - 1;
1085-
}
1086-
}
1087-
return l;
1088-
}
1089-
1090-
class Node<T> {
1091-
value: T;
1092-
next: Node<T> | null;
1093-
prev: Node<T> | null;
1094-
1095-
constructor(value: T) {
1096-
this.value = value;
1097-
this.next = null;
1098-
this.prev = null;
1099-
}
1100-
}
1101-
1102-
class Deque<T> {
1103-
private front: Node<T> | null;
1104-
private back: Node<T> | null;
1105-
private size: number;
1106-
1107-
constructor() {
1108-
this.front = null;
1109-
this.back = null;
1110-
this.size = 0;
1111-
}
1053+
let ans = 0;
1054+
let l = 0;
1055+
const maxQueue: number[] = [];
1056+
const minQueue: number[] = [];
11121057

1113-
pushFront(val: T): void {
1114-
const newNode = new Node(val);
1115-
if (this.isEmpty()) {
1116-
this.front = newNode;
1117-
this.back = newNode;
1118-
} else {
1119-
newNode.next = this.front;
1120-
this.front!.prev = newNode;
1121-
this.front = newNode;
1122-
}
1123-
this.size++;
1124-
}
1058+
for (let r = 0; r < nums.length; r++) {
1059+
while (maxQueue.length && nums[maxQueue.at(-1)!] < nums[r]) maxQueue.pop();
1060+
while (minQueue.length && nums[minQueue.at(-1)!] > nums[r]) minQueue.pop();
1061+
maxQueue.push(r);
1062+
minQueue.push(r);
11251063

1126-
pushBack(val: T): void {
1127-
const newNode = new Node(val);
1128-
if (this.isEmpty()) {
1129-
this.front = newNode;
1130-
this.back = newNode;
1131-
} else {
1132-
newNode.prev = this.back;
1133-
this.back!.next = newNode;
1134-
this.back = newNode;
1135-
}
1136-
this.size++;
1137-
}
1064+
const diff = nums[maxQueue[0]] - nums[minQueue[0]];
11381065

1139-
popFront(): T | undefined {
1140-
if (this.isEmpty()) {
1141-
return undefined;
1142-
}
1143-
const value = this.front!.value;
1144-
this.front = this.front!.next;
1145-
if (this.front !== null) {
1146-
this.front.prev = null;
1066+
if (diff <= limit) {
1067+
ans = Math.max(ans, r - l + 1);
11471068
} else {
1148-
this.back = null;
1149-
}
1150-
this.size--;
1151-
return value;
1152-
}
1069+
l++;
11531070

1154-
popBack(): T | undefined {
1155-
if (this.isEmpty()) {
1156-
return undefined;
1071+
if (maxQueue[0] < l) maxQueue.shift();
1072+
if (minQueue[0] < l) minQueue.shift();
11571073
}
1158-
const value = this.back!.value;
1159-
this.back = this.back!.prev;
1160-
if (this.back !== null) {
1161-
this.back.next = null;
1162-
} else {
1163-
this.front = null;
1164-
}
1165-
this.size--;
1166-
return value;
1167-
}
1168-
1169-
frontValue(): T | undefined {
1170-
return this.front?.value;
1171-
}
1172-
1173-
backValue(): T | undefined {
1174-
return this.back?.value;
1175-
}
1176-
1177-
getSize(): number {
1178-
return this.size;
11791074
}
11801075

1181-
isEmpty(): boolean {
1182-
return this.size === 0;
1183-
}
1076+
return ans;
11841077
}
11851078
```
11861079

solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README_EN.md

Lines changed: 16 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,137 +1045,30 @@ func (q Deque) Get(i int) int {
10451045

10461046
```ts
10471047
function longestSubarray(nums: number[], limit: number): number {
1048-
const n = nums.length;
1049-
let [l, r] = [0, n];
1050-
const check = (k: number): boolean => {
1051-
const minq = new Deque<number>();
1052-
const maxq = new Deque<number>();
1053-
for (let i = 0; i < n; ++i) {
1054-
while (!minq.isEmpty() && i - minq.frontValue()! + 1 > k) {
1055-
minq.popFront();
1056-
}
1057-
while (!maxq.isEmpty() && i - maxq.frontValue()! + 1 > k) {
1058-
maxq.popFront();
1059-
}
1060-
while (!minq.isEmpty() && nums[minq.backValue()!] >= nums[i]) {
1061-
minq.popBack();
1062-
}
1063-
while (!maxq.isEmpty() && nums[maxq.backValue()!] <= nums[i]) {
1064-
maxq.popBack();
1065-
}
1066-
minq.pushBack(i);
1067-
maxq.pushBack(i);
1068-
if (i >= k - 1 && nums[maxq.frontValue()!] - nums[minq.frontValue()!] <= limit) {
1069-
return true;
1070-
}
1071-
}
1072-
return false;
1073-
};
1074-
while (l < r) {
1075-
const mid = (l + r + 1) >> 1;
1076-
if (check(mid)) {
1077-
l = mid;
1078-
} else {
1079-
r = mid - 1;
1080-
}
1081-
}
1082-
return l;
1083-
}
1084-
1085-
class Node<T> {
1086-
value: T;
1087-
next: Node<T> | null;
1088-
prev: Node<T> | null;
1089-
1090-
constructor(value: T) {
1091-
this.value = value;
1092-
this.next = null;
1093-
this.prev = null;
1094-
}
1095-
}
1096-
1097-
class Deque<T> {
1098-
private front: Node<T> | null;
1099-
private back: Node<T> | null;
1100-
private size: number;
1101-
1102-
constructor() {
1103-
this.front = null;
1104-
this.back = null;
1105-
this.size = 0;
1106-
}
1048+
let ans = 0;
1049+
let l = 0;
1050+
const maxQueue: number[] = [];
1051+
const minQueue: number[] = [];
11071052

1108-
pushFront(val: T): void {
1109-
const newNode = new Node(val);
1110-
if (this.isEmpty()) {
1111-
this.front = newNode;
1112-
this.back = newNode;
1113-
} else {
1114-
newNode.next = this.front;
1115-
this.front!.prev = newNode;
1116-
this.front = newNode;
1117-
}
1118-
this.size++;
1119-
}
1053+
for (let r = 0; r < nums.length; r++) {
1054+
while (maxQueue.length && nums[maxQueue.at(-1)!] < nums[r]) maxQueue.pop();
1055+
while (minQueue.length && nums[minQueue.at(-1)!] > nums[r]) minQueue.pop();
1056+
maxQueue.push(r);
1057+
minQueue.push(r);
11201058

1121-
pushBack(val: T): void {
1122-
const newNode = new Node(val);
1123-
if (this.isEmpty()) {
1124-
this.front = newNode;
1125-
this.back = newNode;
1126-
} else {
1127-
newNode.prev = this.back;
1128-
this.back!.next = newNode;
1129-
this.back = newNode;
1130-
}
1131-
this.size++;
1132-
}
1059+
const diff = nums[maxQueue[0]] - nums[minQueue[0]];
11331060

1134-
popFront(): T | undefined {
1135-
if (this.isEmpty()) {
1136-
return undefined;
1137-
}
1138-
const value = this.front!.value;
1139-
this.front = this.front!.next;
1140-
if (this.front !== null) {
1141-
this.front.prev = null;
1061+
if (diff <= limit) {
1062+
ans = Math.max(ans, r - l + 1);
11421063
} else {
1143-
this.back = null;
1144-
}
1145-
this.size--;
1146-
return value;
1147-
}
1064+
l++;
11481065

1149-
popBack(): T | undefined {
1150-
if (this.isEmpty()) {
1151-
return undefined;
1066+
if (maxQueue[0] < l) maxQueue.shift();
1067+
if (minQueue[0] < l) minQueue.shift();
11521068
}
1153-
const value = this.back!.value;
1154-
this.back = this.back!.prev;
1155-
if (this.back !== null) {
1156-
this.back.next = null;
1157-
} else {
1158-
this.front = null;
1159-
}
1160-
this.size--;
1161-
return value;
1162-
}
1163-
1164-
frontValue(): T | undefined {
1165-
return this.front?.value;
1166-
}
1167-
1168-
backValue(): T | undefined {
1169-
return this.back?.value;
1170-
}
1171-
1172-
getSize(): number {
1173-
return this.size;
11741069
}
11751070

1176-
isEmpty(): boolean {
1177-
return this.size === 0;
1178-
}
1071+
return ans;
11791072
}
11801073
```
11811074

0 commit comments

Comments
 (0)