Skip to content

Commit 9111c59

Browse files
committed
feat: add 3rd ts solution to lc problem: No.1438
1 parent 00bdace commit 9111c59

File tree

3 files changed

+400
-1
lines changed

3 files changed

+400
-1
lines changed

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

+138
Original file line numberDiff line numberDiff line change
@@ -1081,4 +1081,142 @@ function longestSubarray(nums: number[], limit: number): number {
10811081

10821082
<!-- solution:end -->
10831083

1084+
<!-- solution:start -->
1085+
1086+
### Solution 3. Min-max heaps
1087+
1088+
<!-- tabs:start -->
1089+
1090+
#### TypeScript
1091+
1092+
```ts
1093+
function longestSubarray(nums: number[], limit: number): number {
1094+
let [ans, l, r, n] = [0, 0, 0, nums.length];
1095+
1096+
const minHeap = new MinHeap();
1097+
const maxHeap = new MaxHeap();
1098+
1099+
minHeap.push(nums[r]);
1100+
maxHeap.push(nums[r]);
1101+
1102+
while (r < n) {
1103+
const diff = maxHeap.peek()! - minHeap.peek()!;
1104+
1105+
if (diff <= limit) {
1106+
ans = Math.max(ans, minHeap.size);
1107+
r++;
1108+
if (r < n) {
1109+
minHeap.push(nums[r]);
1110+
maxHeap.push(nums[r]);
1111+
}
1112+
} else {
1113+
minHeap.remove(nums[l]);
1114+
maxHeap.remove(nums[l]);
1115+
l++;
1116+
}
1117+
}
1118+
1119+
return ans;
1120+
}
1121+
1122+
class MinHeap<T = number> {
1123+
#h: T[] = [];
1124+
1125+
get size() {
1126+
return this.#h.length;
1127+
}
1128+
1129+
push(x: T) {
1130+
const h = this.#h;
1131+
1132+
h.push(x);
1133+
if (h.length === 1) return;
1134+
1135+
let i = h.length - 1;
1136+
while (i !== undefined) {
1137+
const p = i >> 1;
1138+
if (h[p] <= h[i]) return;
1139+
this.#swap(i, p);
1140+
i = p;
1141+
}
1142+
}
1143+
1144+
pop(): T | undefined {
1145+
return this.#sinkingDown();
1146+
}
1147+
1148+
remove(v: T) {
1149+
const i = this.#h.indexOf(v);
1150+
if (i === -1) return;
1151+
this.#sinkingDown(i);
1152+
}
1153+
1154+
peek(): T | undefined {
1155+
return this.#h[0];
1156+
}
1157+
1158+
#sinkingDown(i = 0): T | undefined {
1159+
const h = this.#h;
1160+
1161+
if (h.length <= 2) {
1162+
const val = this.#h.splice(i, 1)[0];
1163+
return val;
1164+
}
1165+
1166+
this.#swap(i, h.length - 1);
1167+
const val = h.pop();
1168+
1169+
while (true) {
1170+
const l = 2 * i + 1;
1171+
const r = 2 * i + 2;
1172+
const child = h[l] && h[r] ? (h[l] <= h[r] ? l : r) : h[l] ? l : r;
1173+
1174+
if (!h[child] || h[i] <= h[child]) break;
1175+
1176+
this.#swap(i, child);
1177+
i = child;
1178+
}
1179+
1180+
return val;
1181+
}
1182+
1183+
#swap(i: number, j: number) {
1184+
const h = this.#h;
1185+
[h[i], h[j]] = [h[j], h[i]];
1186+
}
1187+
1188+
*[Symbol.iterator]() {
1189+
const h = [...this.#h];
1190+
while (this.#h.length) yield this.pop();
1191+
this.#h = h;
1192+
}
1193+
}
1194+
1195+
export class MaxHeap extends MinHeap {
1196+
push(x: number): void {
1197+
super.push(-x);
1198+
}
1199+
1200+
pop(): number | undefined {
1201+
const res = super.pop();
1202+
if (res === undefined) return;
1203+
return -res;
1204+
}
1205+
1206+
peek(): number | undefined {
1207+
const res = super.peek();
1208+
if (res === undefined) return;
1209+
return -res;
1210+
}
1211+
1212+
remove(v: number): void {
1213+
super.remove(-v);
1214+
}
1215+
}
1216+
```
1217+
1218+
<!-- tabs:end -->
1219+
1220+
<!-- solution:end -->
1221+
10841222
<!-- problem:end -->

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

+139-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Therefore, the size of the longest subarray is 2.
7171

7272
<!-- description:end -->
7373

74-
## Solutions
74+
## Solutions
7575

7676
<!-- solution:start -->
7777

@@ -1076,4 +1076,142 @@ function longestSubarray(nums: number[], limit: number): number {
10761076

10771077
<!-- solution:end -->
10781078

1079+
<!-- solution:start -->
1080+
1081+
### Solution 3. Min-max heaps
1082+
1083+
<!-- tabs:start -->
1084+
1085+
#### TypeScript
1086+
1087+
```ts
1088+
function longestSubarray(nums: number[], limit: number): number {
1089+
let [ans, l, r, n] = [0, 0, 0, nums.length];
1090+
1091+
const minHeap = new MinHeap();
1092+
const maxHeap = new MaxHeap();
1093+
1094+
minHeap.push(nums[r]);
1095+
maxHeap.push(nums[r]);
1096+
1097+
while (r < n) {
1098+
const diff = maxHeap.peek()! - minHeap.peek()!;
1099+
1100+
if (diff <= limit) {
1101+
ans = Math.max(ans, minHeap.size);
1102+
r++;
1103+
if (r < n) {
1104+
minHeap.push(nums[r]);
1105+
maxHeap.push(nums[r]);
1106+
}
1107+
} else {
1108+
minHeap.remove(nums[l]);
1109+
maxHeap.remove(nums[l]);
1110+
l++;
1111+
}
1112+
}
1113+
1114+
return ans;
1115+
}
1116+
1117+
class MinHeap<T = number> {
1118+
#h: T[] = [];
1119+
1120+
get size() {
1121+
return this.#h.length;
1122+
}
1123+
1124+
push(x: T) {
1125+
const h = this.#h;
1126+
1127+
h.push(x);
1128+
if (h.length === 1) return;
1129+
1130+
let i = h.length - 1;
1131+
while (i !== undefined) {
1132+
const p = i >> 1;
1133+
if (h[p] <= h[i]) return;
1134+
this.#swap(i, p);
1135+
i = p;
1136+
}
1137+
}
1138+
1139+
pop(): T | undefined {
1140+
return this.#sinkingDown();
1141+
}
1142+
1143+
remove(v: T) {
1144+
const i = this.#h.indexOf(v);
1145+
if (i === -1) return;
1146+
this.#sinkingDown(i);
1147+
}
1148+
1149+
peek(): T | undefined {
1150+
return this.#h[0];
1151+
}
1152+
1153+
#sinkingDown(i = 0): T | undefined {
1154+
const h = this.#h;
1155+
1156+
if (h.length <= 2) {
1157+
const val = this.#h.splice(i, 1)[0];
1158+
return val;
1159+
}
1160+
1161+
this.#swap(i, h.length - 1);
1162+
const val = h.pop();
1163+
1164+
while (true) {
1165+
const l = 2 * i + 1;
1166+
const r = 2 * i + 2;
1167+
const child = h[l] && h[r] ? (h[l] <= h[r] ? l : r) : h[l] ? l : r;
1168+
1169+
if (!h[child] || h[i] <= h[child]) break;
1170+
1171+
this.#swap(i, child);
1172+
i = child;
1173+
}
1174+
1175+
return val;
1176+
}
1177+
1178+
#swap(i: number, j: number) {
1179+
const h = this.#h;
1180+
[h[i], h[j]] = [h[j], h[i]];
1181+
}
1182+
1183+
*[Symbol.iterator]() {
1184+
const h = [...this.#h];
1185+
while (this.#h.length) yield this.pop();
1186+
this.#h = h;
1187+
}
1188+
}
1189+
1190+
export class MaxHeap extends MinHeap {
1191+
push(x: number): void {
1192+
super.push(-x);
1193+
}
1194+
1195+
pop(): number | undefined {
1196+
const res = super.pop();
1197+
if (res === undefined) return;
1198+
return -res;
1199+
}
1200+
1201+
peek(): number | undefined {
1202+
const res = super.peek();
1203+
if (res === undefined) return;
1204+
return -res;
1205+
}
1206+
1207+
remove(v: number): void {
1208+
super.remove(-v);
1209+
}
1210+
}
1211+
```
1212+
1213+
<!-- tabs:end -->
1214+
1215+
<!-- solution:end -->
1216+
10791217
<!-- problem:end -->

0 commit comments

Comments
 (0)