Skip to content

Commit cca8c2e

Browse files
committed
测试
1 parent 6d83fc3 commit cca8c2e

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import maxAscendingSum from "./index.ts";
3+
Deno.test("maximum-ascending-subarray-sum", () => {
4+
assertEquals(
5+
[
6+
[10, 20, 30, 5, 10, 50],
7+
[10, 20, 30, 40, 50],
8+
[12, 17, 15, 13, 10, 11, 12],
9+
].map(maxAscendingSum),
10+
[65, 150, 33],
11+
);
12+
});
Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import { BinaryIndexTree } from "../rank-from-stream-lcci/BinaryIndexTree.ts";
2-
export default function numberOfPairs(
3-
nums1: number[],
4-
nums2: number[],
5-
diff: number
6-
): number {
7-
for (const [i, x] of nums2.entries()) {
8-
nums1[i] -= x;
9-
}
10-
const b = Array.from(nums1).sort((a, b) => a - b);
11-
let ans = 0;
12-
const t = new BinaryIndexTree(nums1.length);
13-
for (const x of nums1) {
14-
ans += t.query(lowerBound(b, x + diff + 1));
15-
t.update(lowerBound(b, x) + 1, +1);
16-
}
17-
return ans;
18-
}
19-
export function lowerBound(a: number[], x: number) {
20-
let left = 0,
21-
right = a.length;
22-
while (left < right) {
23-
const mid = Math.floor(left + (right - left) / 2);
24-
if (a[mid] < x) left = mid + 1;
25-
else right = mid;
26-
}
27-
return left;
28-
}
1+
import { BinaryIndexTree } from "../rank-from-stream-lcci/BinaryIndexTree.ts";
2+
export default function numberOfPairs(
3+
nums1: number[],
4+
nums2: number[],
5+
diff: number,
6+
): number {
7+
for (const [i, x] of nums2.entries()) {
8+
nums1[i] -= x;
9+
}
10+
const b = Array.from(nums1).sort((a, b) => a - b);
11+
let ans = 0;
12+
const t = new BinaryIndexTree(nums1.length);
13+
for (const x of nums1) {
14+
ans += t.query(lowerBound(b, x + diff + 1));
15+
t.update(lowerBound(b, x) + 1, +1);
16+
}
17+
return ans;
18+
}
19+
export function lowerBound(a: number[], x: number) {
20+
let left = 0,
21+
right = a.length;
22+
while (left < right) {
23+
const mid = Math.floor(left + (right - left) / 2);
24+
if (a[mid] < x) left = mid + 1;
25+
else right = mid;
26+
}
27+
return left;
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import numberOfPairs from "./index.ts";
3+
Deno.test("number-of-pairs-satisfying-inequality", () => {
4+
assertEquals(
5+
[
6+
[[3, 2, 5], [2, 2, 1], 1],
7+
[[3, -1], [-2, 2], -1],
8+
//@ts-ignore
9+
].map((a) => numberOfPairs(...a)),
10+
[3, 0],
11+
);
12+
});

rank-from-stream-lcci/BinaryIndexTree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class BinaryIndexTree<T = number> {
1515
operation: (a: number, b: number) => a + b,
1616
//@ts-ignore
1717
defaultValue: 0,
18-
}
18+
},
1919
) {
2020
const { defaultValue, operation } = options;
2121
this.#tree = Array(size + 1).fill(defaultValue);

0 commit comments

Comments
 (0)