deno.land / x / masx200_leetcode_test@10.6.5 / shu-zu-zhong-de-ni-xu-dui-lcof / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { le as binarySearch } from "https://esm.sh/@masx200/binary-search-bounds@2.0.5";
import { BinaryIndexTree } from "../rank-from-stream-lcci/index.ts";
export default function reversePairs(nums: number[]): number { const n = nums.length; const tmp = Array.from(nums); tmp.sort((a, b) => a - b); for (let i = 0; i < n; ++i) { nums[i] = binarySearch(tmp, nums[i]) + 1; } const bit = new BinaryIndexTree(n); let ans = 0; for (let i = n - 1; i >= 0; i--) { ans += bit.query(nums[i] - 1); bit.update(nums[i], 1); } return ans;}
masx200_leetcode_test

Version Info

Tagged at
a year ago