problem: You are playing a game that contains multiple characters, and each of the characters has two main properties: attack and defense. You are given a 2D integer array properties where properties[i] = [attacki, defensei] represents the properties of the ith character in the game.
A character is said to be weak if any other character has both attack and defense levels strictly greater than this character's attack and defense levels. More formally, a character i is said to be weak if there exists another character j where attackj > attacki and defensej > defensei.
Return the number of weak characters.
my solution:
class Solution {
public:
int numberOfWeakCharacters(vector<vector<int>>& properties) {
int count =0;
for(int i=0; i<properties.size(); i++){
for(int j=0; j<properties.size(); j++){
if((properties[j][0]>properties[i][0])&& (properties[j][1]>properties[i][1])){
count++;
}
}
}
return count;
}
};
this is my solution. I don't know why this doesn't work. I basically compared from everyone in O(n^2) way and increment count if found such case.
Testcase: [[7,7],[1,2],[9,7],[7,3],[3,10],[9,8],[8,10],[4,3],[1,5],[1,5]]
My Output: 26 Expected: 6