1
var numberOfWeakCharacters = function (properties) {
2
// sort strongest to weakest
3
properties.sort((a, b) => {
4
if (b[0] - a[0]) {
5
return b[0] - a[0];
6
}
7
return b[1] - a[1];
8
});
9

10
// map all the unique index 0 values to a (descending)
11
// sorted array
12
const uniqueZeroIndexToOneIndex = new Map();
13
for (const [a, b] of properties) {
14
if (!uniqueZeroIndexToOneIndex.has(a)) {
15
uniqueZeroIndexToOneIndex.set(a, []);
16
}
17
uniqueZeroIndexToOneIndex.get(a).push(b);
18
}
19

20
// get all the unique index 0 values
21
const nums = [...uniqueZeroIndexToOneIndex.keys()];
22

23
let maxValue = -1;
24
let numWeakCharacters = 0;
25
for (let i = 0; i < nums.length; i++) {
26
// skip key in case if we deleted the key
27
if (!uniqueZeroIndexToOneIndex.has(nums[i])) continue;
28

29
const maxValOfI = uniqueZeroIndexToOneIndex.get(nums[i])[0];
30
if (maxValOfI <= maxValue) continue;
31
maxValue = maxValOfI;
32

33
for (let j = i + 1; j < nums.length; j++) {
34
// skip key in case if we deleted the key
35
if (!uniqueZeroIndexToOneIndex.has(nums[j])) continue;
36

37
// valuesOfJ will be sorted in descending order
38
const valuesOfJ = uniqueZeroIndexToOneIndex.get(nums[j]);
39

40
// pop off all the weak values and add to numWeakCharacters
41
for (let k = valuesOfJ.length - 1; k >= 0; k--) {
42
if (maxValue > valuesOfJ[k]) {
43
valuesOfJ.pop();
44
numWeakCharacters++;
45
} else {
46
// we won't be able to find any more weak characters
47
// in valuesOfJ
48
break;
49
}
50
}
51
if (valuesOfJ.length === 0) {
52
// delete the key if all the values were weak characters
53
uniqueZeroIndexToOneIndex.delete(nums[j]);
54
} else {
55
// if there is a stronger character updated it so we can
56
// eliminate more characters
57
maxValue = Math.max(valuesOfJ[0], maxValue);
58
}
59
}
60
}
61
return numWeakCharacters;
62
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0