Skip to content

Commit 0f19100

Browse files
authored
Create 3382-maximum-area-rectangle-with-point-constraints-ii.js
1 parent 95c0995 commit 0f19100

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @param {number[]} xCoord
3+
* @param {number[]} yCoord
4+
* @return {number}
5+
*/
6+
var maxRectangleArea = function (xCoord, yCoord) {
7+
const n = xCoord.length
8+
const co = []
9+
const sy = imap(yCoord)
10+
11+
for (let i = 0; i < n; i++) {
12+
co.push([xCoord[i], binarySearch(sy, yCoord[i])])
13+
}
14+
co.sort((a, b) => a[0] - b[0] || a[1] - b[1])
15+
16+
let result = -1
17+
const map = new Map()
18+
const mapX = new Map()
19+
const ft = new Array(sy.length + 1).fill(0)
20+
for (let i = 0; i < co.length; i++) {
21+
addFenwick(ft, co[i][1], 1)
22+
23+
if (i - 1 >= 0 && co[i][0] === co[i - 1][0]) {
24+
const yc = (BigInt(co[i][1]) << 32n) | BigInt(co[i - 1][1])
25+
const aft = sumFenwick(ft, co[i][1]) - sumFenwick(ft, co[i - 1][1] - 1)
26+
27+
if (map.has(yc)) {
28+
const bef = map.get(yc)
29+
if (aft === bef + 2) {
30+
const x = mapX.get(yc)
31+
const S =
32+
BigInt(co[i][0] - x) * BigInt(sy[co[i][1]] - sy[co[i - 1][1]])
33+
result = Number(BigInt(result) > S ? result : S)
34+
}
35+
}
36+
37+
map.set(yc, aft)
38+
mapX.set(yc, co[i][0])
39+
}
40+
}
41+
42+
return result
43+
44+
function sumFenwick(ft, i) {
45+
let sum = 0
46+
for (i += 1; i > 0; i -= i & -i) {
47+
sum += ft[i]
48+
}
49+
return sum
50+
}
51+
52+
function addFenwick(ft, i, v) {
53+
if (v === 0 || i < 0) return
54+
for (i += 1; i < ft.length; i += i & -i) {
55+
ft[i] += v
56+
}
57+
}
58+
59+
function imap(a) {
60+
const imap = Array.from(a)
61+
imap.sort((a, b) => a - b)
62+
let p = 1
63+
64+
for (let i = 1; i < imap.length; i++) {
65+
if (imap[i] !== imap[p - 1]) imap[p++] = imap[i]
66+
}
67+
68+
return imap.slice(0, p)
69+
}
70+
71+
function binarySearch(nums, target) {
72+
let left = 0
73+
let right = nums.length - 1
74+
while (left <= right) {
75+
const mid = (left + right) >> 1
76+
if (nums[mid] === target) return mid
77+
if (nums[mid] < target) {
78+
left = mid + 1
79+
} else {
80+
right = mid - 1
81+
}
82+
}
83+
return -1
84+
}
85+
}

0 commit comments

Comments
 (0)