-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjs.js
46 lines (40 loc) · 1.07 KB
/
js.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// @ts-check
/**
* @param {number[][]} points
* @param {string} s
* @return {number}
*/
var maxPointsInsideSquare = function (points, s) {
const pointsBySquareSize = points
.map(([x, y]) => Math.max(Math.abs(x), Math.abs(y)))
.map((distance, i) => ({ distance, tag: s[i] }))
.sort((a, b) => a.distance - b.distance)
.reduce((acc, cur) => {
if (!acc[cur.distance]) acc[cur.distance] = []
acc[cur.distance].push(cur.tag)
return acc
}, {})
const seenTags = new Set()
let pointsInSquare = 0
console.log('-----')
console.log(pointsBySquareSize)
for (const pointsAtSquareSize of Object.values(pointsBySquareSize)) {
console.log(pointsInSquare)
let addPointsToCount = true
// Check if contains duplicates
for (const point of pointsAtSquareSize) {
if (seenTags.has(point)) {
addPointsToCount = false
break
} else {
seenTags.add(point)
}
}
if (addPointsToCount) {
pointsInSquare += pointsAtSquareSize.length
} else {
break
}
}
return pointsInSquare
}