Skip to content

Commit 7a7306b

Browse files
authored
Create 1996-the-number-of-weak-characters-in-the-game.js
1 parent 5a3d816 commit 7a7306b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[][]} properties
3+
* @return {number}
4+
*/
5+
const numberOfWeakCharacters = function(properties) {
6+
if (properties == null || properties.length == 0) {
7+
return 0;
8+
}
9+
properties.sort((o1, o2) => {
10+
if (o1[0] == o2[0]) {
11+
return o1[1] - o2[1];
12+
}
13+
return o1[0] - o2[0];
14+
});
15+
const { max } = Math
16+
let mmax = Array(1e5 + 10).fill( 0);
17+
let ans = 0;
18+
let n = properties.length;
19+
for (let i = n - 1; i >= 0; i--) mmax[properties[i][0]] = max(properties[i][1], mmax[properties[i][0]]);
20+
for (let i = 1e5; i >= 1; i--) mmax[i] = max(mmax[i], mmax[i + 1]);
21+
for (let i = 0; i < n; i++) {
22+
if (mmax[properties[i][0] + 1] > properties[i][1]) ans++;
23+
}
24+
return ans;
25+
};

0 commit comments

Comments
 (0)