Skip to content

Commit dfc4fb4

Browse files
committed
feat: finished problems for next week
1 parent ffe5cae commit dfc4fb4

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @param {number[]} instructions
3+
* @return {number}
4+
*/
5+
const createSortedArray = function (instructions) {
6+
const n = instructions.length
7+
instructions = instructions.reverse().map((a, idx) => [a, idx])
8+
const a = Array(n).fill(0); const b = Array(n).fill(0)
9+
const count = (i, j) => {
10+
if (i === j) return
11+
const t = i + j >> 1
12+
count(i, t)
13+
count(t + 1, j)
14+
for (let x = i; x <= t; x++) {
15+
const p = instructions[x][0]; const q = instructions[x][0]
16+
// >p 的第一个数
17+
let l = t + 1; let r = j + 1
18+
while (l < r) {
19+
const mid = l + r >> 1
20+
if (mid === j + 1 || instructions[mid][0] > p) r = mid
21+
else l = mid + 1
22+
}
23+
const p1 = l
24+
// < q 的最后一个数
25+
l = t, r = j
26+
while (l < r) {
27+
const mid = l + r + 1 >> 1
28+
if (mid === t || instructions[mid][0] < q) l = mid
29+
else r = mid - 1
30+
}
31+
const p2 = l
32+
const idx = instructions[x][1]
33+
a[idx] += j - p1 + 1
34+
b[idx] += p2 - t
35+
}
36+
37+
let x = i; let y = t + 1; const tmp = []
38+
while (x <= t && y <= j) {
39+
if (instructions[x][0] <= instructions[y][0]) tmp.push(instructions[x++])
40+
else tmp.push(instructions[y++])
41+
}
42+
while (x <= t) tmp.push(instructions[x++])
43+
while (y <= j) tmp.push(instructions[y++])
44+
for (let k = i; k <= j; k++) instructions[k] = tmp[k - i]
45+
}
46+
count(0, n - 1)
47+
48+
let ans = 0
49+
for (let i = 0; i < n; i++) {
50+
ans += Math.min(a[i], b[i])
51+
ans %= 1e9 + 7
52+
}
53+
54+
return ans
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} edgeList
4+
* @param {number[][]} queries
5+
* @return {boolean[]}
6+
*/
7+
const distanceLimitedPathsExist = function (n, edgeList, queries) {
8+
const fa = Array(n).fill(0).map((_, idx) => idx)
9+
edgeList.sort((a, b) => a[2] - b[2])
10+
const ans = []; let i = 0; const m = edgeList.length
11+
const get = (x) => {
12+
if (x === fa[x]) return x
13+
return fa[x] = get(fa[x])
14+
}
15+
const merge = (x, y) => {
16+
const [fx, fy] = [get(x), get(y)]
17+
fa[fx] = fy
18+
}
19+
queries.map((q, idx) => [...q, idx]).sort((a, b) => a[2] - b[2])
20+
.forEach(([x, y, d, idx]) => {
21+
while (i < m && edgeList[i][2] < d) {
22+
merge(edgeList[i][0], edgeList[i][1])
23+
i++
24+
}
25+
ans[idx] = get(x) === get(y)
26+
})
27+
return ans
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} lower
4+
* @param {number} upper
5+
* @return {number}
6+
*/
7+
var countRangeSum = function(nums, lower, upper) {
8+
const sum = [0], n = nums.length
9+
for (let i = 0; i < n; i++) sum[i+1] = sum[i]+nums[i]
10+
let ans = 0
11+
12+
const count = (i, j) => {
13+
if (i === j) return
14+
const t = i+j>>1
15+
count(i, t)
16+
count(t+1, j)
17+
for (let x = i; x <= t; x++) {
18+
let p = sum[x]+lower, q = sum[x]+upper
19+
// >=p 的第一个数
20+
let l = t+1, r = j+1
21+
while (l < r) {
22+
const mid = l+r>>1
23+
if (mid === j+1 || sum[mid] >= p) r = mid
24+
else l = mid+1
25+
}
26+
let p1 = l
27+
// <= q 的最后一个数
28+
l = t, r = j
29+
while (l < r) {
30+
const mid = l+r+1>>1
31+
if (mid === t || sum[mid] <= q) l = mid
32+
else r = mid-1
33+
}
34+
let p2 = l
35+
// console.log(sum, i, j, x, p1, p2)
36+
ans += p2-p1+1
37+
}
38+
39+
let x = i, y = t+1, tmp = []
40+
while (x <= t && y <= j) {
41+
if (sum[x] <= sum[y]) tmp.push(sum[x++])
42+
else tmp.push(sum[y++])
43+
}
44+
while (x <= t) tmp.push(sum[x++])
45+
while (y <= j) tmp.push(sum[y++])
46+
for (let k = i; k <= j; k++) sum[k] = tmp[k-i]
47+
}
48+
count(0, n)
49+
50+
return ans
51+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const reversePairs = function (nums) {
6+
const n = nums.length
7+
let ans = 0
8+
const count = (i, j) => {
9+
if (i === j) return
10+
const t = i + j >> 1
11+
count(i, t)
12+
count(t + 1, j)
13+
for (let x = i; x <= t; x++) {
14+
const q = nums[x] / 2
15+
// < q 的最后一个数
16+
l = t, r = j
17+
while (l < r) {
18+
const mid = l + r + 1 >> 1
19+
if (mid === t || nums[mid] < q) l = mid
20+
else r = mid - 1
21+
}
22+
ans += l - t
23+
}
24+
25+
let x = i; let y = t + 1; const tmp = []
26+
while (x <= t && y <= j) {
27+
if (nums[x] <= nums[y]) tmp.push(nums[x++])
28+
else tmp.push(nums[y++])
29+
}
30+
while (x <= t) tmp.push(nums[x++])
31+
while (y <= j) tmp.push(nums[y++])
32+
for (let k = i; k <= j; k++) nums[k] = tmp[k - i]
33+
}
34+
count(0, n - 1)
35+
return ans
36+
}

0 commit comments

Comments
 (0)