1
var numberOfWeakCharacters = function (properties) {2
// sort strongest to weakest3
properties.sort((a, b) => {10
// map all the unique index 0 values to a (descending)12
const uniqueZeroIndexToOneIndex = new Map();13
for (const [a, b] of properties) {14
if (!uniqueZeroIndexToOneIndex.has(a)) {15
uniqueZeroIndexToOneIndex.set(a, []);17
uniqueZeroIndexToOneIndex.get(a).push(b);20
// get all the unique index 0 values21
const nums = [...uniqueZeroIndexToOneIndex.keys()];24
let numWeakCharacters = 0;25
for (let i = 0; i < nums.length; i++) {26
// skip key in case if we deleted the key27
if (!uniqueZeroIndexToOneIndex.has(nums[i])) continue;29
const maxValOfI = uniqueZeroIndexToOneIndex.get(nums[i])[0];30
if (maxValOfI <= maxValue) continue;33
for (let j = i + 1; j < nums.length; j++) {34
// skip key in case if we deleted the key35
if (!uniqueZeroIndexToOneIndex.has(nums[j])) continue;37
// valuesOfJ will be sorted in descending order38
const valuesOfJ = uniqueZeroIndexToOneIndex.get(nums[j]);40
// pop off all the weak values and add to numWeakCharacters41
for (let k = valuesOfJ.length - 1; k >= 0; k--) {42
if (maxValue > valuesOfJ[k]) {46
// we won't be able to find any more weak characters51
if (valuesOfJ.length === 0) {52
// delete the key if all the values were weak characters53
uniqueZeroIndexToOneIndex.delete(nums[j]);55
// if there is a stronger character updated it so we can56
// eliminate more characters57
maxValue = Math.max(valuesOfJ[0], maxValue);61
return numWeakCharacters;