Skip to content

Commit 0b70750

Browse files
authored
Update 1996-the-number-of-weak-characters-in-the-game.js
1 parent 97e019e commit 0b70750

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

1996-the-number-of-weak-characters-in-the-game.js

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/**
2+
* @param {number[][]} properties
3+
* @return {number}
4+
*/
5+
const numberOfWeakCharacters = function(properties) {
6+
const props = properties, n = props.length, maxDefFromRight = Array(n)
7+
props.sort((a, b) => a[0] - b[0])
8+
for(let max = 0, i = n - 1; i >= 0; i--) {
9+
max = Math.max(max, props[i][1])
10+
maxDefFromRight[i] = max
11+
}
12+
let res = 0
13+
14+
for(let i = 0; i < n; i++) {
15+
const cur = props[i]
16+
let l = i, r = n
17+
while(l < r) {
18+
const mid = l + Math.floor((r - l) / 2)
19+
if(props[mid][0] > props[i][0]) r = mid
20+
else l = mid + 1
21+
}
22+
23+
if(l < n && maxDefFromRight[l] > props[i][1]) {
24+
res++
25+
}
26+
}
27+
28+
return res
29+
};
30+
31+
// another
32+
133
/**
234
* @param {number[][]} properties
335
* @return {number}

0 commit comments

Comments
 (0)